- Home /
Animator - Wait until animation finishes
Hello! I'm writing a script which will handle monster attack. Script plays Attack animation in Animator component by trigger. I want to stop monster from moving before the animation start and resume his moves after animation ends. I wrote this so far, but agent starts before animation ends. I must add that during the fight monster may get pissed and start to move faster(animation play speed multiplier).
anim.SetTrigger("Attack");
agent.Stop();
yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
Debug.Log("Attack has ended!");
agent.Resume();
If you need something more than this code please let me know. I would really appreciate your help
I find it easier to just add AnimationEvents to the clip at the start and/or end which, in your case, stop and start the movement by calling those functions. It's much more reliable than using a CoRoutine because an AnimationState might get interrupted and this wouldn't be caught be the CR timer.
I didn't know about existance of such thing. I'll check it and let you know.
Hi, i've been playing with your idea for a while. This solution looks good, but i don't like the thing that i have to divide my function into 2 or 3 other functions.
Have you checked, that the "CurrentAnimatorState" is the state you think it is? Happened to me, that I checked the previous state, not the current.
I'm not sure if i understand you. Is the state a current animation being played? If yes, than the State is the right one.
Answer by Eco-Editor · Jul 19, 2017 at 01:45 PM
Thanks so much @marcin107 This did the work for me: yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
I know this post is old but this solution worked for me. Why does this work, wouldn't this code add the total length of the animation with the length of the animation normalized between 0-1? I don't get it I've been trying to fix this for hours and I finally have but I don't understand how this works!
Answer by Crimx · Jun 28, 2016 at 07:23 AM
Try this code. I was using it for my game and it work perfectly. But I was using it at Update method. Haven't try this code at IEnumerator though
//if animation with name "Attack" finished
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Attack"))
{
// do something
}
this works but you wrote it the wrong way. this need an exclamation mark at the beginning like so if (!anim.GetCurrentAnimatorStateInfo(0).IsName("Attack")) {
and it should be commented as, if animation with name "Attack" is not playing do something
Answer by petru23 · Nov 22, 2018 at 03:52 PM
@marcin107 This worked for me as well: yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
,@marcin107 this worked for me as well thank you yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
Your answer
Follow this Question
Related Questions
Conflicting Animator SetTrigger 0 Answers
Set boolean to false when animation changes? 1 Answer
pause Animation 0 Answers
Is it possible to always make animations play differently? 0 Answers