- Home /
Play whole animation over specified time via script
Hello. ;)
I don't just want to change the speed of animation. I want to have control of animation state in seconds - e.g. If I put 0.5 in the inspector the animation will play for 0.5s displaying all frames over that time.
For example - character has attackSpeed variable, when its set to 0.5f I want the attack animation play for exactly 0,5 seconds (no matter how many frames are there, all frames must be displayed over that time).
I'm not sure if there is a proper way to do it, or should I just figure out some workaround like calculating frames per second with animation speed? Any ideas?
I dont really think thats physically possible? You want to play, lets say a 3 second animation, in 0.5 seconds, but you dont want the animations speed to be any faster? (altering the animations speed variable does exactly what your describing) - if you calculate how long it takes for your player to swing and actually hit your enemy, you can add (or subtract) that time from the animation speed, which is probably the easiest way to get to as close as what your trying to achieve.
Answer by xpavelos · Jun 14, 2016 at 11:18 PM
Thanks for your comment Dibbie. But that's not what I was looking for.
I found an easy way to achieve it. First, I must assume that animation must be exactly 1 second long. And when I set attack speed to 0.5, which is half a second then animation speed must be doubled - in this case must equal 2 (and now animation also lasts half a second). And if I set attackSpeed to 2 seconds, animation speed must be slowed down to 0.5. This is how I solved it:
public void SetAttackSpeed(float value)
{
attackSpeed = value;
upperAnim.SetFloat("attackSpeed", 1/value);
}
The "attackSpeed" parameter in upperAnim is used as a multiplier parameter.
Your answer
![](https://koobas.hobune.stream/wayback/20220612082930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animation Stop and Stay 1 Answer
Clamp animation to a certain amount of time? 1 Answer
animation to play faster over time 2 Answers
[Solved] Reverse animation help 2 Answers
Mecanim - changing animation clip speed, through script? 4 Answers