![]() ![]() It is entirely possible for two or more Update() cycles to run in between pressing the button, and triggering OnTriggerStay(). Update() runs as many times as your hardware can handle, per second. ![]() This makes things a bit more tricky, trying to perform the same check in an OnTriggerStay() method. 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. GetKeyDown() tells us if the user pressed the key down during the last Update() function. If you want to go really modular, you might use a C# event that the trigger subscribes to in OnTriggerEnter() (and unsubscribes to when leaves) but that might be too complex for your needs. for like 250ms or something small so that it You might want to do a timestamp check here too IsEPressed = isEPressed || Input.GetKeyDown(Ke圜ode.E) You can also just use an OR expression which you need to clear yourself later private bool isEPressed = false mentions Input.GetKey which returns true as long as you're holding the key down. ![]() There are a couple ways to fix this though. There's even more complexity (physics timesteps are different from Update() timesteps, and OnTriggerStay() isn't called every frame) so you're basically relying on a race condition to check E which is not reliable.įixing this isn't so easy as mentioned by (originally I had a simple flag set using GetKeyDown). So trying to use it from OnTriggerStay() will most likely not work as there's a good chance that Update() will occur before OnTriggerStay() is called and reset the keys state (it also depends on when and how they handle user input from how the operating system reports it). It will not return true until the user has released the key and pressed it again. You need to call this function from the Update function, since the state gets reset each frame. Unity chose to reset the flag that services Input.GetKeyDown every frame as is noted in their docs. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |