# Flip a 2d character

i want to flip my 2d character but i cant figure out how to do it can someone pls help me im new to programing so im trying to learn it

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

public float moveSpeed;
public float jumpForce;

public KeyCode left;
public KeyCode right;
public KeyCode jump;
public KeyCode shootBullet;

private Rigidbody2D theRB;

public Transform groundCheckPoint;

public bool isGrounded;
public bool Flip;

private Animator anim;

// Start is called before the first frame update
void Start()
{
theRB = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{

if (Input.GetKey(left))
{
theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
if (Flip == false)
{
Flip = true;
}
}
else if (Input.GetKey(right))
{
theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
if (Flip == true)
{
Flip = false;
}
}
else
{
theRB.velocity = new Vector2(0, theRB.velocity.y);
}

if (Input.GetKeyDown(jump) && isGrounded)
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
}

anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
anim.SetBool("Grounded", isGrounded);
}

private void flip()
{
if (Flip == false)
{
transform.Rotate(0f, 0f, 0f);
}

if (Flip == true)
{
transform.Rotate(0f, 180f, 0f);
}
}

}

In need yo use this code SpriteRenderer.flipX to flip your 2d sprite. So go ahead and change it. Do something like this :

private void flip()
{
if (Flip == false)
{
GetComponent<SpriteRenderer>().flipX =false;
}
if (Flip == true)
{
GetComponent<SpriteRenderer>().flipX = true;
}
}

Ok, I edited your script. Now just make sure the transform is facing left at the start and it should work. If you are doing this with a 2D sprite on the other hand, just set the scale of the x value to -1 for left and 1 for right instead of rotate.

Cheers

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpForce;

public KeyCode left;
public KeyCode right;
public KeyCode jump;
public KeyCode shootBullet;

private Rigidbody2D theRB;

public Transform groundCheckPoint;

public bool isGrounded;
public bool facingLeft = true;

private Animator anim;

// Start is called before the first frame update
void Start()
{
theRB = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{

if (Input.GetKey(left))
{
theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
if (facingLeft == false)
{
transform.Rotate(0,180,0);
facingLeft = true;
}
}
else if (Input.GetKey(right))
{
theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
if (facingLeft == true)
{
transform.Rotate(0,-180,0);
facingLeft = false;
}
}
else
{
theRB.velocity = new Vector2(0, theRB.velocity.y);
}

if (Input.GetKeyDown(jump) && isGrounded)
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
}

anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
anim.SetBool("Grounded", isGrounded);
}
}

Hi !

Actually, you do not call your flip() method anywhere in your update loop. You may try the following :

if (Input.GetKey(left))
{
theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
if (!Flip) // same as Flip == false
{
flip();
}
}
else if (Input.GetKey(right))
{
theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
if (Flip) // same as Flip == true
{
flip()
}
}
else
{
theRB.velocity = new Vector2(0, theRB.velocity.y);
}

Your flip() method should look like this.

private void flip()
{
// in both case, we rotate it by 180° around the Y-axis
transform.Rotate(0f, 180f, 0f);
// flip takes its opposite value (if it was flipped, it becomes unflipped and vice versa)
flip != flip;
}

Thanks for all the answers but none of them works like i want them to because i have a shoot point and it wont flip with the character