Problems with sprites and angles

I am having some problems with this code I’ve been trying to write up. I got some hints from other questions, but none pointing to what I need. This is what I’ve wrote up

using UnityEngine;
using UnityEngine.Scripting;

public class PlaneMovement : MonoBehaviour
{
    public float Plane_Speed = 1;
    public float Plane_Rotatation_Speed = 1;
    public float Plane_Audio_Amount = 1;
    public float Bullet_Speed = 1;
    public float Bullet_DMG = 1;
    public float Bullet_Spread = 1;
    public float Plane_Health = 1;
    public Sprite Baron1;
    public Sprite Baron2;
    public Sprite Plane;

    private SpriteRenderer spriteRenderer;

    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>(); // we are accessing the SpriteRenderer that is attached to the Gameobject
    }

    
    void Update()
    {
       // Rotates plane \/
            // Get Angle in Radians
            float AngleRad = Mathf.Atan2(Input.mousePosition.y - transform.position.y, Input.mousePosition.x - transform.position.x);
            // Get Angle in Degrees
            float AngleDeg = (180 / Mathf.PI) * AngleRad;
            // Rotate Object
            this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
       // Rotates Plane /\

       // Swaps Sprite \/

        if (AngleDeg < 180)
        {
            if (AngleDeg > -180)
            {
            // Find a way to change sprite to face right

            // Find a way to change sprite to face right 
            }
        }
        else
        {
            // Find a way to change sprite to face right

            // Find a way to change sprite to face right 
        }

       // Swaps Sprite /\

       // Moves plane forward



    }
}

For the rotation I’m having an issue, because sometimes it doesn’t rotate correctly, my mouse will be on one side, and the plane will look the other way, and sometimes My mouse is in front of the plane, it follows it, but it doesn’t look directly at it.

The other problem I’m having is getting the sprite to change its image when it has reached the middle of the screen. I want it to look to the right when its on the right side of the screen, and left when its on the left side of the screen.

Here’s a picture of my sprite:

I really want to figure this out and would appreciate any help and I can get.
:slight_smile:

To change your image, you can just set the x scale to negative 1, this inverts it along the x axis. However to make it simpler and without needing to do a lot of work… if were rotated too much the plane appears upside down, so just flip the y axis instead. I included code of what it should look like.

Update()
{
    if (transform.rotation.z > 90 && transform.rotation.z < 270)
    {
        transform.localScale = new Vector3(transform.localScale.x,Mathf.Abs(transform.localScale.y)*-1,transform.localScale.z);
    }
    ielse
    {
        transform.localScale = new Vector3(transform.localScale.x,Mathf.Abs(transform.localScale.y),transform.localScale.z);
    }
}

That Should do it, but you may have to change the angles if you have yours set up differently, but that’s not a hard fix.

Now for the other part! I have no idea what your rotation script is doing, so I’m just going to show you how I would do something like this. You want to make the plane look directly at the mouse position, but have a bit of a delay defined under Plane_Rotation_Speed, so here we go:

Vector2 circ = new Vector2 (Input.mousePosition.x - transform.position.x, Input.mousePosition.y - transform.position.y).normalized;
float angle = Mathf.Atan2 (circ.x, circ.y) * 180 / Mathf.PI;

Quaternion newDir = Quaternion.identity;
newDir.eulerAngles = new Vector3(0,angle,0);
transform.rotation = Quaternion.Slerp (transform.rotation, newDir, Time.deltaTime * Plane_Rotation_Speed);

That’s ripped from and modified from one of my own projects which used a 3D flight pattern, but I did my best to see if it’ll work in 2d. If it doesn’t work for you, we’ll continue from there.