How do I know when an animation has finished being played?

Good morning ,

how to know please when any animation has finished being played (it means when to know the end of an animation) to be able to realize other things consequently at the end of the animation

Thank you very much and good day?

Cordially .

Hii @Bioshok7 !!

You can always check if some animation is runing with the Animation.IsPlaying :

Animation anim                   //Animation component
if (anim.IsPlaying("Qwerty")     // If animation named Querty is playing....
 { do something }

Did it help? Upvote !!
If need more, just ask with @tormentoarmagedoom

Bye :S :smiley:

Hi @tormentoarmagedoom,

Thank you very much ,

but I’m talking about source code to implement as soon as the animation has finished being played and not checking whether an animation is being executed or not,

for example in my case I would like to check if the animation of player failure has finish being played , to be able to reload and restart the level again,

Thanks again :slight_smile: .

You can use GetCurrentAnimatorStateInfo().length to get the length of the currently playing animation. Such as:

Animator anim;

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

public IEnumerator Foo()
{
    float animLength = anim.GetCurrentAnimatorStateInfo().length;
    yield return new WaitForSeconds(animLength);
}

↑ Untested (Slow day at work)

Hopefully using this with WaitForSeconds(animLength) should get you where you need to go!

Hi all, bit late to the party, but just cooked this up for a client. It will call OnAnimationStopped() using System.Action once the animation has… stopped. I hope it helps some people, I personally think it’s the cleanest solution for my purposes at least.

using System;
using System.Collections;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {


    public string animClipNameToPlay; //AnimationClip must exist in _anim component
    Animation _anim;

    void Start()
    {
        _anim = GetComponent<Animation>();
        PlayAndWaitForStop(animClipNameToPlay);
    }

    protected void OnAnimationStopped() {
        Debug.Log("Animation stopped playing.");
    }

    public void PlayAndWaitForStop (string animClipName) {
        _anim.Play(animClipName);
        StartCoroutine(Listener_AnimationStopped(_anim, delegate {OnAnimationStopped();}));
    }

    public IEnumerator Listener_AnimationStopped(Animation a, Action callback) {

        while (a.isPlaying) {
            yield return null;
        }
        callback();
    }
}