0
\$\begingroup\$

I have an animation that does not loop. It plays for the default duration (n seconds), then stops on the last frame. This is exactly what I want to have happen, but I want it to play for m seconds, not n seconds. I do not know how long n seconds is.

I have to get it exactly to m seconds, because it is a changing variable. So estimating will not work. I need it to match up exactly with another animation. How can I stretch an animation to last an exact number of seconds?

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

An easy way if it is a simple animation (rotating gear, a piston, etc) would be to edit your animation to be exactly 1 second long then use the Animator.speed and set it to 1.0f / wanted_seconds, but that's not usable for complex character animations.

You can figure out the length of the AnimationClip ( http://docs.unity3d.com/ScriptReference/AnimationClip.html ) then set the speed to my_animator.speed = my_animator.animation.clip.length / wanted_seconds.

\$\endgroup\$
  • \$\begingroup\$ Awesome thanks! Any idea how to get the animation from my_animator? When I use my_animator.animation, it says that there is no Animation attached :-/ \$\endgroup\$ – Evorlor Jan 24 '15 at 19:19
  • 1
    \$\begingroup\$ You probably need the animator to be in Play() mode to have an active state first before reading the animation member. Otherwise you'll have to figure out what is the correct clip some other way like create a property in your script and set it there by hand. It depends on what you're trying to do exactly. \$\endgroup\$ – Stephane Hockenhull Jan 24 '15 at 19:40

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.