Search Unity

Input.GetKeyDown being registered multiple times

Discussion in 'Getting Started' started by mdzoidberg, Feb 16, 2017.

Thread Status:
Not open for further replies.
  1. mdzoidberg

    mdzoidberg

    Joined:
    Feb 16, 2017
    Posts:
    1
    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.



    Code (csharp):
    1.  
    2. public KeyCode Jump //Set to Space
    3.  
    4. void Update () {
    5.   if (Input.GetKeyDown(Jump)) {
    6.     //Jump
    7.   }
    8. }
    9.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
    Homicide, Ryiah and SarfaraazAlladin like this.
  3. SarfaraazAlladin

    SarfaraazAlladin

    Joined:
    Dec 20, 2013
    Posts:
    280
    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?
     
  4. omriben3110

    omriben3110

    Joined:
    Nov 26, 2017
    Posts:
    1
    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?
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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?
     
  6. Rexor4321

    Rexor4321

    Joined:
    Sep 4, 2017
    Posts:
    3
    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 :)
     
    00Sage00, schcui, Himundo and 2 others like this.
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,190
    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://forum.unity.com/threads/the-truth-about-fixedupdate.231637/
     
    00Sage00 likes this.
  8. Dante_

    Dante_

    Joined:
    Apr 2, 2015
    Posts:
    14
    Yep I did the same and it worked
     
  9. Himundo

    Himundo

    Joined:
    Mar 18, 2019
    Posts:
    1
    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.
     
  10. schcui

    schcui

    Joined:
    Mar 28, 2020
    Posts:
    7
    l've tried to fix this problem all day, thank you !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
Thread Status:
Not open for further replies.