Input.GetButtonDown not working...

Discussion in 'Unity Support' started by Iron-Warrior, Apr 7, 2010.

  1. Iron-Warrior

    Iron-Warrior

    Member

    Joined:
    Nov 3, 2009
    Messages:
    373
    Howdy Unityers,

    Been fiddling around with Unity and have encountered a curious problem. Input.GetButtonDown() doesn't seem to always register...and I can't find any reason at all for it.

    When I put this code into a FixedUpdate()

    Code (csharp):
    1.     function FixedUpdate () {
    2.     if (Input.GetButtonDown("Fire1"))
    3.     {
    4.             Debug.Log(Time.time);
    5.             //Jump();
    6.     }
    7. }
    Each time I click it should update the time in my debugger, right? Well, sometimes it just misses it. Fire1 is mapped to either the left mouse or the ctrl key, and this problem happens if I use the ctrl key or the mouse button. Sometimes when I click, it just doesn't register at all...no other variables or conditions at all. You can copy this code, apply it to an object and click away to see the problem. Is this a known bug? I was searching around and didn't seem to find anything...

    Thanks for any help!
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    25,588
    It's not a bug. You can't really use GetButtonDown in FixedUpdate, because it's only true for the one frame in which it occurs, and it's reasonably likely (depending on the framerate vs. the physics framerate) that FixedUpdate won't be executed during that particular frame.

    --Eric
  3. Iron-Warrior

    Iron-Warrior

    Member

    Joined:
    Nov 3, 2009
    Messages:
    373
    Oh. Good to know it's not a bug.

    After reading your response, I went out and learned about the differences between Update and FixedUpdate...so basically if I'm performing any kind of physics (like CharacterController.Move or AddForce) I should use FixedUpdate, but for anything else I use Update?

    Thanks for the quick response...and just looked at your post count...and it's over 9000! You are one helpful guy Eric, seriously, half my questions I ask are answered by you!
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    25,588
    Yep. Unfortunately somebody already beat you to the "over 9000" thing in another topic. ;)

    --Eric