GetCurrentAnimatorStateInfo returns incorrect Animation State when called from an event.

I have an event at the very beginning of an animation (well the end actually, but the beginning when playing backward) that calls a method in a script which has the following code:

float animationSpeed = animator.GetCurrentAnimatorStateInfo(layerIndex).speed;

When I run it, it returned the incorrect speed value for the Animation State that the event was in.

After some digging around I realized that GetCurrentAnimatorStateInfo() is returning the incorrect State !!! It is returning the previous state (one with an animation with no events) and not the one with the animation with the Event that supposedly fired the code!

Has anyone seen this behavior? Is this a bug or incorrect implementation/understanding on my part?

It could actually not be in the new state yet. For example, if you call Play(“State Name”), it won’t actually be in that state until after the next animation update. So I suspect it’s by design, even though it’s rather unhelpful.

You might be interested in Animancer (link in my signature) which doesn’t have that issue and gives you much easier access to things like speed and time.

1 Like

But how is it able to execute a call from an event inside the clip, before being inside the state with the clip?

Thanks for the link :slight_smile:

I don’t know the details of how it works internally, but from what I understand the functionality for actually playing clips and checking their events is entirely separate from the concept of a state. So it could simply be checking the events first and only setting the current state info at the end of the update.

Fun times.

I did look into running the animations directly then got discouraged by having to flag the Animations as legacy. But I might have to just do it…

Thank you Kybernetik. Your tool does look very interesting.