Here, I will demonstrate how to flip a sprite across its X axis in Unity, using the sprite renderer’s flip X property.
Below, we have our character’s sprite facing to the right. This was how the sprite was originally drawn and it should look this way when imported into a project:
Now if we move to the right, there are no problems. He moves and animates as he should when he moves to the right:
What happens when we want to move to the left? Do we need to have an artist redraw the sprite and associated animations for the left movement? The answer is no. Unity provides a very easy way to flip the sprite. All we need to do is access the sprite renderer in our code and set the flipX property to true. This will give us a nice left movement and can be done with the following code:
Here “move” is obtained with Unity’s Input.GetAxisRaw(“Horizontal”) command. If the left movement key is pressed, move will be -1. If the right movement key is pressed, the move will be 1. So if it’s -1 that means we are moving to the left and should flip the sprite.
The code will give us the following proper behavior:
That’s all there is to it when it comes to flipping a sprite with it’s sprite renderer.