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

Question Problem when I using AnimationTrack and My Custom TransformTrack

Discussion in 'Timeline' started by sans108, Jan 12, 2022.

  1. sans108

    sans108

    Joined:
    Apr 1, 2021
    Posts:
    13
    Hi,
    I was create a custom timeline track which can modify transform like position and rotation, When I used it indivisually, It worked perfectly.

    upload_2022-1-12_15-47-22.gif

    But if I add an animation by using "AnimationTrack", "TransformTrack" will not work anymore.

    upload_2022-1-12_15-48-51.gif

    BTW, AnimationClip contains a root motion, if I remove it, "TransformTrack" could work again. I wonder if I can find a way to disable the root motion of "AnimationTrack" can solve the problem.
    upload_2022-1-12_15-50-25.png

    Here is my code:

    Code (CSharp):
    1. public class TransformMixerBehaviour : PlayableBehaviour
    2.     {
    3.         public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    4.         {
    5.             var trans = playerData as Transform;
    6.  
    7.             if (!trans)
    8.                 return;
    9.  
    10.             bool setData = false;
    11.             Vector3 position = Vector3.zero;
    12.             Vector3 eulerAngles = Vector3.zero;
    13.  
    14.             int inputCount = playable.GetInputCount();
    15.             for (int i = 0; i < inputCount; i++)
    16.             {
    17.                 float inputWeight = playable.GetInputWeight(i);
    18.                 if (inputWeight > 0f)
    19.                 {
    20.                     setData = true;
    21.  
    22.                     var inputPlayable = (ScriptPlayable<TransformBehaviour>)playable.GetInput(i);
    23.                     var transformBehaviour = inputPlayable.GetBehaviour();
    24.  
    25.                     position += transformBehaviour.position * inputWeight;
    26.                     eulerAngles += transformBehaviour.eulerAngles * inputWeight;
    27.                 }
    28.             }
    29.  
    30.             if (setData)
    31.             {
    32.                 trans.localPosition = position;
    33.                 trans.localEulerAngles = eulerAngles;
    34.             }
    35.         }
    36.     }
    How can I make the TransformTrack works and keep the root motion of AnimationClip

    If anyone know the answer, please please help me.
     
    Last edited: Jan 14, 2022
  2. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    366
    The problem you have is that both tracks update the same transform every frame.
    If you want both tracks to work simulataniuosly you would have to modifiy both, that they dont do it!
     
  3. lonely2015

    lonely2015

    Joined:
    Aug 16, 2017
    Posts:
    17
    man i just find my trouble is same with you.
     
  4. sans108

    sans108

    Joined:
    Apr 1, 2021
    Posts:
    13
    Hi guys, I found a way to solve this.
    1.add a MonoBehaviour script to the animated Gameobject
    2.Here is the script (It's little hacky..):

    Code (CSharp):
    1.     [ExecuteInEditMode]
    2.     public class RootMotionController : MonoBehaviour
    3.     {
    4.         private Animator _anim;
    5.  
    6.         private void Start()
    7.         {
    8.         }
    9.  
    10.         //This function will block the root motion
    11.         private void OnAnimatorMove()
    12.         {
    13.             if (null == _anim)
    14.             {
    15.                 _anim = GetComponent<Animator>();
    16.             }
    17.  
    18.             if (null != _anim)
    19.             {
    20.                 //if you want use root motion here
    21.                 _anim.ApplyBuiltinRootMotion();
    22.             }
    23.         }
    24.     }
    Now you can control the transform but keep the animation root motion.

    But I still can't get over it why Unity doesn't add a toggle "Enabled Root Motion" on the Animator Component.
     
  5. lonely2015

    lonely2015

    Joined:
    Aug 16, 2017
    Posts:
    17
    Hi , I want this animation to play in timeline like an animator .




    Here is my anim clip.
     

    Attached Files:

    Last edited: Feb 23, 2022