dmkanerer
dmkanerer

Reputation: 141

How can I make an animation in Unity play just once and then switch back to another animation

I am making a 2d game in Unity, and for the main character I have created 4 animations; up, down, left, and right. To do this I just made the animation and trigger them with a line of code like this:

anim.SetBool ("movingUp", true);

which would happen when the up key is pressed. This type of animation is easy to deal with because it is not based on time, but how long the player wants the character to be in that state. I am now trying to add an animation that is triggered when the character collides with another game object. I am not sure how to make it so the animation will play just one time and then go back to the other animation. Is there a way to trigger an animation to play just once in Unity?

Upvotes: 1

Views: 8729

Answers (3)

Ken Stephenson
Ken Stephenson

Reputation: 21

Select the animation in Assets. Have you unchecked "Loop Time"? This is what solved the problem for me.

Upvotes: 2

Ethan K-B
Ethan K-B

Reputation: 135

MAke an animator controller and add the 4 animations into it. then make a transition between them with the Boolean. Are you doing this as an automatic animation without triggering it?

Upvotes: 0

agiro
agiro

Reputation: 2080

Generally speaking you don't have to code your own controller, Unity already has that one and here is the link to official Unity tutorials how to use it. What you need is to fire off a trigger just like you did upon collision and check has exit time on that animation in the controller. After that you just guide the state back to the animation you want it to go back to. Again, accurate and thorough tutorials are found online on the term Unity Mecanim how to or Animations in Unity or something like that.

Upvotes: 1

Related Questions