How to get Animation Clip timeline values from code

Hello,

I’m working on a 2D game on Unity. I need to parse values of transformations (position, rotation etc.) from Animation Clips (*.anim files) and use them in my code.

So I need to know sprite’s position, rotation etc. on every frame. It looks something like here if I open *.anim file in text editor:

  m_Curve:
      - time: 0
        value: {x: 0, y: 0, z: 0}
      - time: 0.0625
        value: {x: 0, y: 2, z: 0}
      - time: 0.125
        value: {x: 4, y: 14, z: 0}

Is there any API how can I get these values for particular frame? I expect something like here (pseudocode):

animator.GetAnimationClip("jumpAnimation").GetValue(parameter, time);

where ‘parameter’ is a name of parameter which is animating with time (like position, rotation etc), ‘time’ - is a frame on timeline or just time, ‘jumpAnimation’ - animation clip name.

Would appreciate if you help me with advices.

Thanks,
Andriy

Well, if you need this at runtime, then there’s no such API when you look at the AnimationClip documentation. The best fit at runtime would be SampleAnimation which applies all transformation for a given time to the given gameobject.

Inside the editor it is possible to access the individual AnimationCurve(s). There’s the AnimationUtility. It has several ways to get hold of the curve data. You most likely are interested in getting a particular curve (for example the curve for the x value of the position). For this there’s the AnimationUtility.GetEditorCurve method. If you have trouble finding the correct property path / name have a look at AnimationUtility.GetCurveBindings.

Of course the “AnimationUtility” is an editor class and as such can only be used inside the Unity editor.