hi there
apologies for the noobiness of this question!
i am authoring my first unity game and have come across a simple but painful issue.
I have googled but was surprised that i could not find the answer to what must be a common issue???!!?
I have the following in the FixedUpdate method on a game object with a rigid body component:
if(Input.GetKeyDown(KeyCode.S)){
Debug.Log ("s pressed");
if( playstate == 0){
spinUp();
}
}
My issue is that I am getting several triggers happening in quick succession when I consult the logs. These are firing several different callbacks.
is there a common practice method for debouncing key presses in unity or have i missed something much more fundamental?
many thanks in advance!!!
Input checks in Fixed Update don’t work properly.
Lets say I just pressed a key, Input will return true for that key until the end of one frame. But since FixedUpdate is independant from this loop, it will only return true in there, if FixedUpdate gets called inbetween the start and end of a frame rendered. Most of the time you will have a higher framerate than the 50fps of FixedUpdate. Lets say you presed the key, three Update’s are called and then FixedUpdate After the first Update the Input.KeyDown is already false.
So in short do Input calls in Update and not FixedUpdate 
Hope this helps, Benproductions1
You should not put it in fixedupdate(), but in update().
Input.GetKeyDown() is set every Update() cycle. You are using it inside the FixedUpdate() which sounds like it is getting called multiple times per Update() cycle.
I would check Input on each Update() then call spinUp() there, or set a flag to spinUp() on the following FixedUpdate().
thanks for your replies!
I was using FixedUpdate because I am adding angular velocity to a rigid body. I read that I need to keep any rigid body scripts within FixedUpdate to keep the physics engine stable.
Do I disregard this advice or is there a common way to add debounced key presses to rigid bodies within the FixedUpdate method?
many thanks