Input.GetKeyDown being registered multiple times

Complete newbie to Unity here!

Im having difficulties in how to get around Input.GetKeyDown being triggered multiple times.

According to the documentation, it seems as it is the automatically repeated keypress settings “at the end user”. That is neat-o and all, but how can i work around it? Is there a way to distinguish between an auto-repeated keystroke and the actual “initial key being pressed down” event?

e.g. say i have the code below; pressing and holding space will “Jump” untill you release it.

public KeyCode Jump //Set to Space

void Update () {
  if (Input.GetKeyDown(Jump)) {
    //Jump
  }
}

Not sure what you mean; GetKeyDown can not fire again until the key is released, as the docs say. “It will not return true until the user has released the key and pressed it again.” Either you have the script attached multiple times, or you might have a keyboard malfunction.

–Eric

3 Likes

Eric is right. My only other suggestion is that there’s a flaw in your jump code.

If you Debug.Log from inside of your Input.GetKeyDown() call, is the message output more than once in the console?

I got a similar problem, i did put a debug log. it registered up to 7 times in one press, is it something with the frame update?

Like everyone (and the documentation) has said, it fires only during the frame the key press started. Do you maybe have this script attached to 7 different objects in the scene? Can you share some code and/or screenshots of your project with us?

1 Like

I had the same problem, and after ten minutes of fiddling, I found a solution that worked for me: When I changed my FixedUpdate to an Update it registered perfectly. Hope this can help somebody else :slight_smile:

6 Likes

If you’re curious on the specifics as to why I recommend the following thread. Basically though the short story of it is that the function doesn’t run at a fixed interval but rather runs as much as it needs to right before the next update occurs to catch up (eg if it’s behind five intervals, it’ll run five times back to back next time it’s scheduled to run).

https://discussions.unity.com/t/530594

1 Like

Yep I did the same and it worked

Me Too. I dont know why, but the FixedUpdate was giving me some random jumps, with random forces. The Update did the job just fine.

l’ve tried to fix this problem all day, thank you !!!