How to obtain the correct angles of a GameObject in Unity

Hello everyone, I’m just starting out in the world of Unity, so I’m a beginner. I have a game with airplanes that I’m implementing on a haptic chair in the real world, and I want to obtain the rotation angles of my GameObject to send them via telemetry to my chair controller. I’ve managed to obtain the angles with the help of ChatGPT to get rotations between -180 and 180, but I have a problem.

I’ll share my code below to proceed with the explanation:

using UnityEngine;

public class Main : MonoBehaviour
{
    private int Roll;
    private int Pitch;  
    
    void Update()
    {
        // Obtener la rotación actual del GameObject
        Quaternion rotacion = transform.rotation;

        // Convertir la rotación a ángulos eulerianos
        Vector3 angulos = rotacion.eulerAngles;

        Roll = Mathf.RoundToInt(angulos.z);
        Pitch = Mathf.RoundToInt(angulos.x);

        // Normalizar los ángulos entre -180 y 180 grados
        if (Roll > 180)
        {
            Roll -= 360;
        }

        if (Pitch > 180)
        {
            Pitch -= 360;
        }

        // Enviar los valores de Roll y Pitch por telemetría
        QuPlaySimtools.QuSimtools_SendTelemetry(-Roll, -Pitch, 0, 0, 0, 0, 0, 0, 0);
    }
}

So, it gives me the values I need, between 180 and -180, but I’m only performing a summation trick because the rotation is from 0 to 360 clockwise. The problem I’m facing is that when I rotate and exceed -180, it abruptly jumps to positive 180 due to the mathematical manipulation.

Is there any solution to avoid this abrupt jump?

Do you want the values between 0 and 360 and with respect to clockwise/counterclockwise?

You can use:

Roll = Angle_0to360 (Roll, true); //counterClockwise
Roll = Angle_0to360 (Roll, false); //clockwise
public static int Angle_0to360 (int angle, bool counterClockwise)
{		
	if (angle < 0f) {
		angle = 360 + angle;
	}	

	if (!counterClockwise) {	
		angle = 360 - angle;
	} 

	angle %= 360;

	return (angle);
}