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.

Question How to correct root motion from an animation job?

Discussion in 'Animation' started by SolarianZ, Jul 31, 2022.

  1. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    90
    I'm trying to do some root motion correction in AnimationScriptPlayable.
    In my test code, the expected output is (0.00, 90.00, 0.00), but the actual output has a big fluctuation range, even negative values.
    Is there any way to fix this problem?

    Test Code:
    Code (CSharp):
    1. public struct RotateJob : IAnimationJob
    2. {
    3.     public static readonly Vector3 rotSpeed = new Vector3(0, 90, 0);
    4.  
    5.     public void ProcessRootMotion(AnimationStream stream)
    6.     {
    7.         var angular = rotSpeed * stream.deltaTime;
    8.         stream.angularVelocity += angular;
    9.     }
    10.  
    11.     public void ProcessAnimation(AnimationStream stream)
    12.     {
    13.     }
    14. }
    15.  
    16. [RequireComponent(typeof(Animator))]
    17. public class Test : MonoBehaviour
    18. {
    19.     private Animator _animator;
    20.     private PlayableGraph _graph;
    21.  
    22.     private void OnEnable()
    23.     {
    24.         _animator = GetComponent<Animator>();
    25.         _graph = PlayableGraph.Create();
    26.  
    27.         // clip playable
    28.         var animClipPlayable = AnimationClipPlayable.Create(_graph, null);
    29.  
    30.         // script playable
    31.         var job = new RotateJob();
    32.         var animScriptPlayable = AnimationScriptPlayable.Create(_graph, job);
    33.         animScriptPlayable.AddInput(animClipPlayable, 0, 1f);
    34.  
    35.         // output
    36.         var animPlayableOutput = AnimationPlayableOutput.Create(_graph, "Test", _animator);
    37.         animPlayableOutput.SetSourcePlayable(animScriptPlayable);
    38.  
    39.         _graph.Play();
    40.     }
    41.  
    42.     private Vector3 _lastRot;
    43.  
    44.     private void LateUpdate()
    45.     {
    46.         var currRot = transform.rotation.eulerAngles;
    47.         var deltaRot = currRot - _lastRot;
    48.         var rotSpeed = deltaRot / Time.deltaTime;
    49.         _lastRot = currRot;
    50.  
    51.         Debug.Log($"RotSpeed: {rotSpeed}"); // not equal to RotateJob.rotSpeed
    52.     }
    53.  
    54.     private void OnDisable()
    55.     {
    56.         _graph.Destroy();
    57.     }
    58. }

    Output:
    upload_2022-7-31_19-36-5.png
     

    Attached Files:

unityunity