- Home /
Using Animator, How To Play an Animation Backwards
Hi there!
My question is simple:
I've tried setting the Animator's speed to -1 and failed;
I've tried setting Animation's speed to -1 and found out Animation has no speed parameter;
I've tried to understand where can I grab a reference to my AnimationStates, to no avail;
So... How do I play an animation backwards if everything is set up to work with Animators?
It's so simple using the editor: you open up a State Machine, click a State and set the Speed to -1.
How can I do that via script?
Could you please guide me through the steps needed, since Internet has provided me no help with this? I've read a lot of comments and questions asked in the past 9 years but all the references and solutions given seems outdated... or maybe I'm really really dumb who knows!
Answer by esitoinatteso · Sep 22, 2015 at 01:37 PM
Bumping the question and adding the workaround I've found out as a possible answer.
I still don't know how to play an Animation Backwards using an Animator but I've managed to achieve what I was looking for using this method:
In the State Machine of the Animator, for each State that you'd like to play Backwards duplicate it and set it's animation speed to -1 via editor.
Allow the two states to transition to one another; you need to make a transition from the original to the duplicate and viceversa... Depending on the complexity of the State Machine you might also need conditional parameters to handle those transitions.
Use a State Machine Behaviour script to handle at runtime the behaviour of the State Machine.
In my case, my State Machines were simple so no conditional parameters were involved... like so:
And I've used this script to handle the State Machine ( in simple terms, each time a new state is entered I set the Animator's speed to 0 and wait for another script to fix it back to 1, when a proper event happens at runtime):
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//Freeze
animator.speed = 0;
//Manually set the next state so that everything works and we never miss a beat
animator.Play(animator.GetNextAnimatorStateInfo(0).shortNameHash,layerIndex,0f);
}
It works for me :D
Your answer
Follow this Question
Related Questions
Animation doesn't play 1 Answer
How do I play my animations w. Mechanim? 1 Answer
2D Animation does not start 1 Answer
Animation not be found 1 Answer
Animate an Object by letting it play out in when I press play? 0 Answers