- Home /
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 float groundCheckRadius;
public LayerMask whatIsGrounded;
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()
{
isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGrounded);
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);
}
}
}
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
make the shoot point a child of the character, then when you rotate, it will as well
I don't $$anonymous$$d taking a look at your project if you can't figure it out, if you want to post a link to the zip project here
okay it works now but the shoot point dont rotate so im shooting my self
Answer by highpockets · Apr 12, 2019 at 08:33 AM
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 float groundCheckRadius;
public LayerMask whatIsGrounded;
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()
{
isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGrounded);
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);
}
}
Answer by shgdi · Apr 12, 2019 at 08:24 AM
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;
}
}
Answer by Kaihyo · Apr 12, 2019 at 08:45 AM
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612195327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip 2D player in x-axis to face movement direction 5 Answers
Flip 2D Character with Keys without moving 2 Answers
2D Character Flip 2 Answers
2D example: Flip character without moving 0 Answers
How to do a sprite flip 1 Answer