PlayerFlip

PHOTO EMBED

Tue May 17 2022 18:34:25 GMT+0000 (UTC)

Saved by @BiscuitTinx #c#

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);
    }
}
content_copyCOPY