Switching between unique left/right sprite animations

Hey, I’m brand spanking new to Unity and I’m trying my hand at a basic 2D platformer.

I’ve set up a map in the my player’s Animator component that works just fine for right-facing movement, but because my character is asymmetric, all its left-facing sprites and animations are different. What’s the most efficient way for me to have the Animator check which direction the player is facing at any given frame (and switch between the right- and left-facing animation trees accordingly)? Can I have two motions within one state that switch conditionally?

My first thought was to create a new animation state for every left-facing counterpart of my right-facing animations, then copy all the necessary transitions, but that would obviously create unnecessary clutter that will be a nightmare once I start adding more to the map. I tried looking up some tutorials using Animation Override Controllers, but most of it went over my head. If override controllers are the answer, could someone explain them in terms of my problem? Any help would be greatly appreciated.

create a new animation facing left, then use an int to check your direction, 0 = right, 1 = left, for example

Its normal to have different animations for different facing sides, it seems you have a side scroller, but for example for top down people will have even more animations for facing up and facing down

1 Like

Ok, it took me a few minutes but I figured out how to do it for you. This assumes that you are able to know when your character is moving left or moving right.

(Note, this is changed from what I originally wrote because setting your Animator Controller back to its original will reset all of your animator flags. However, your animator flags are preserved when using AnimatorOverrideControllers)

First you’ll need two AnimatorOverrideControllers. One of them has your right facing animations, the other has your left facing animations.

If your Animator Controller has right facing animations by default, you can just leave your Right AnimatorOverrideController with everything set to none.

public AnimatorOverrideController rightController;
public AnimatorOverrideController leftController;
private Animator animator;
private int playerDirection = 1;

void Start()
{
   animator = GetComponent<Animator>();
}

Drag your controllers into those fields on your script.

Whenever you want to change the animation controller, use the following line.

animator.runtimeAnimatorController = rightController; // To face right
animator.runtimeAnimatorController = leftController; // To face left

However, what you’ll want to do is optimize this so that you’re only changing the controller when your direction changes.

(You can use a boolean value instead if you want, but I personally like to have an integer with 1 = right and -1 = left. This allows me to do things like apply movement in the direction without having to figure out what direction I’m facing beforehand. You can also use enumerators which would be more efficient for something like for a top-down perspective where you have more than two cardinal directions. C# | Enumeration (or enum) - GeeksforGeeks)

You can do that with something like this.

int oldPlayerDirection = playerDirection;

// Change playerDirection after this
playerDirection = 1; // For facing right
playerDirection = -1; // For facing left

if(oldPlayerDirection != playerDirection)
{
   if(playerDirection == 1)
   {
      animator.runtimeAnimatorController = rightController;
   }
   else if(playerDirection == -1)
   {
      animator.runtimeAnimatorController = leftController;
   }
}

Please let me know if any of this is unclear, I would be happy to elaborate further!

EDIT: I’m making a small change to the post because setting the Animator Controller back will reset all of your flags. Instead, you should use two Animator Override Controllers.

1 Like

Thanks so much! You’re the best

1 Like