Input with FixedUpdate(): avoiding input loss with high frame rates?

It’s pretty simple actually, but a bit tricky to understand at first. Update() and FixedUpdate() are two separate “execution flows”. Several Update() calls may happen between each FixedUpdate() call, and several FixedUpdate() calls may happen between each Update() call. More information and details on this here:

The input state is updated before each Update() call. In your case, think on what happens in your examples when Update() is called twice before the next FixedUpdate() call. Assuming the “button down” event is triggered in the first Update() call:

  • LOSS: Your flag is set to true in the first call but the second Update() call will set it back to false, as GetButtonDown() returns false. It will be false when FixedUpdate() is called, hence the input loss.
  • NO LOSS: Your flag is set to true in the first call. In the second call GetButtonDown() returns false, so the flag value remains unmodified. It remains true when FixedUpdate() is called, so input is processed properly.

An alternative that also processes the input properly is:

    void Update()
    {
        jum = jum || Input.GetButtonDown("Jump");
    }

    void FixedUpdate()
    {
        if (jum)
        {
            playerRigidbody2D.AddForce(new Vector2(0f, powerJump));
            jum = false;
        }
    }

This will OR the value returned by GetButtonDown() with the current value of the flag. So if it’s already true and GetButtonDown() returns false, the flag will remain true.

1 Like