Unity Mecanim - play animation in certain amount of time

3k Views Asked by At

Programming RPG game, I would like to control animation speed in relation to its duration.

For example, character has to cast spell in X seconds. I would like to play the animation state so fast so the animation is finished exactly in X seconds.

-X is determined right before starting an animation.

-I have multiple animations for multiple characters so I would like to avoid doing this via manual calculation and adjustment of animation clip speed in animator

How can I implement this behavior?

1

There are 1 best solutions below

3
On

You could change the speed of the animator component. The thing is that you should update the speed on each iteration of the game loop (or every Y time).

Animation speed may be [0-1] where 1 is default. You'll need to take the length of the animation and do some calculation base on your X and the animation.

Something like: float length = gameObject.animation.clip.length;

newSpeed = ....

https://answers.unity.com/questions/902029/how-to-get-and-change-animation-speed-of-animator.html