- Home /
SetTrigger animation won't wait for correct time before continuing code
Hi,
I have an attack animation that is called via trigger and I want it to wait for the duration of that animation's length, then move on to next code. The issue is that the wait time is actually the wait time for the 'Idle' animation and not the triggered 'Attack' animation.
So what happens is the character plays the attack animation and then waits a little bit longer before any other code continues because it is running off the Idle.
The attack animation should wait 0.7 sec and the Idle waits 2.6 seconds.
Could you tell me what i'm doing wrong?
I've tried a couple of ideas:
public IEnumerator Attack ()
{
myAnimator.SetTrigger("Bite");
yield return new WaitForEndOfFrame();
AnimatorClipInfo[] clipInfos = myAnimator.GetCurrentAnimatorClipInfo(0);
AnimationClip clip = clipInfos[0].clip;
float clipDuration = clip.length;
string clipName = clip.name;
Debug.Log("ATTACK ANIMATION: duration: " + clipDuration + " Name: " + clipName);
yield return new WaitForSeconds(clipDuration);
}
and i've also tried:
public IEnumerator Attack ()
{
myAnimator.SetTrigger("Bite");
yield return new WaitForEndOfFrame();
Debug.Log("ATTACK ANIMATION: duration: " + myAnimator.GetCurrentAnimatorStateInfo(0).length);
yield return new WaitForSeconds(myAnimator.GetCurrentAnimatorStateInfo(0).length);
}
and:
public IEnumerator Attack ()
{
myAnimator.SetTrigger("Bite");
yield return new WaitForEndOfFrame();
Debug.Log("ATTACK ANIMATION: duration: " + myAnimator.GetCurrentAnimatorStateInfo(0).length + myAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime);
yield return new WaitForSeconds(myAnimator.GetCurrentAnimatorStateInfo(0).length + myAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime);
}
I have tried putting a debug in update to see if 'Bite' is ever called and it does get called eventually, so i know it can be found somewhere in there... I just don't know if i'm using the right code to access the clip length to do a wait for?
Answer by jampakdd · Jul 07, 2021 at 10:59 PM
Just ran into this on Unity 2019.4.22f1... Will update if I find a solution.
Your answer
Follow this Question
Related Questions
Wierd issue with Coroutines? 2 Answers
Ienumerator wait for event 0 Answers
Sequentially wait for multiple coroutines to finish 0 Answers
WaitUntil and WaitWhile don't work 1 Answer