I am creating this directly in the post editor - so nothing’s tested and there may be typos - but here’s the gist:
The Player
Make a script called Mover.cs
to move the player
public class Mover : MonoBehaviour
{
[SerializeField] float _moveSpeed = 1f;
private void Update()
{
var moveVector = Vector3.zero;
moveVector += Vector3.right * Input.GetAxis("Horizontal");
moveVector += Vector3.forward * Input.GetAxis("Vertical");
moveVector = moveVector.normalized;
transform.Translate(moveVector * _moveSpeed * Time.deltaTime);
}
}
Add an empty game object to the scene, call it ‘Player’ and tag it with ‘Player’. Give it some visuals, a collider and a rigidbody. Make the rigidbody kinematic, and set gravity to 0. Put the Mover
script on it.
Make a script called Player.cs
to hold some player info
public class Player : MonoBehaviour
{
private bool _hasKey = false;
public bool HasKey() => _hasKey;
public void SetHasKey() => _hasKey = true;
}
Put this script on the player game object. It will only hold whether the player has the key or not.
The Key
Make a script called KeyPickup.cs
to detect when the player ‘picks it up’
public class KeyPickup : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player")) return;
if (!other.TryGetComponent<Player>(out Player player)) return;
player.SetHasKey();
Destroy(gameObject);
}
}
Make a new game object and call it ‘Key’. Give it some visuals, and add a collider. Set the collider to trigger, and put the KeyPickup
script on it.
The Car
Make a script called CarTrigger.cs
to detect when the player is within range. You could use a collider, but I’ll do something different.
using UnityEngine.SceneManagement;
public class CarTrigger : MonoBehaviour
{
[SerializeField] float _detectRadius = 3f;
[SerializeField] string _targetScene;
private Player _player;
private void Start() => _player = FindObjectOfType<Player>();
private void LateUpdate()
{
if (!_player.HasKey()) return;
if (Vector3.Distance(transform.position, _player.transform.position) > _detectRadius) return;
SceneManager.LoadScene(_targetScene);
}
}
Make a new game object and call it ‘Car’. Give it some visuals and put the CarTrigger
script on it. Set your target scene and detect range.
Job’s done.
Edit
Oh, I forgot the UI
The UI
Make a script called KeyPickupUI.cs
public class KeyPickupUI : MonoBehaviour
{
[SerializeField] Sprite _keySprite;
private void Start() => _keySprite.enabled = false;
public void SetHasKey() => _keySprite.enabled = true;
}
Make a new canvas game object, and add a Sprite to it. Give it a ‘key’ image. Put the KeyPickupUI
script on the canvas and drag the sprite object into the Key Sprite
field in the inspector.
Updates
We need to update the KeyPickup.cs
script
public class KeyPickup : MonoBehaviour
{
// Get a reference to the UI
[SerializeField] KeyPickupUI _keyPickupUI;
private void OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player")) return;
if (!other.TryGetComponent<Player>(out Player player)) return;
player.SetHasKey();
// Add this line
_keyPickupUI.SetHasKey();
Destroy(gameObject);
}
}
Drag the canvas into the Key Pickup UI
field in the inspector
/Me: Made a game in 18 minutes…
Another Edit I seem to have misread your question, but the above should still be helpful