4
\$\begingroup\$

I was looking at the Script Lifecycle Flowchart and suddenly I got thinking about Animation events.

Where do they fit, inside this flowchart? In which position would they be executed?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I just did a quick test, and they appear to be called in the grey section labelled "Internal Animation Update" (as one might expect).

ie. after yield StartCoroutine and before LateUpdate.

Rendering happens after LateUpdate, so visible changes caused by the firing of an animation event should be seen immediately in the current frame.

You can verify this by writing a simple script that prints to Debug.Log in a coroutine (other than WaitForEndOfFrame or WaitForFixedUpdate), LateUpdate, and a public method called by an animation event, and observing the order in which the printed lines appear.

Edit: It looks like animation changes caused in response to an Animation Event function call do not get acted on by the animation system until the next frame.

I tested this by responding to an event with Animation.Play(some other animation) and pausing execution in the subsequent LateUpdate. The first frame of the new animation was not yet reflected in my object's state, and animation events on that first frame had not yet fired.

So, at least with regard to the animation that triggers it, animation events seem to occur strictly after the internal animation update. (Its possible they're interleaved between updates of different animations - I haven't checked this case)

Annotated script flow chart showing animation events after the animation update

I have not verified this for objects using an Animator controller, but I expect them to behave the same way. I'd appreciate a correction if your experiments show otherwise. :)

\$\endgroup\$
11
  • \$\begingroup\$ Does this mean that the changes that the Animation event does will appear on the frame printed in this lifecycle? Or the next one? \$\endgroup\$ Oct 17, 2015 at 9:21
  • \$\begingroup\$ @Dbugger I've edited my answer to clarify this. You should see any visible changes in the current frame, since rendering doesn't happen until after the internal animation update step. This is later than the Update() method though, so if another script's update depends on new information from an animation event, you may want to put that script in LateUpdate to ensure it's not lagging a frame behind. \$\endgroup\$
    – DMGregory
    Oct 17, 2015 at 14:22
  • \$\begingroup\$ So Animation Events happen after Update, but before rendering the screen? So if for instance I change an parameter from my animator, the first frame of the new animation will be rendered in that frame cycle? \$\endgroup\$ Oct 17, 2015 at 14:28
  • \$\begingroup\$ @Dbugger: edited to clarify. If you set a new animation parameter before the Internal Animation Update (eg in Update() or a coroutine) you will see the first frame of the new animation in that frame. If you set it in response to an animation event, you will not see the first frame of the new animation until the next frame, because the Internal Animation Update has already run for that object, and doesn't appear to re-run after a change from an animation event. \$\endgroup\$
    – DMGregory
    Oct 17, 2015 at 15:04
  • \$\begingroup\$ Aha! So Animation events are processed post-frame! Thats what I wanetd to know. Thanks! \$\endgroup\$ Oct 17, 2015 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .