- Home /
How to register a held down key.
Hello, I have the code below inside FixedUpdate
if (Input.GetKeyDown (KeyCode.D))
{
print ("D key pressed");
rigidbody.AddRelativeTorque(0,1,0);
}
but this does not work if 'D' is held down it runs once on each key press, and i've discovered that using a while loop freezes unity completely. My question is how do I make the if statement run every frame if the 'D' key is held down?
Do not use Get$$anonymous$$eyDown in FixedUpdate. FixedUpdate doesn't run every frame, so it can and will miss input events. (Not that you actually want to use Get$$anonymous$$eyDown in this case anyway.)
Answer by dan19 · Jul 28, 2013 at 03:54 PM
Maybe what you're looking for is Input.GetKey(). Also, I didn't know about this method before a minute ago. That's how long it took to look up the documentation.
Please note that even if this works, Eric5h5 has a point about using Input in FixedUpdate(). I completely missed it. Although my own gut feeling tells me that while Get$$anonymous$$eyDown() is a problem, Get$$anonymous$$ey() might be okay in a FixedUpdate(). But I would think twice.
Actually Get$$anonymous$$ey would work in FixedUpdate, since it returns true every frame as long as the key is held down, therefore there's no chance for the input to be missed. None of the *Down or *Up functions would work though.
Your answer