GetKeyUp in OnTriggerStay sometimes execute twice
I found that in the OnTriggerStay code block, when a key pressed, the events were sometimes executed twice. Why? private void OnTriggerStay(Collider other) { if (Input.GetKeyUp(KeyCode.E)) { Debug.Log("1"); } }
(It sometimes output "1" in the console twice...)
Oh, I think I got the solution. I can just use a delay, I think the event sometimes will be called twice when press a key. bool triggerDelay = false; float delayTime = 0.2f; private void OnTriggerStay (Collider other) { if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.E) && triggerDelay == false) { Debug.Log("1"); triggerDelay = true; } if (triggerDelay == true) { delayTime -= Time.deltaTime; if (delayTime <= 0) { triggerDelay = false; delayTime = 0.2f; } } }
Answer by Pat_Max · Jul 03, 2018 at 02:48 AM
private void OnTriggerStay (Collider other)
{
if (Input.GetKeyUp(KeyCode.E))
{
Debug.Log("1");
}
}
sorry, the script wasn't so clear...
Your answer
![](https://koobas.hobune.stream/wayback/20220612165930im_/https://answers.unity.com/themes/thub/images/avi.jpg)