How to flip a character in Unity 2D
Code Example - How to flip a character in Unity 2D
//Checks if the left and right keys are pressed and flips the player accordingly
//Make sure the player scale on the x axis is 1
//Checks if the right key is pressed
if (moveHorizontal > 0)
{
rb2D.transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
}
//Checks if Left Key is pressed
else if (moveHorizontal < 0)
{
rb2D.transform.localScale = new Vector3(-1, transform.localScale.y, transform.localScale.z);
}
how to flip character in unity 2d
Put this in Update() =
//Flip the player's localScale.x
if the move speed is greater than .01 or less than -.01
if (targetVelocity.x < -.01)
{
transform.eulerAngles = new Vector3(0, 180, 0); // Flipped
}
else if (targetVelocity.x > .01)
{
transform.eulerAngles = new Vector3(0, 0, 0); // Normal
}