1
\$\begingroup\$

I want to know the current time of the played animation using script in Unity3d. I tried using currentState.normalizedTime but this time is not the time of the animation, like it's the time of the runtime I think. Any advice please?

\$\endgroup\$
1
\$\begingroup\$

Try this:

    int frameNumber = Mathf.FloorToInt(normalizedtimeOfFrame* totalFrames);
\$\endgroup\$
1
\$\begingroup\$

You would use AnimationState.time

It can be larger than the length of the animation, so to get the value with this, you would need to modulo with the animation clip length.

float time = currentState.time % currentState.clip.length;

You can also use Normalized Time, but you would have to mulitply the value by the length instead of the animation (AnimationClip.Length)

float time = currentState.normalizedTime * currentState.clip.length;
\$\endgroup\$
  • \$\begingroup\$ But this is the animation time or the run time? Like what if after running my application I didn't play the animation until 5 seconds, would these 5 seconds included when using AnimationState.Time? \$\endgroup\$ – Tak Jul 27 '15 at 16:26
  • \$\begingroup\$ Sorry, I was still editing the question after I posted it. \$\endgroup\$ – jgallant Jul 27 '15 at 16:28
  • \$\begingroup\$ I tried it and it gives this error Error CS0117: 'UnityEngine.Animation' does not contain a definition for 'length' (CS0117) (Assembly-CSharp) \$\endgroup\$ – Tak Jul 27 '15 at 16:43
  • \$\begingroup\$ You have to reference the actual animation instance. Use currentState.clip.length; \$\endgroup\$ – jgallant Jul 27 '15 at 16:44
  • \$\begingroup\$ it is still not working as it gives this error Error CS1061: 'UnityEngine.AnimatorStateInfo' does not contain a definition for 'time' and no extension method 'time' accepting a first argument of type 'UnityEngine.AnimatorStateInfo' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp) \$\endgroup\$ – Tak Jul 27 '15 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.