Flip a Character Sprite in Unity
This story was supposed to be published on July 31, but for some reason became unlisted, I’ll publish it now. Sorry for any confusion.
So yesterday, we scripted our character but discovered he wasn’t facing the right direction when moving left; which is expected because we didn’t tell Unity to flip the sprite.
Enter .flipX! Unity has us covered. All we need to do is just set it to true when the player is facing left. Sounds easy enough. We just need to create a holder for the SpriteRenderer. Remember that the Sprite object is a child of our player so we need to use GetComponentInChildren rather than the usual GetComponent.
And depending on _horizontalInputs output (-1,0,1), we’ll flip the sprite. Mainly, if it’s 1. we don’t flip it, and set it to false, and if it’s -1, then we do flip it, and set it to true. I created a new function called FlipCharacter() to do it.
You can place this line either as the last line of the CheckMovement function, or in Update.
And that’s all you need to do to have your player flip!