Search Unity

How to properly use OnAnimatorMove() without breaking timeline?

Discussion in 'Timeline' started by tarahugger, Dec 18, 2018.

  1. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    71
    I have some code which i believe was from the 3rd person unity examples, and it implements OnAnimatorMove. In play mode, selecting an object and hitting the play/preview button in the timeline pane works and applies root motion just like if you had the inspector 'apply root motion' box checked. However in edit mode, it doesn't work, the animations play on the spot without root motion.

    The only work around right now for me seems to be to not use OnAnimatorMove(). Is this a bug with timeline? or is there another way to replicate the default functionality.?

    2018.3.0.0f2

    Code (CSharp):
    1.  
    2.     public void OnAnimatorMove()
    3.     {
    4.         if (Animator == null) return;
    5.  
    6.         Animator.ApplyBuiltinRootMotion();
    7.  
    8.         // we implement this function to override the default root motion.
    9.         // this allows us to modify the positional speed before it's applied.
    10.         if (_isGrounded && Time.deltaTime > 0)
    11.         {
    12.             Vector3 v = (Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
    13.  
    14.             // we preserve the existing y part of the current velocity.
    15.             v.y = m_Rigidbody.velocity.y;
    16.             m_Rigidbody.velocity = v;
    17.         }
    18.     }
    19.  
     
    Last edited: Dec 18, 2018
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    That is related to the state machines, which, like physics, are not run in edit mode. Are you trying to define your own root motion? Is it deterministic?
     
  3. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    71
    What i'm trying to do is setup some animation sequences in the timeline, but i would like it so that what i preview and tweak in edit mode is the same as what happens in play mode.

    There are three options for applying root motion, on, off and handled by script. The question was in other words, is there is a way to replicate the 'on' functionality when using 'handled by script' ? I had thought perhaps just calling ApplyBuiltinRootMotion() should do the trick. But it doesn't seem to work when it comes to how the timeline interacts with it.
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    745
    It works if you add the [ExecuteInEditMode] attribute to your script with OnAnimatorMove. Otherwise the OnAnimatorMove doesn't get called.
     
  5. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    71
    Ahhh of course! thanks a lot!