What is the proper way to wait for an Animator Controller to update?

I’ve been noticing a slight problem with this block of code after doing some debugging. It was actually my initial thought when I saw something was wrong. Apparently, it seems that SetTrigger() waits a frame or longer before finally transitioning to the new state. Which kind of sucks in this situation.

Here’s a little explanation:
NullState is the Idle state and baseAttack is the state it should transition to before calling WaitForSeconds(). This script is suppose to wait the length of baseAttack before performing some other actions within the method.

The print statement below, before I added WaitForEndOfFrame(), would usually print True|False|0.3, After adding WaitForEndOfFrame(), it sometimes prints False|True|1 (the desired result) and then, at random, it will sometimes print True|False|0.3.

The Issue: The print statement isn’t consistent and it doesn’t seem to be waiting for the transition, after SetTrigger() is called, to move to the next state. In short, it’s not fast enough (or to be even more precise, I’m most likely ignorant to this subject and I’m doing something terribly wrong)!

public IEnumerator PlaySkillAnimation(string name)
{
    animator.SetTrigger(name);

    animator.SetFloat("movementSpeed", 0);

    yield return new WaitForEndOfFrame(); // new code added in to finally discover that it 'helps' some

    print(animator.GetCurrentAnimatorStateInfo(0).IsName("NullState") + "|" + animator.GetCurrentAnimatorStateInfo(0).IsName("baseAttack") + "|" + animator.GetCurrentAnimatorStateInfo(0).length); // just some debugging

    yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
}

That’s pretty weird. I usually get problems like these when, for some reason, my Triggers aren’t resetting by themselves, so I have to use ResetTrigger manually.

Anyway, I think you’ll be able to get what you want by using a State Machine Behaviour. If you want to do something just as the animation ends, just use the OnStateExit.
See Survival Shooter Training Day Phases - Unity Learn for more.