Both systems support several different possible approaches to waiting until an animation ends before running some code:
- Both can make use of Animation Events, though they are not really the best tool for specifically waiting until the animation ends because it would be quite tedious to set up an event on every animation.
Mecanim
- Constantly check which state is playing using
Animator.GetCurrentAnimatorStateInfo
.- This requires the state to have an
Exit Time
transition to actually exit the state and requires you to work with hash codes which aren't particularly user-friendly in order to determine which state is which. - You also need to be aware that it won't enter the desired state immediately, so you actually need to wait for it to enter that state then wait for it to exit.
- This requires the state to have an
- Same as above, but check the value of the state’s
normalizedTime
property instead of relying on anExit Time
transition. - Make a
StateMachineBehaviour
script that does essentially the same thing as the previous two options by overridingOnStateExit
orOnStateUpdate
, add it to the state in question.
Animancer
- End Events: when you call
Play
it returns anAnimancerState
. Simply register a delegate to itsEvents.OnEnd
callback. - Coroutines: if you are inside a coroutine you can
yield return
that state to wait for it to end. - Manually: store the state in a field and constantly check its
Time
, orNormalizedTime
. If you're worried about other scripts stopping it, you can also check itsIsPlaying
and/orWeight
depending on exactly what you want to achieve.