Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

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:
    74
    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:
    863
    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:
    74
    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:
    863
    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:
    74
    Ahhh of course! thanks a lot!
     
  6. Storm4_

    Storm4_

    Joined:
    Mar 2, 2016
    Posts:
    4
    I have the same problem but with normal animations, sprite-based. In my case, the preview DID work without [ExecuteInEditMode], but only until 2019.1. Now with the attribute,
    transfom.position += animator.deltaPosition
    in my OnAnimatorMove works to preview things, but it seems to conflict with record mode, adding the delta position multiple times when I move the transform manually (upd: it does so even if I just edit the keyframe values). If I put
    ApplyBuiltInRootMotion()
    there instead, it does not do anything at all.
     
    Last edited: Apr 4, 2019
  7. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    863
    Possibly the issue is evaluation occuring multiple times in editor without clearing the deltaPosition?

    Also, I can definitely see this having conflicts with recording. You may need to disable it while keyframing root motion.
     
  8. Storm4_

    Storm4_

    Joined:
    Mar 2, 2016
    Posts:
    4
    Turns out it didn't work properly because animator.applyRootMotion was false, even though that checkbox disappears and "Handled by script" is shown in its place if there's a component with OnAnimatorMove attached. Is this a bug? ApplyBuiltInRootMotion still does nothing, but adding deltaPosition now works perfectly even when recording.
     
  9. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    863
    Yeah, that sounds like a UI bug with the animator inspector.