1
\$\begingroup\$

I need to call a function immediately after animator animation finished playing, I know I could solve this by adding an event to my animation, but I try it without.

How can I find out if the current animator animation is playing?

On normal legacy animations I solved it by writing a coroutine:

public static IEnumerator playAndWaitForAnim(Animation anim, string clipName, Action callback)
{
    anim.Play(clipName);

    //Wait until Animation is done Playing
    while (anim.IsPlaying(clipName)) {
        yield return null;
    }

    //Done playing. Do something below!
    callback();
}

Which I use like this:

StartCoroutine(AnimationHelper.playAndWaitForAnim(
    myAnimation,
    "ToSettings",
    () => { 
        Debug.Log ("Finished playing");
    }
));

But how does this work with mecanim animator animations?

\$\endgroup\$
  • \$\begingroup\$ May I ask why you don't want to use events? \$\endgroup\$ – ZEKE Nov 2 '17 at 16:53
  • \$\begingroup\$ Because normaly if I would do this with legacy animations I wouldn't need events for this purpose, and I thought that this must be possible in the new system too. \$\endgroup\$ – Black Nov 2 '17 at 16:57
1
\$\begingroup\$

Might not be best but you can add bool parameters in layers in animator and then assign the condition to transition then with these bools so you can check with a script which animation is playing or not , here is example of my code i used recently :

// when animation starts        
if (AnimationState == true)
    {
        anim.SetBool("TurnRight", true); /* this will make the animator 
TurnRight paramter to ture so animation will play */
        //Do something

if (AnimCounter == Timing) /* this if is used to make the TurnRight false 
mean that animation must stop or go to another transition also some 
variables like AnimCounter and Timing i used to stop the animation at 
propriate time */
        {
            anim.SetBool("TurnRight", false);
           //Do something when animation ends


        }
    }

with this as long as the TurnRight bool is true i know that is still playing.

This code might not do the exact thing you want but it can gives you some idea or at least lighten something for you to do your work.
Hope it helps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.