using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFlip : MonoBehaviour
{
public float speed;
Rigidbody2D rb;
bool facingRight = true;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float move = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(move * speed, rb.velocity.y);
if(move<0 && facingRight) //if you press the left arrow and your facing right then you will face left
{
flip();
}
else if (move>0 && !facingRight)
{
flip();
}
}
void flip()
{
facingRight = !facingRight; //if facing right is true it will be false and if it is false it will be true
transform.Rotate(0f, 180f, 0f);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter