Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting mecanim root motion translation

Discussion in 'Animation' started by romi-fauzi, Apr 18, 2014.

  1. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    Hi guys, so I have a character setup, and the parent object it's actually an empty game object, consist with the character model as a child in it. And all the scripting are on the parent object (moving, jumping, edge grab, etc). The problem is, the edge climbing animation are using root motion to translate across the Y-axes, but because the mecanim character is the child object, the Y position snaps back after climbing animation is done. Is there a way to apply the climbing root motion to the parent object?

    hopefully this makes sense....
     
    Last edited: Apr 20, 2014
  2. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    You can use this: https://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnAnimatorMove.html
    It would be something like:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Example : MonoBehaviour {
    5.     void OnAnimatorMove() {
    6.         Animator animator = GetComponent<Animator>();
    7.         if (animator != null  !Mathf.Approximately(0f,animator.deltaPosition.y)) {
    8.             Vector3 newPosition = transform.parent.position;
    9.             newPosition.y += animator.deltaPosition.y;
    10.             transform.parent.position = newPosition;
    11.         }
    12.     }
    13. }
     
  3. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    Hi aniv,

    Thanks a lot for the help, a little question though, that script should be attached to the child object right(the mecanim character)? not the parent object(emptyGO)...I've tried it but it doesn't working....
     
  4. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    Yes, the object that has Animator, I think you didn't select good root bone in import settings. If there is root motion your object shouldn't snap back after climbing animation is done.
     
  5. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    Hi Aniv,

    I'm using 3dsmax biped FYI, and I've set the root Bip as the hips as well pelvis as the hip, and still not working. Under the import setting animation tab, the only animation that bakes root motion Y is the edge climbing, could this be an issue? And after I added the script above, under Animator, the option apply root motion is handled by script...

    I'm new with mecanim by the way, so I may be doing this the wrong way...
     
  6. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    Yay, it is working now, I've unchecked the root transform position Y for the climbing animation and that seems to be the problem. Now the parent object follows the mecanim character nicely upon climbing..Thanks a lot for the help Aniv!
     
  7. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    Here I post the code I'm using (just a slight modification from aniv snippet, I've added the x root motion into account), in case someone needed the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnimatorMove : MonoBehaviour {
    5.     void OnAnimatorMove() {
    6.         Animator animator = GetComponent<Animator>();
    7.         if (animator) {
    8.             Vector3 newPosition = transform.parent.position;
    9.             newPosition.y += animator.deltaPosition.y;
    10.             newPosition.x += animator.deltaPosition.x;
    11.             transform.parent.position = newPosition;
    12.         }
    13.     }
    14. }
     
    Griffo and jwinn like this.
  8. Gazoid

    Gazoid

    Joined:
    Jan 28, 2015
    Posts:
    1
    Thank you very much both! This saved me quite the headache!
     
    romi-fauzi likes this.