how to flip sprite in unity

hello,

in todays video I am going to show you how to flip sprite in unity 2d

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

public class FlipTut : MonoBehaviour
{

float moveInput;
float scaleX;

public float moveSpeed;
Rigidbody2D rb2d;

private void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
    scaleX = transform.localScale.x;
}

public void Update()
{
    moveInput = Input.GetAxisRaw("Horizontal");
}

private void FixedUpdate()
{

        Move();
}

public void Flip()
{
    if (moveInput > 0)
    {
        transform.localScale = new Vector3(scaleX, transform.localScale.y, transform.localScale.z);
    }
    if (moveInput < 0)
    {
        transform.localScale = new Vector3((-1) * scaleX, transform.localScale.y, transform.localScale.z);
    }

}
public void Move()
{
    Flip();
    rb2d.velocity = new Vector2(moveInput * moveSpeed, rb2d.velocity.y);
}

}


Leave a Reply

Your email address will not be published. Required fields are marked *