Sprite rotation 2D problem

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

public class movementaxis : MonoBehaviour
{
    public float MovementSpeed = 1;
    public Rigidbody2D r;
    public float JumpForce = 1;
    bool facingright;
    // Start is called before the first frame update
    void Start()
    {
       r = GetComponent<Rigidbody2D>();
        facingright = true;
    }

    // Update is called once per frame
    void Update()
    {
        var movement = Input.GetAxis("Horizontal");
      
        transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;

        if (Input.GetButtonDown("Jump") && Mathf.Abs(r.velocity.y) < 0.001f) {
            r.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);

          if (movement<0 && facingright == true)
            {
                flip();
            }
          if (movement>0 && facingright == false)
            {
                flip();
            }

            void flip()
            {
             
                facingright = !facingright;
                transform.rotation = Quaternion.Euler(0f, 180f, 0f);
            }
        }
      
    }
}

I wrote a code, but i dont get how im supposed to flip my sprite, when

how to make my sprite flip to the left or right. i want to use transform.rotation not scale, because i want to learn what im doing wrong that results in transform.rotation not working

Unless you’re doing something unusual, in 2D game you need to rotate the z. Your Quaternion.Euler has a 180 for the y-axis, not z.

Also just in case by “flip” you mean mirroring flip, the SpriteRenderer has flipX and flipY properties that you can use to horizontally or vertically mirror the sprite. You might want that instead.

1 Like