- Home /
Unity Mecanim Behaviour OnEnter is called too late
Hi!
I have a Behaviour on one of my Animator States and I made an Event which is fired then the OnEnter() method is called.
My problem is that I have some Animation Events on the same animation and the Animation Event if fired before my event from the Animator State OnEnter().
The Animation Event is at the 1st frame of the Animation. The execution order seems off for me, first the event inside the animation is executed after that the OnEnter()? Shouldn't this be the other way around?
I checked, it happens it the same frame but the execution order if still off.
Thanks,
Answer by Immanuel-Scholz · Jun 30, 2015 at 07:32 PM
What are the reasons that you think animation events and StateBehaviour.OnEnter should be called in any particular order - except from "it would maybe make sense from some academic purity point of view"?
As I see it, animation events are reactions from the animation system to certain points in an animation.
StateBehaviour.OnEnter is a callback to process when entering a state machine state in mecanim (that may or may not play some animations - beside doing other things).
Animation Events are also triggered when playing an animation via the Animation component. Or probably when using AnimationClip.SampleAnimation (although I haven't tried that one).
OnEnter callbacks are also called, if you don't even have an animation on the mecanim state.
I rather have Unity not guarantee any particular order of things if this makes it easier for them to do better performance. ;)
Do you have a concrete problem to solve? maybe it can be solved in a different way that does not depend on the internal order of things within Unity?
I solved the problem related to the execution order, but still, I think OnEnter() should be called before any animation event.
I don't think it is related to performance, I just think they are 2 separate systems (as you said) and no one thought about these kind of situations.
OnEnter() and OnExit() are ment to detect enter/exit from an animation state. If animation events are called before OnEnter() than at that moment I can't deter$$anonymous$$e the state from which is co$$anonymous$$g based on OnEnter(), and by this OnEnter() is no use for me in this case. Of course OnEnter() + Animation Event combo will work perfectly for any other situation, in this one it will not, and by this becomes inconsistent which can lead to a lot of nasty bugs.
I think the whole idea of the mapping of animations to states is fragile.
You can play multiple animations at the same time (e.g. while blending between them). But you can be in only one state at a time. AnimationEvents as they are in Unity are tight to the animation, not a state.
But I give you that: There is no good way of specifying "something related to a normalized time of a mecanim state (which has a fixed length based on an animation)", so naturally, the first that obviously jumps in anyones face are animation events. ;)
Your answer
Follow this Question
Related Questions
How to Determine What AnimationClip/AnimationState Generated an AnimationEvent? 0 Answers
Mecanim events firing incorrectly when clip playing backwards? 1 Answer
Skills controlled by animation (event callbacks and parameters) 0 Answers
Get an Animator state inside an AnimationEvent callback of the last frame 2 Answers