Animator. Length of clip of current playing State.

Hello!

I am trying to understand how to get the length of current playing clip in Animator.
I use AnimatorStateInfo and get information about clip playing in current State.

But I don’t understand why following code return the same result in different three cases?

AnimatorStateInfo clipInfo;

clipInfo = animator.GetCurrentAnimatorStateInfo(0);
Debug.Log("walk1"+clipInfo.length+" "+clipInfo.loop);

animator.Play("attack1");

clipInfo = animator.GetCurrentAnimatorStateInfo(0);
Debug.Log("attack1"+clipInfo.length+" "+clipInfo.loop);

animator.Play("attack2");
clipInfo = animator.GetCurrentAnimatorStateInfo(0);
Debug.Log("attack2"+clipInfo.length+" "+clipInfo.loop);

State “walk1” have already been worked before this code. And I get length of clip witch is in state “walk1” in all this three cases.

Try adding some clipInfo.IsName(statename) checks before each section. You may find that you need to give Unity a frame to actually make the transition.

1 Like

Tony is right, even if you call animator.Play() it won’t change the current state right way, the animator need at least one evaluation tick before your new state is played.

Tips and Trick: if this code is editor only you can call manually animator.Update(0); which will tick the animator.

1 Like