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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Get the position of an animationClip at a given time

Discussion in 'Animation' started by greddu9t, Nov 26, 2019.

  1. greddu9t

    greddu9t

    Joined:
    Sep 24, 2019
    Posts:
    7
    Hello,

    I would like to retrieve the Transform/Position of an animationClip at a given time/frame.
    Is that possible ? If not, do you have any possible alternative ?

    Thank you
     
  2. greddu9t

    greddu9t

    Joined:
    Sep 24, 2019
    Posts:
    7
    I've found the solution, it takes a lot of process, but as it's done only in Editor it doesn't affect the game. I am getting the curve of the animationclip and I store 10 position, each corresponding to 10% of the animation.

    For this I need to compute each x, y and z axis curves :

    Code (CSharp):
    1. void OnValidate()
    2. {
    3.     //Check if we are in the Editor, and the animationClip is getting change
    4.     if (Application.isEditor && !Application.isPlaying && previousClip != animationClip)
    5.     {
    6.         previousClip = animationClip;
    7.         m_checkPoints = new Vector3[10];
    8.         int index = 0;
    9.  
    10.         //Taking the curves form the animation clip
    11.         AnimationCurve curve;
    12.         var curveBindings = AnimationUtility.GetCurveBindings(animationClip);
    13.  
    14.         //Going into each curves
    15.         foreach (var curveBinding in curveBindings)
    16.         {
    17.             //Checking curve's name, as we only need those about the position
    18.             if (curveBinding.propertyName == "m_LocalPosition.x")
    19.                 index = 0;
    20.             else if (curveBinding.propertyName == "m_LocalPosition.y")
    21.                 index = 1;
    22.             else if (curveBinding.propertyName == "m_LocalPosition.z")
    23.                 index = 2;
    24.             else
    25.                 continue;
    26.  
    27.             //Get the curveform the editor
    28.             curve = AnimationUtility.GetEditorCurve(animationClip, curveBinding);
    29.  
    30.                     for (float i = 1; i < 11; i++)
    31.                     {
    32.                         //Evaluate the curves at a given time
    33.                         if(i == 1)
    34.                             m_checkPoints[(int)i - 1][index] = curve.Evaluate(0);
    35.                         else
    36.                             m_checkPoints[(int)i - 1][index] = curve.Evaluate(animationClip.length * (i / 10));
    37.                     }
    38.                 }
    39.         }
    40.     }
    41. }
     
    Last edited: Nov 28, 2019