Play animation while button is pressed fails when using boolean parameters.

All right, I got my GameObject with its animator and several states and parameters.

My “need” is making an animation play while a button is pressed (You press for X seconds the button, the animation plays for X seconds).

So, I got my “Idle state”(IS), my “Playing animation State”(PAS).
The transition from IS to PAS uses a boolean parameter “buttonPressed” and the condition value is “true”.

There’s some code around to handle this, and when the user presses a button, the code executes
Animator.SetBool("Transition", true);

So far, so good: The state jumps from IS to PAS, and the animation starts to play. The problem is that, somehow, the value of the boolean resets, executing the transition from PAS to IS, resetting the state machine and re-executing the transition from IS to PAS.

So, I’m scratching my head on what could be wrong here: Is my approach to keeping the animation playing wrong? I’m ignoring any other “right” way to do this? Should I re-check any value on the animator?


Generally, the problem with “input loss” is due to check this input on the FixedUpdate. This problems appear because the input is updated at the beginning of every frame, but the FixedUpdate can be called outside that frame or multiple times in it.

The best you can do is to check the ups and downs of the input on the Update in order to have the freshly updated input values.

If doing this check in the Update is not possible, another solution is to check the “GetButton/GetKey” instead of “GetButtonDown/GetKeyDown” and “GetButtonUp/GetKeyUp”. Generally this value works fine on the FixedUpdate, but not always.