Button Registering twice when pressed
I'm making a hiding mechanic for my game that allows the player to hide in a closet when they press E. They can then click E again to get out of the closet.
I am using Input.GetKeyDown(KeyCode.E) when taking input, but it seems to immediately register the second button click. I thought that GetKeyDown only registered once until the second button click?
Why is this happening?
Here is my function:
public class Hide : MonoBehaviour
{
public GameObject player;
public GameObject warpTarget;
private CharacterController characterController;
[SerializeField] private bool isHiding = false;
private void Start()
{
characterController = player.GetComponent<CharacterController>();
}
private void Update()
{
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
Debug.Log("Player is ready to hide");
if (isHiding == false)
{
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Player is hiding!");
isHiding = true;
PlayerPrefs.SetFloat("PlayerX", player.transform.position.x);
PlayerPrefs.SetFloat("PlayerY", player.transform.position.y);
PlayerPrefs.SetFloat("PlayerZ", player.transform.position.z);
characterController.enabled = false;
player.transform.position = warpTarget.transform.position;
player.transform.rotation = warpTarget.transform.rotation;
characterController.enabled = true;
}
}
if (isHiding == true)
{
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Player is not hiding anymore");
isHiding = false;
player.transform.position = new Vector3(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
}
}
}
}
}
Comment