How To Make This Script More Readable

As the title suggests I would like some feedback as how to make this script better and more readable. All it does is smoothly rotates an object around its axis at 90 degrees when you input the A and D keys. It works fine, which is great, but it looks ugly. Any feedback would be great! Thank you.

The Script:

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

public class move : MonoBehaviour
{
    Quaternion targetAngle90 = Quaternion.Euler(0, 0, -90);
    Quaternion targetAngle180 = Quaternion.Euler(0, 0, -180);
    Quaternion targetAngle270 = Quaternion.Euler(0, 0, -270);
    Quaternion targetAngle360 = Quaternion.Euler(0, 0, -360);
    Quaternion targetAngle0 = Quaternion.Euler(0, 0, 0);

    public Quaternion currentAngle;


    private void Start()
    {
        currentAngle = targetAngle0;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.D))
        {
            DecreaseCurrentAngle();
           
        }
        else if (Input.GetKeyDown(KeyCode.A))
        {
            IncreaseCurrentAngle();

        }


        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, currentAngle, .2f);
    }

    void DecreaseCurrentAngle()
    {
        if (currentAngle.eulerAngles.z == targetAngle0.eulerAngles.z)
        {
            currentAngle = targetAngle90;
        }
        else if (currentAngle.eulerAngles.z == targetAngle90.eulerAngles.z)
        {
            currentAngle = targetAngle180;
        }
        else if (currentAngle.eulerAngles.z == targetAngle180.eulerAngles.z)
        {
            currentAngle = targetAngle270;
        }
        else if (currentAngle.eulerAngles.z == targetAngle270.eulerAngles.z)
        {
            currentAngle = targetAngle0;
        }
    }

        void IncreaseCurrentAngle()
        {
            if (currentAngle.eulerAngles.z == targetAngle0.eulerAngles.z)
            {
                currentAngle = targetAngle270;
            }
            else if (currentAngle.eulerAngles.z == targetAngle90.eulerAngles.z)
            {
                currentAngle = targetAngle0;
            }
            else if (currentAngle.eulerAngles.z == targetAngle180.eulerAngles.z)
            {
                currentAngle = targetAngle90;
            }
            else if (currentAngle.eulerAngles.z == targetAngle270.eulerAngles.z)
            {
                currentAngle = targetAngle180;
            }

        }
}

I don’t know if it’s going to be more readable, but here’s what you could do:

Replace all target angle variables with one array containing them. (I’ll call it “array” in my description)
Create an int variable for your target angle. (“currentAngleInt”)

When pressing a button call a method (call it “ChangeAngle” for example) that receives an int parameter (lets call it “direction”).
When pressing D pass 1 into that method.
When pressing A pass -1 into that method.

Inside of the ChangeAngle method:
Add the “direction” parameter to your current angle variable.
Check if it’s lower than zero
if true - set it to be equal to array.Length - 1. This is going to be the last element index.
if not - check if it’s equal/greater than “array.Length”
if it is - set it to 0.

In your update method replace “currentAngle” with “array[currentAngleInt]” and you should be good to go.

Let me know if my explanation wasn’t clear.

Hi @jpet1991 ,

You could certainly improve that code. For starters comparing floats is not really wise as it can lead to inaccuracies or your comparison might not give a match when you expect it to.

To rotate the values around 0-360 range you can use Mathf.Repeat to avoid chaining if statements.
Repeat will limit the first given parameter value within 0 and the 2nd value (i.e. that 360.)

(This is just pseudo code/critical parts.)

// Turning left
newAngle = Mathf.Repeat(newAngle - 90f, 360f);

// Turning right
newAngle = Mathf.Repeat(newAngle + 90f, 360f);

Now you got a correct angle without any ifs or such.

Then you can just set the Transform’s rotation angle in Eulers.

transform.eulerAngles = new Vector3(0f, newAngle, 0f)

And if you want to do it over time, just make a it incremental using setting the value in Update or a Coroutine and interpolate it over time.