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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug What is the relationship between Animator.velocity and AnimationStream.velocity?

Discussion in 'Animation' started by SolarianZ, Apr 19, 2023.

  1. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    215
    Recently, I have been using Playable to create a new animation controller, and I am using AnimationScriptPlayable (ASP for short) to perform some motion corrections. However, I have noticed that in some cases, the corrections made by ASP do not take effect, and the values of Animator.velocity and AnimationStream.velocity are inconsistent.

    For example, I have created a PlayableGraph with the following connections (→ represents the direction of Playable output):

    AnimationClipPlayable → ASP → AnimationPlayableOutput
    upload_2023-4-19_10-1-45.png

    When no AnimationClip is assigned to the AnimationClipPlayable , the motion corrections made by ASP will take effect. However, when an AnimationClip is assigned to the AnimationClipPlayable , the motion corrections made by ASP will not take effect.

    There is more information in these two posts:
     
  2. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    215
    Sample code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Animations;
    3. using UnityEngine.Playables;
    4.  
    5. public struct ModifyMotionJob : IAnimationJob
    6. {
    7.     public void ProcessAnimation(AnimationStream stream) { }
    8.  
    9.     public void ProcessRootMotion(AnimationStream stream)
    10.     {
    11.         stream.velocity = Vector3.zero; // force zero velocity
    12.         //stream.angularVelocity += Vector3.up * 180 * Mathf.Deg2Rad;
    13.     }
    14. }
    15.  
    16. [RequireComponent(typeof(Animator))]
    17. public class Test : MonoBehaviour
    18. {
    19.     public AnimationClip clip;
    20.  
    21.     private Animator _animator;
    22.     private PlayableGraph _graph;
    23.  
    24.  
    25.     private void Awake()
    26.     {
    27.         _animator = GetComponent<Animator>();
    28.         _graph = PlayableGraph.Create("ASP Test");
    29.         _graph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
    30.  
    31.         var acp = AnimationClipPlayable.Create(_graph, clip);
    32.         var asp = AnimationScriptPlayable.Create(_graph, new ModifyMotionJob());
    33.         asp.AddInput(acp, 0, 1f);
    34.         var output = AnimationPlayableOutput.Create(_graph, "Animation", _animator);
    35.         output.SetSourcePlayable(asp);
    36.  
    37.         _graph.Play();
    38.     }
    39.  
    40.     private void OnDestroy()
    41.     {
    42.         _graph.Destroy();
    43.     }
    44.  
    45.     private void OnAnimatorMove()
    46.     {
    47.         //_animator.velocity is not zero
    48.         transform.rotation *= _animator.deltaRotation;
    49.         transform.position += _animator.deltaPosition;
    50.     }
    51. }
     
  3. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    215