Keys and doors
The unlock prompt appears when the player carries a key within range of a locked door. Pressing E plays the unlock animation and sound and disables the door collider.
Gameplay video
Unlocking the door
bool InRange()
{
return Vector3.Distance(player.transform.position, door.transform.position) < unlockRange;
}
void TryUnlock()
{
if (player.IsHoldingKey && InRange())
{
doorAnimator.SetTrigger("Unlock");
doorCollider.enabled = false;
FN_SoundManager.PlaySound(SoundType.DoorUnlock, 1f);
}
}
Requires a carried key and a player position within unlockRange.
Showing the unlock prompt
void Update()
{
if (player.IsHoldingKey && InRange())
{
interactPrompt.Show("Press E to unlock");
if (Input.GetKeyDown(KeyCode.E))
TryUnlock();
}
else
{
interactPrompt.Hide();
}
}
Shows the prompt while a key is held within range. Pressing E calls TryUnlock().