[Solved] GetKeyUp registering more than once.

When using this bit of code.

if(Input.GetKeyUp("x")){
            Debug.Log("UP");
            dampening = !dampening;
            if(dampening){
                gameObject.rigidbody.drag = dragAmount;
            } else {
                gameObject.rigidbody.drag = 0;
            }
        }

The key will register as ‘up’ several times on one release of the key. Any idea what might be causing such an issue?

Depends on where the code is called. Let me guess - OnGUI()? OnGUI is not only called once like Update() - how often exactly depends on the code included in that function.

1 Like

It’s called from a function I placed in FixedUpdate

The input is updated once per frame. The problem is that FixedUpdate can be called several times per frame, just as it was mentioned for OnGUI. You have to call it in Update to get the correct result.

1 Like

Could be that this is the same then, depending on your framerate. FixedUpdate() is tied to the physics timing, meaning that if your framerate is lower, FixedUpdate() might be called multiple times during a frame. GetKeyUp() however is only updated once per frame.

Gotcha. I wasn’t quite clear on the difference between Update() and FixedUpdate()

Doc says: You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has pressed the key and released it again.

Try calling it from Update.