2

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?

Zedd
  • 167
  • 3
  • 16
  • 1
    [Why “is this possible?” is a poor question](https://softwareengineering.meta.stackexchange.com/questions/7273/). – Dour High Arch Nov 05 '19 at 19:59
  • Thanks, edited the wording. – Zedd Nov 05 '19 at 20:31
  • The problem is not the wording; to give directions we need to know both where you are now and where you are trying to go. Statements like “is this possible?”, “help me”, “how can I implement this?”, and so on do not do this. – Dour High Arch Nov 05 '19 at 22:30
  • I dont understand. I described pretty exact behavior that I aim for in Unity, but dont know how to implement. I also believe such question might be helpful to others in future. I am not trying to be disrespectful, but I dont get your point. – Zedd Nov 05 '19 at 23:03
  • I already provided a link that explains in detail what is wrong with questions like these, why you are not getting any good answers, and exactly what you should be doing. Pay particularly close attention to the paragraph after “So what should be asked instead?” Another good site explains why you should probably not ask a question until [after you have some code to show](https://idownvotedbecau.se/noattempt/). – Dour High Arch Nov 06 '19 at 00:13

1 Answers1

1

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

EduLopez
  • 721
  • 7
  • 18
  • gameObject.animation.clip.length is for old animation system I think? Doing that seem much more complicated in Mecanim. – Zedd Nov 05 '19 at 23:01
  • https://answers.unity.com/questions/692593/get-animation-clip-length-using-animator.html – Immersive Nov 06 '19 at 00:12
  • @Immersive Yes, that manages to do it, but as EvilWarren pointed out in that thread, in doesnt work on the frame you switch the animation (which is the frame you want to get the info) which brings whole set of new problems. – Zedd Nov 06 '19 at 01:01