How can I sample a Mecanim Animation at a specified time?

Hi All -

Does anyone know a way to force a Mecanim animation to a particular time and sample the animation? I have tried using ForceNormalizedTime method on the Animator which does seem to set the time there and the animation will “pick up” from that point the NEXT TIME IT IS UPDATED, but it does not immediately place the animation to that point in time.

In my case I want to loop through (in C# code) various time values of a Mecanim animation and collect some information about the vertex positions at those times.

(I have not used legacy animation system, but documentation looks like it was possible with Animation.Sample in the legacy system).

Thanks in advance!

I just had exactly this problem and Google only brought me here, but I managed to find a solution myself :slight_smile:

So if anyone else is looking for answer to this in the future, here’s how you can get an Animation to instantly change to a specified frame:

First you have to set the animation and time via animator.Play(…) and then you can force the animator to update via animator.Update(float deltaTime).

Example:

int iLayer = 0;
float fNormalizedTime = .5f;

//Get Current State
AnimatorStateInfo aniStateInfo = animator.GetCurrentAnimatorStateInfo(iLayer );

//Set Normalized Time
animator.Play(aniStateInfo.shortNameHash, iLayer,  fNormalizedTime);

//Force Update
animator.Update(0f);