- Home /
End of animation clip using Animator (Mecanim)
I'm using Mecanim Aninator for my walks, run etc and all that works nicely driven from a C# script. Sometimes I need to start a one-shot animation and that also is not problem using Exit Time. However I need to know when the one-shot has ended from my script.
Any ideas how to do that?
I tried to figure out the same thing but was unsuccessful. Hopefully someone else wants this as well.
I'm using something like this
private IEnumerator BackToIdle() {
//start your new animation here
yield return null; //wait 1 frame so nextState loaded
float time = anim.GetNextAnimatorStateInfo(0).length;
yield return new WaitForSeconds(time); //wait for animation to finish
//do what you want like back to normal speed
anim.speed = 1;
//start idle animation
yield break;
}
You have to uncheck "have exit time" in back transition
Answer by AndyMartin458 · Sep 05, 2013 at 12:33 AM
The last answer in this page seems to answer the question pretty well. http://answers.unity3d.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html You essentially need to poll the animation state until it is not the expected one. Mixamo also created a package that is free and can be hooked to different parts of the animation.
My suggestion is to create a system that will call that event whenever this situation is not true. I can probably do this if you poke me. I'm just going to make an animation event system that does this for you easily.
Thanks. GetCurrentAnimatorStateInfo solved my problem nicely :D
I didn't downvote this, but you need to leave this as a comment on my post, and if you like the answer, click the checkmark to mark it as the answer.
Answer by yonixw · Apr 24, 2015 at 03:53 AM
I had the same problem detecting the end of the clip. I prefer using Animator\Animation events.
It's explained in the API documentation: http://docs.unity3d.com/Manual/AnimationEventsOnImportedClips.html http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html
Answer by Jeet_9811 · Jun 03, 2014 at 11:27 AM
if(stateInfo.IsName("Hat"))
{
if(stateInfo.normalizedTime >= 0.99f)
{
//b_hat = false;
_animator.SetBool("hat", false);
}
}
But this is playing the animation again sometimes.
Have you written correct boolean variable name...? Or are you trying to stop the existing animation before playing the new one..?
Think of it.. You can figure it out easily...
Answer by DantaliaN · Mar 19, 2016 at 05:04 PM
In animator set trigger "IsDestroy" and make connections between "Breake" and "Destroy" animation throw change trigger. "Destroy" animation is empty, Just for triggering
IEnumerator PlayBreak()
{
animator.SetTrigger("IsDestroy");
animator.Play("Break");
while (animator.GetBool("IsDestroy"))
{
yield return null;
}
GameObject.Destroy(this.gameObject);
}
Your answer