Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How does Mecanim actually work?

Discussion in 'Animation' started by SoftwareGeezers, Jan 14, 2015.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    898
    Can someone (Unity) explain how the animation system actually integrates with the rest of Unity? Do the keyframes and blends of animation clips represent absolute world positioning, or local positioning, or a delta to be applied each frame to the object's world position? Does it change depending on if the animation is on a root object or a child object? When is animation applied in the frame life cycle - after Update and FixedUpdate, or between? Or somewhere else like a separate thread?

    My current understanding is that Mecanim animations apply an absolute transform (or other parameter) change, and this takes precedent over other changes in code, suggesting OnAnimate, as it were, happens after the other Updates. This also makes sense as you'd apply changes to Animator in code that it'd then apply, so should be evaluated last.

    These transform changes are local to the child object the animation is applied to, though in absolute world space when the Animator is applied to the root object. Therefore, an animation of a rotation on the spot will fix the object in place if applied to a root gameobject, but will rotate locally if in a child object (and the parent can be moved). This might be changeable in the Animator settings?

    So if you change a transform in code, Animator will override it with an absolute (local) set of values based on the animations applied. If you want to combine Animator animation with code-based, you can use nested children. eg. A gun turret that has recoil as an Animation but who's rotation is handled in code. You can put the animated graphic into a child object and rotate its parent in code. (Yes I know you can animate rotation in the Animator - this is just an example ;))

    How wrong am I?!
     
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Mecanim animation clip are stored in local space for generic rig.
    Humanoid rig are different because animation curve are in fact muscle value that goes from ~ -1 to 1. Which are converted by the retargeter to a local transfrom pose
    Mecanim does write the animated value in local space.

    The evaluation pipeline look like this:

    MonoBehaviour.FixedUpdate()
    Animator.FixedFKUpdate() <-- Evaluate the statemachine and animation clip pose
    Animation.FixedUpdate();
    Animator.FixedRetargetingAndIKUpdate() <-- Write values to unity transforms

    Physics.Update()
    Physics2D.Update()

    MonoBehaviour.Update()
    Animator.FKUpdate() <-- Evaluate the statemachine and animation clip pose
    Animation.Update();
    Animator.RetargetingAndIKUpdate() <-- Write values to unity transforms

    MonoBehaviour.LateUpdate()

    So you can see that the animator is always evaluated after the animation legacy component which is why it does always overwrite any values written by the animation component.

    Like you said you can mix them in the same hierarchy if they are animating different properties.
    If you want to override an animated properties value by script you have to do it in LateUpdate().

    You nail it!
     
  3. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    898
    One last important plot point - it appears that changes within a state only exist as long as that state is active, and the moment that state is left, it's influence ends. So in my case, I have a state that disables the eye sprites, but the moment that state exits, the eyes become visible again because they are on the root character the Animator is applied to.

    As such, one can't create a state and then build on it from other states. You can't string together states as I was trying, State 1 (turn off eyes) -> State 2 (change hair) -> State 3 (animate with turned-off eyes and changed hair from previous states).

    Correct, or have I missed something?
     
  4. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    In 5.0 we did add a new feature that allow you to do that.

    On each AnimatorState there is an option call Write defaults which is set to true by default. If you uncheck this options in your state 2 change hair, and the clip in this state doesn't write any value to turn off eyes then they should stay off.

    In 4.x there is no way to do this.
     
  5. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    898
    Cool. Thanks.
     
  6. saddam751

    saddam751

    Joined:
    Nov 6, 2013
    Posts:
    41