Fast-forwarding an animation to its last frame

What’s the best way to fast-forward an animation to its last frame?

The context is straightforward: in a multi-player game with an authoritative server, users logging on should see the last animation played for NPCs, but they shouldn’t see the entire animation being played back. For instance, if a user logs on at a location where combat has just taken place, the user should see the relevant NPCs as dead and static – they shouldn’t see the death animation played back. Thus, in that case, there is a need to fast-forward the animation to its end as part of the logic that sets up the state of existing NPCs. (No, buffering RPC calls is not a solution, the proxy NPCs must call the server for state info as part of their init sequence.)

This only applies to non-cyclic animations, of course: a “sleep” animation shouldn’t be fast-forwarded in this way.

Usually most games have seperate idle animations for such states like dead. The dieing animation transits into the dead-idle. Anyway you can manipulate an animation as you like. Animations lika dead should have its wrapmode set to clampforever to be sure you reach the last frame.

You can set the AnimationState.time to any position in your animation at any time. You could als set the normalizedTime to 1.0 which will be the end of the animation. Note that a finished / stopped animation isn’t sampled. Clampforever will still sample the last keyframe.

You can even sample any state at any time with Animation.Sample.

In case you are doing this via an animator, then you can do this with the CrossFade (or CrossFadeInFixedTime) like so

anim = GetComponent<Animator>();
anim.CrossFade("SomeState", 0f, 0, 1f);