- Home /
Dynamic Mecanim animation speed/time
Hi,
I'm currently working on implementing a weapons system, that features many different guns with different reload times and rates of fire. I need to shorten or lengthen character animations based to these weapon stats. Is there any way to dynamically alter the speed of animations from code in mecanim, or maybe just stop an animation state?
The workaround in this doesn't apply in this case as far as I can tell. Any ideas?
Answer by TonyLi · Jun 17, 2013 at 01:27 PM
The forum thread covers a couple different ideas. I'm not sure which one doesn't apply. Here are some options to consider:
Set playback of the entire Animator using Animator.speed. Note that this overrides the speeds of all states in your controller. Probably not your best bet.
Set up different states using the same animation clip but with different speeds. Transition to the right state based on the current weapon.
Use a blend tree to blend between different versions of the same animation clip that are set to different speeds. This is a common solution to this problem, as you have very fine control of the speed.
(Undocumented and unsupported:) Dig into the internals of the controller: http://answers.unity3d.com/questions/418854/getting-a-list-of-mecanim-states-in-animator.html
And is it possible to stop an animation and play it from the beginning?
How would go about solving this with a blend tree?
Here's one way:
Create a float parameter named reloadSpeed.
Create a blend tree based on reloadSpeed.
Add two motion fields. Assign the reload animation to both motion fields. Set the speed of the first one to the $$anonymous$$imum reload speed, and the speed of the second one to the maximum reload speed.
When you change weapons, call Animator.SetFloat("reloadSpeed", X), where X=0 is the slowest speed and X=1 is the fastest. X=0.5 would be the speed right in between.
And is it possible to stop an animation and play it from the beginning?
Create a transition to a null state and back. Trigger the transition on a boolean parameter called "restart" or something like that.
I'm having trouble with restarting an animation. I need to restart a firing animation if the firing animation is already playing, and the player tries to fire again.
I have made a transition to a null state on a isRestarting boolean and an instant transition back to the firing animation, but it's not working. I set the isRestarting boolean on an event if we are already in the firing state, but that check does not always return true, even if it is in the firing state. I guess that check can only be made in Update. I reset isRestarting in Update if we are in the null state, and I reset isFiring if we are in the firing state.
How should this be implemented?
Your answer
Follow this Question
Related Questions
Mecanim issue: Twitching when blending into blendtrees 1 Answer
Animator System doesnt work! 1 Answer
Cannot restart animation in Mecanim 3 Answers
Changing how my character looks depending on his current lifes. 2 Answers
Mecanim animation issue 1 Answer