UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

architecture

[Mecanim] Master Animator Animation Completion

Solution

state machinesevents

Unity 2021.x - Unity 6.3.x

Published Fri, Mar 13

Issue

 Detecting the precise moment a non-looping animation concludes within an Animator Controller is a frequent requirement. This is essential for triggering follow-up actions like enabling AI behavior or instantiating objects immediately after your animation finishes playback.

Explanation

Detect animation completion using a StateMachineBehaviour script or AnimationEvents.

  1. Create a new script inheriting from StateMachineBehaviour.
  2. Override the OnStateExit method to execute logic when the state concludes.
  3. Attach your state machine behavior script to the specific AnimatorState within the Animator Controller.
  4. Alternatively, navigate to the Animation Window and locate your animation.
  5. Move the playhead to the final frame and click Add Event.
  6. Select a public method from a component on the GameObject to trigger upon completion.

Additional Tips

  • Use StateMachineBehaviour for logic decoupled from specific scripts.
  • AnimationEvents require the target script to be on the same GameObject.
  • Ensure Animator transitions do not prematurely trigger OnStateExit during blending.

TL;DR

Animation completion in an Animator can be detected by either implementing a StateMachineBehaviour script on the animation state or by utilizing AnimationEvents directly within the animation clip.


Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.