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\$

2 Answers 2

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\$
6
  • \$\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
    Commented Jul 27, 2015 at 16:26
  • \$\begingroup\$ Sorry, I was still editing the question after I posted it. \$\endgroup\$
    – jgallant
    Commented Jul 27, 2015 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
    Commented Jul 27, 2015 at 16:43
  • \$\begingroup\$ You have to reference the actual animation instance. Use currentState.clip.length; \$\endgroup\$
    – jgallant
    Commented Jul 27, 2015 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
    Commented Jul 27, 2015 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .