How to flip a sprite in Unity

There are many cases when you need to flip a sprite in your game. It might be when the player starts walking in the opposite direction or if the player is falling upside down. You can always rotate the sprite using Unity transform to flip the sprite. Rotation affects the child gameobjects if any and may cause them not to render properly.

Unity gives up another option to flip the sprite using sprite renderer. In this tutorial, we will see how to flip the sprite using sprite renderer in code.

Flip sprite using sprite Renderer

Sprite renderer component Unity

I am assuming you have a script attached to your sprite. If not, create a new script.

You need to access the sprite renderer as shown in the script below and enable the flip parameter.

Free Unity asset banner

using UnityEngine;

public class cat_control : MonoBehaviour
{

    
SpriteRenderer spi
    void Start()
    {
          
spi=GetComponent<SpriteRenderer>();
    }

   
    void Update()
    {
        if (Condition1)
        {
          spi.flipX=true;
        }
        if (Condition2)

        {
         spi.flipY=true;
        }
        
    }
    
    
}

FlipX flips the sprite along the X axis and FlipY flips the sprite along the Y axis. If you want to bring the sprite back you can just set it to false.

If you need to flip the sprite permanently, you can just check the FlipX or the FlipY checkbox on the SpriteRenderer.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.