- Home /
Quickly switch sprite animation
I have a character for a 2D platformer with many different animations, such as idle/running/jumping animations for left and right. These are animations made from a spritesheet (nothing has been done inside of Unity). Currently I'm akwardly switching animation through the Animator window and turning on and off parameters. The problem is that not only the Animator window is very messy, but my script as well. So I'm looking for a better way to switch between animations while using sprite sheet animations. Down below a code preview of how I currently switch between animations:
// Check and set animations for action when facing right.
if(player.Horizontal > 0)
{
if(player.isGrounded)
{
// Running.
playerAnimator.SetBool("Running_R", true);
playerAnimator.SetBool("Idle_R", false);
playerAnimator.SetBool("Jump_R", false);
}
else
{
// Jumping while running.
playerAnimator.SetBool("Jump_R", true);
playerAnimator.SetBool("Running_R", false);
playerAnimator.SetBool("Idle_R", false);
}
}
if(player.Horizontal == 0)
{
if(player.isGrounded)
{
// Idle.
playerAnimator.SetBool("Idle_R", true);
playerAnimator.SetBool("Running_R", false);
playerAnimator.SetBool("Jump_R", false);
}
else
{
// Jumping while idle.
playerAnimator.SetBool("Jump_R", true);
playerAnimator.SetBool("Idle_R", false);
}
}
So my question is: How do I switch spritesheet animations more easily and more efficient ?
your player will have three animation and transition
Use playerAnimator.SetInteger("AnimationNumber",0)
- to play default or idle playerAnimator.SetInteger("AnimationNumber",1)
- Running playerAnimator.SetInteger("AnimationNumber",2)
- Jump
give all transitions so you can switch between any of them.
hope this will make your work a bit easy
Answer by Pearbook · Nov 27, 2015 at 10:25 AM
I actually forgot about this question I asked. I did find a solution a while back, but I'll share it anyway:
using UnityEngine;
using System.Collections;
public class Animation : MonoBehaviour
{
public enum animations
{
Idle,
Walk,
Jump,
Fall
}
public animations currentAnimation; // Animation that is currently playing.
public Animator Animator; // The animator you want to use.
void Update ()
{
if(/* Idle */)
TransitionTo(animations.Idle, "NameOfIdleAnimation");
if(/* Moving */)
TransitionTo(animations.Idle, "NameOfWalkingAnimation");
}
// Set the animation enum, Name of the animation you want to play.
void TransitionTo(animation anim, string name)
{
// Check if the animation is already playing or not/
if (currentAnimation != anim)
{
Animator.Play(name);
currentAnimation = anim;
}
}
}
The nice thing about it is that because I use Pixel art graphics I don't need smooth transitions and this solution doesn't require a transition in the Animator. Just an Animator with the animations in it (with the right names of course).
Answer by alexi123454 · Jul 21, 2015 at 02:02 PM
Rather than using Bools for everything in your animator, you could use Triggers instead. They another parameter type, but they're more suited for rapid changes in state.
http://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html
Instead of changing a number of bool for each thing, your code would look something like this:
// Check and set animations for action when facing right.
if(player.Horizontal > 0)
{
if(player.isGrounded)
{
// Running.
playerAnimator.SetTrigger("Running_R");
}
else
{
// Jumping while running.
playerAnimator.SetTrigger("Jump_R");
}
}
if(player.Horizontal == 0)
{
if(player.isGrounded)
{
// Idle.
playerAnimator.SetTrigger("Idle_R");
}
else
{
// Jumping while idle.
playerAnimator.SetTrigger("Jump_R");
}
}
You would have to change a few little things around in your transitions, but it should be cleaner in the long run.
This is code wise a lot cleaner, thanks. I was hoping for a way to clean up my transitions in the Animator view, which currently looks this and hasn't even got all the animations in yet:
Ins$$anonymous$$d of having a transition from each thing, you could replace a lot of them by making them transition from the "Any State" bubble. If you call a transition between the Any State and a state, it will do it automatically regardless of what state you're currently in. That should make the animator view a little cleaner :P
Thanks, the "Any State" is something I was looking for. But I haven't figured out how it works, when I make a transition it keeps repeating that transitions. So my animation only plays 1 or 2 frames and then repeats itself.
Are you still using the Boolean parameters ins$$anonymous$$d of the triggers? If you still have boolean parameters set up, it's going to see that the boolean is still set and keep doing the transition.
Say you have "Right" playing. The animator things "oh hey, right should be playing, let's transition to that from any state". Then, once the transition finishes and the program is playing Right, it thinks again "Hey, the transition is finished! But wait a second, right should still be playing. Let's transition to it from any state again". Since Right is a state, and it can transfer to Right from any state, it can transfer to itself, which would cause the the 1-2 frame repeating animations you're getting.
Thanks for your quick reply, but I already got rid of the booleans but for some reason the triggers have the same effect and some times trigger won't turn of after being used. This is how it looks right now:
Your answer
![](https://koobas.hobune.stream/wayback/20220611232229im_/https://answers.unity.com/themes/thub/images/avi.jpg)