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?
2 Answers
\$\begingroup\$
\$\endgroup\$
Try this:
int frameNumber = Mathf.FloorToInt(normalizedtimeOfFrame* totalFrames);
\$\begingroup\$
\$\endgroup\$
6
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;
-
\$\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\$– TakCommented Jul 27, 2015 at 16:26
-
\$\begingroup\$ Sorry, I was still editing the question after I posted it. \$\endgroup\$– jgallantCommented 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\$– TakCommented Jul 27, 2015 at 16:43 -
\$\begingroup\$ You have to reference the actual animation instance. Use currentState.clip.length; \$\endgroup\$– jgallantCommented 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\$– TakCommented Jul 27, 2015 at 16:47