Jenius1
Jenius1

Reputation: 21

How to play animation forward and backward in Unity?

Hello there :) I'm making a 2d platform game with Unity and got stuck with the crouch animation... I have quite a few frames for crouching, so, when player presses appropriate button character sits down and when the button is released I want this animation to be played backward, so, character stands up. Currently in the animator I created two states with the crouching animation assigned to them. Crouch state speed is 1 and so called un-crouch one is -1 which sort of works fine. My inner perfectionist's question: is there more elegant solution for this kind of cases that allows not to "duplicate" states? Thank you in advance!

Upvotes: 2

Views: 6899

Answers (1)

Ignacio Alorre
Ignacio Alorre

Reputation: 7605

Something you can try is to add in your script a parameter to multiply the speed of the animation. You can call this parameters animDirection or something like this.

I am not sure if you want to speed up the animation as well, but just in case you can do something like this:

float animSpeed = 1;
float animDirection = 1; 

Now you can manipulate in your script this variables, to make the animation move faster and also forward (animDirection = 1) or backward (animDirection = -1)

gameObject.animation["crouch"].speed = animSpeed * animDirection;

So with this you don't need to have two different states.

Upvotes: 2

Related Questions