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

Is it possible to use root motion on concrete animations only?

Discussion in 'Animation' started by don-Klimdigo, Sep 15, 2021.

  1. don-Klimdigo

    don-Klimdigo

    Joined:
    Nov 28, 2017
    Posts:
    1
    Hi guys! I'm using Spine for my 2d platformer character animations. I'm going to use Root Motion for animations like attacks and roll, but i have scriptable physic based movement and jumps. Is it possible to use root motion on concrete animations only, or i should make all character actions based on it?
     
  2. shimoop

    shimoop

    Joined:
    Nov 21, 2014
    Posts:
    2
    I think you can use OnAnimatorMove() callback to manually calculate transform like this:
    Code (CSharp):
    1. public class Example : MonoBehaviour
    2. {
    3.     ...
    4.  
    5.     void OnAnimatorMove()
    6.     {
    7.         if(needRootMotion)
    8.             {
    9.                 transform.rotation *= _Animator.deltaRotation;
    10.                 transform.position += _Animator.deltaPosition;
    11.             }
    12.     }
    13. }
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnAnimatorMove.html
     
  3. DSivtsov

    DSivtsov

    Joined:
    Feb 20, 2019
    Posts:
    151
    Are Existed some variants,
    One - you put this script to GameObject with Animator (after that the field "Apply Root Animation" automatically are switched to "Handled by Script")
    Code (CSharp):
    1. public class ApplyRootMotionToClip : MonoBehaviour
    2. {
    3.     Animator anim;
    4.     int hashAnimTurn;
    5.  
    6.     private void Awake()
    7.     {
    8.         anim = GetComponent<Animator>();
    9.         hashAnimTurn = Animator.StringToHash("TurnRight");
    10.     }
    11.  
    12.     void OnAnimatorMove()
    13.     {
    14.         AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
    15.  
    16.         // APPLY DEFAULT ROOT MOTION, ONLY WHEN IN THESE ANIMATION STATES
    17.         if (stateInfo.shortNameHash == hashAnimTurn)
    18.         {
    19.             anim.ApplyBuiltinRootMotion();
    20.         }
    21.     }
    22. }
    I use other, It's also attached to Animator, but It is controlled through variables from my State Machine
    Code (CSharp):
    1.         void OnAnimatorMove()
    2.         {
    3.             if (_playRootMotion)
    4.             {
    5.                 _playerAnimator.ApplyBuiltinRootMotion();
    6.             }
    7.         }