How to snap rotate my cube to the nearest 90 degree when i let got of my thumb stick?

At the moment the rotation is nice but id like the cube to snap back in to place (to the nearest 90 degrees) when i let go of the thumb stick, i tried with it the, If eulerAngles are less than or grater than but it got confused after a while i’m learning … and i feel this way eventually the number work against each other in the end because i want it spinning on two Axis really…

public class CubeRotation : MonoBehaviour
{
    public JoyStick virtualJoystick;
    public float RoatitionSpeed = 20f;

    private static int SnapRot;
    private float LeftoOrRight;
    private float UpOrDown;
  
    private Vector3 CurrentRotation;
  
    private void Update()
    {
        CurrentRotation.z = 0;
         UpOrDown = virtualJoystick.Vertical();
         LeftoOrRight = virtualJoystick.Horizontal();
  
        // thunbstick rotation....
        if (LeftoOrRight > 0.93f)
        {
            transform.Rotate(Vector3.up * -RoatitionSpeed * Time.deltaTime, Space.World);
        }
        else if(LeftoOrRight < -0.93f)
        {
            transform.Rotate(Vector3.up * RoatitionSpeed * Time.deltaTime, Space.World);
        }
   
        if(UpOrDown > 0.93f)
        {
            transform.Rotate(Vector3.right * RoatitionSpeed * Time.deltaTime, Space.World);
        }
        else if (UpOrDown < -0.93f)
        {
            transform.Rotate(Vector3.right * -RoatitionSpeed * Time.deltaTime, Space.World);
        }
        // snap rotation
        SnapRot = virtualJoystick.Snaprot;
        CurrentRotation = transform.rotation.eulerAngles;

        if (SnapRot == 1)
        {
            if(CurrentRotation.x >= 0.1f && CurrentRotation.x <= 45 )
            {
                transform.eulerAngles = new Vector3(0,0,0);
            }
            else if(CurrentRotation.x >= 45.1f && CurrentRotation.x <= 90)
            {
                transform.eulerAngles = new Vector3(90, 0, 0);
            }
            else if(CurrentRotation.x >= 90.1f && CurrentRotation.x <= 135)
            {
                transform.eulerAngles = new Vector3(90, 0, 0);
            }
        }
    }
}

The simplest way to do this is to take the cube’s forward and up vectors, align them to the nearest world axes, and then recalculate the cube orientation using Quaternion.LookRotation.

To snap a vector along the nearest world axis, find out which of its components (x, y, or z) has the greatest magnitude (positive or negative), and zero out the other two. You don’t need to normalize the result, LookRotation will handle it fine.

Something like this ought to work:

private static Vector3 NearestWorldAxis(Vector3 v)
{
    if (Mathf.Abs(v.x) < Mathf.Abs(v.y))
    {
        v.x = 0;
        if (Mathf.Abs(v.y) < Mathf.Abs(v.z))
            v.y = 0;
        else
            v.z = 0;
    }
    else
    {
        v.y = 0;
        if (Mathf.Abs(v.x) < Mathf.Abs(v.z))
            v.x = 0;
        else
            v.z = 0;
    }
    return v;
}

...

Vector3 alignedForward = NearestWorldAxis(transform.forward);
Vector3 alignedUp = NearestWorldAxis(transform.up);
transform.rotation = Quaternion.LookRotation(alignedForward, alignedUp);

If I understood the question right, this solution might be even better.

public int SnapRotation(int rotation)
{
    // Calculate the remainder when dividing the rotation by 360
    int remainder = rotation % 360;

    // Snap the remainder to the nearest 90 degree step
    int snappedRotation = (int)(Math.Round((double)remainder / 90) * 90);

    // Handle negative values
    if (snappedRotation < -270)
        snappedRotation += 360;

    return snappedRotation;
}

For a smooth transition you could use Quaternion.RotateTowards in void Update(). It could look something like this:

//Im not sure how to cast/tranform a eulerangle to quaternion so I store the rotation in an empty gameObject
private Transform _targetRotationObject = new GameObject().transform;
private GameObject _objectToSnap;
private void Update()
{
    if (Input.GetKeyUp(KeyCode.L))
        _targetRotationObject.eulerAngles = new Vector3(0, SnapRotation(Mathf.RoundToInt(_objectToSnap.transform.eulerAngles.y)), 0);
    _objectToSnap.transform.rotation = Quaternion.RotateTowards(_objectToMove.transform.rotation, _targetRotationObject.rotation, 720);        
}

Hope this helps:)