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] Fix Animation Snapping during Crossfade with Motion Time

Solution

animationeditor scriptingmecanimanimator controller

Unity 2021.x - Unity 6.3.x

Published 30 days ago

Issue

 When attempting to use Animator.CrossFadeInFixedTime between two animation states whose normalized motion time is controlled programmatically via a shared float parameter, the animation appears to snap instantly to the new state's initial motion time rather than interpolating smoothly. This behavior mimics a direct Animator.Play call, negating the intended blending effect.

Explanation

The observed snapping behavior during an Animator.CrossFadeInFixedTime operation, even when animations are driven by a Motion Time parameter, typically arises when multiple animation states share the same motion time float parameter. Before a crossfade can effectively blend the animations, the shared Motion Time parameter is updated, causing the target animation to immediately jump to the specified time. This preempts the visual interpolation effect.

To resolve this, each animation state that is programmatically controlled via normalized motion time should have its own dedicated float parameterID within the Animator Controller. This ensures that the motion time for the incoming animation can be set independently without affecting the outgoing animation’s parameterID value during the blend.

  1. Modify the Animator Controller by adding a unique float parameter for each animation state requiring manual time control.
  2. In the Inspector for each state, assign the corresponding unique parameterID to the Motion Time field.
  3. Update your script responsible for controlling the animation’s motion time to manage these distinct parameters.
  4. When initiating a crossfade to a new state, identify the corresponding parameterID for that new state and update it accordingly, while allowing the previous state’s parameterID to maintain its value during the transition.

Additional Tips:

  • Use a Dictionary<int, int> to map state hashes to their respective parameterID hashes for efficient lookup.
  • Ensure that Motion Time values are normalized (0 to 1) unless the animation is set to loop and your logic accounts for it.
  • Check the Write Defaults setting on your states, as this can sometimes interfere with parameter-driven transitions.

TL;DR

Ensure each Animator state driven by Motion Time utilizes its own distinct float parameter. Sharing a single parameter across states causes animations to snap rather than blend during crossfades.


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.