Search Unity

[Solved] Erratic behavior when switching animation states using triggers

Discussion in 'Animation' started by seejayjames, Dec 31, 2019.

  1. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    I have two animation states that are called by triggers, attached to keystrokes in script (GetKeyDown). If I use the default transitions that show up when adding a state (where there is some overlap between each animation clip), then when I press the key to transition, there are occasional times when it will switch to the new state after finishing the current state, play through once, then transition back to the one it came from. Expected behavior is to remain in that other state and loop, which works *most of the time*. I’ve also gotten it to do this by clicking the trigger buttons in the Animator itself, though it seems to happen less often (?). It also seems to happen more often right after you click Play, then not as much later on…just to add to the confusion.

    I tried using no transitions at all (“instant” ones with no overlap), and that seemed to fix it for awhile (almost until posting this!), but a bit later in testing it started happening even with these settings. Haven’t tried restarting Unity, but that is an unacceptable workaround, especially as I’ve deleted and re-created the transitions multiple times, so I can’t see there’s any way it’s remembering earlier settings somehow.

    Note that this is not just my project, I’ve seen it in multiple student projects as well. Again, it’s erratic and difficult to reproduce consistently.

    I can play the animations manually through script easily enough, but this is more cumbersome if I want the current animation to play through before switching. This also negates the whole purpose of the state machine and its accompanying UI and functionality.

    Hope this can be looked into and figured out, or if there’s an explanation as to why it’s expected behavior (and what a workaround is) that would be great to hear. Otherwise, using the state machine has proven to be unreliable. This is unfortunate as there are so many excellent features in it!
     
  2. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Never mind! I wasn't resetting the Triggers. Makes perfect sense now, especially that it was erratic...the timing of pressing the key to change states could potentially either make the "error" happen or not. Seems to work flawlessly now.

    Sorry for the noise, but hopefully this is helpful to others!

    https://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html