- Home /
Get an Animator state inside an AnimationEvent callback of the last frame
I would like to write a method that traces debug info about animator state. Its purpose is to detect which state called AnimationEvent callback. But I have a problem with the last frame. When I bind callback to the last frame and request current state info of Animator by GetCurrentAnimatorStateInfo() inside this callback I'm getting the next state.
Here on the picture OnAnimationEvent() is bound to the last frame:
It seems Animator performs transition before executing the last event. Does anyone know how to get actual state for this case? Or maybe anybody knows how to detect which state executed callback in runtime for Debug purposes?
Answer by FIFTYTWO · Jan 21, 2017 at 06:44 AM
Finally I've found a simple solution that is suitable for me. I've changed callback signature to accept AnimationEvent as argument instead of int. The great thing is serialization of old signature mapped to the new one automatically and I don't need to remember all motions where I used the callback and setup its parameters again in the Animation window. Here is my callback that logs the necessary info. Also I'm using int from AnimationEvent instead of direct passing:
public void OnAnimationEvent ( AnimationEvent evt )
{
Debug.LogFormat( "I'm called by animator of {0} in clip {1}",
GetComponent<Animator>().gameObject.name, evt.animatorClipInfo.clip.name );
Debug.Log( "I'm using int parameter: " + evt.intParameter );
}
AnimationEvent also contains animatorStateInfo that can be used to detect the state too.
Answer by IgorAherne · Jan 19, 2017 at 09:24 PM
If it's applicable, you can use StateMachineBehaviors.
Those act just like the Monobehaviors, except they can only be attached to "AnimatorState", in mechanim. They provide all the callbacks you need.
Your answer
Follow this Question
Related Questions
Why does this animator appear to occupy two states, but report only one? 0 Answers
Skills controlled by animation (event callbacks and parameters) 0 Answers
Accessing AnimationState in Animator 0 Answers
Animation motion on base layer does not play after additional motion on second layer 0 Answers
Mecanim Animate Physics - When to use? 0 Answers