GetKeyDown() tells us if the user pressed the key down during the last
Update() function. This allows us to react during the frame where the button has been pressed, without reacting in the same way for each consecutive frame where the key is still being pressed.
This makes things a bit more tricky, trying to perform the same check in an
Update() runs as many times as your hardware can handle, per second. It is entirely possible for two or more
Update() cycles to run in between pressing the button, and triggering
OnTriggerStay(). Remember, collision is physics based, and only checks with
FixedUpdate(), which typically runs far less infrequently than
In the end, we could and up with an
OnKeyDown() that initially flips to
true, but flips back to
false before we actually get to check it. For this reason, caching this value (as suggested in an alternate answer) runs into this same problem.
Having experienced this, before, your best bet is to use
OnKey() checks if the key is down, regardless. If the key is down, it will always return
true. In situations where this would lead to issues resulting from always checking the key, you can then start to think about caching a local variable to keep record of whether they key has just been pressed, for the purposes of your
OnTriggerStay() method, or if the key is still being pressed from a previous