How to wait for an animation to finished in Unity C#

I want to wait for an animation clip to finish after I set the trigger.

  private void Start()
    {
        anim = GetComponent<Animator>();
    }


    private IEnumerator Die()
    {
        // Play the animation for getting suck in
        anim.SetTrigger("Shrink")     

        // Wait for the current animation to finish
        while (!anim.IsInTransition(0))
        {
            yield return null;
        }

        // Move this object somewhere off the screen

    }

The animation plays, but it doesn’t wait to finish before it moves on to the code after the while loop.

Here is my animator component:

@BenjaFriend22

Hi there,

you could take look at StateMachineBehaviour in scripting manual.

Maybe you could put your code into method OnStateExit, this should be run on end of state.

1 Like