Flipping sprite by y axis when rotated past a certain point.,

Hi all! I’m new to unity, and and amateur c# user. I have a submarine sprite that I would like to flip by the y axis when rotated more than 90 degress, and less than -90 degrees so that it won’t be upside down. It’s rotated by mouse movement, which I’ll give the code if necessary. I’m not sure why but this doesn’t seem to work. Any help would be appreciated!

Code:(rot90 is a bool)

    if (transform.rotation.z > 90 & transform.rotation.z >-90)
    {
       rot90 = false;
    }
    if (transform.rotation.z < 90 & transform.rotation.z < -90)
    {
        rot90 = true;
    }

    if (rot90 == true)
    {
        Vector3 scale = transform.localScale;
            scale.y = -22;
        transform.localScale = scale;

        
    }

  if (rot90 == false)
    {
        Vector3 scale = transform.localScale;
        scale.y = 22;
        transform.localScale = scale;
    }

Hi PickleB01, maybe you can try this script?


using UnityEngine;

public class TestRotateObject : MonoBehaviour
{
    private Camera mainCamera;

    private void Start()
    {
        mainCamera = Camera.main;
    }

    private void Update()
    {
        // Update rotation based on the mouse position

        Vector3 aimPosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);

        Vector3 aimDirection = aimPosition - transform.position;
        aimDirection.z = 0;

        Vector3 angles = Vector3.zero;
        angles.z = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg;
        transform.eulerAngles = angles;


        // Flip scale y based on the sign of the aimDirection

        float faceDirection = Mathf.Sign(aimDirection.x);

        Vector3 scale = transform.localScale;
        scale.y = Mathf.Abs(scale.y) * faceDirection;
        transform.localScale = scale;
    }
}