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.
What is your desired behavior? What do you want to happen when the rotation exceeds -180? Do you instead want a value between 0 and 360?
– dstears