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.