- Home /
Mecanim: normalizedTime is sometimes wrong
Hello,
I am trying to achieve something incredibly simple but it has taken the past 2 days to get something workable that would have been doable in a few seconds with the legacy system. I am trying to have a method that plays an animation, waits until the animation has completed, then stops. I have figured out how to do this using some non ideal ways but I want to know how to do this like this.
public IEnumerator PlayOnceUntilFinished(string paramName)
{
animator.CrossFade(tempName, 0.25f, 0, 0.01f);
yield return null;
if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.95f)
{
Debug.Log("Current is right");
while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.95f)
{
//Debug.Log(animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
Debug.Log("Attempting current...");
yield return null;
}
}
else if (animator.GetNextAnimatorStateInfo(0).normalizedTime < 0.95f && !(animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.95f))
{
Debug.Log("Next is right");
yield return new WaitForEndOfFrame();
}
else if (animator.GetNextAnimatorStateInfo(0).normalizedTime < 0.95f && animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.95f)
{
Debug.Log("Both are right.");
}
Debug.Log("Animation completed.");
Here is what is happening - sometimes 'Current is right' fires, as well as it's while statement (which works beautifully) but most of the time 'Next is right fires' and skips the current 'if' statement completely. I need to be able to wait and know when the animation is completed due to my state machine on my AI character so triggers will not work for me (also because of my system, triggers fail a lot of times to show the animation because the machine will continue without knowing is it still happening)
I am almost positive the solution to the issue is to some how wait long enough until the next state (which should be the current) has transitioned into the current, but how would I go about knowing this or figuring it out? The solution must be so simple but I for the life of me cannot see it after so much time spent. Thank you for any and all help!
PS: Here is how I currently do it:
if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("Base Layer." + mecanimActionName))
{
// Avoid action until animation is finished
this.playingAnimation = true;
if (debug)
Debug.Log("Beginning Animation");
}
else if (this.playingAnimation)
{
this.playingAnimation = false;
// Animation has just completed!
playCounter++;
if(playCounter == playCount)
{
if(debug)
Debug.Log("Played Animation " + playCounter + " times.");
EndAction(true);
}
else
{
if(trigger)
animator.SetTrigger(mecanimActionName);
//StartCoroutine(PlayOnce(mecanimActionName));
StartCoroutine(PlayOnceUntilFinished(mecanimActionName));
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613143952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with the wrist bones during conversion to Mecanim's skeleton. 0 Answers
Animator set current animation normalized time. 1 Answer
Mecanim, my object deforms when I put my own animations! 1 Answer
Distortion in blending animations of walk 0 Answers
creating new animations with mecanim 0 Answers