I am trying to rotate a object based on the sensor values (pitch roll yaw). I have the following script snippet of code to rotate the object.
if ((0<=ax&& ax<1.03f) && (0 <= az && az <1.03f))
{
if (x > calibrateX)
x = x - calibrateX;
else
x = calibrateX - x;
//x = Mathf.Abs(x);
Debug.Log("0-90 Rotation on X @ " + x);
Quaternion xangle = Quaternion.Euler(new Vector3(x, transform.eulerAngles.y, transform.eulerAngles.z));
transform.rotation = Quaternion.Slerp(transform.rotation, xangle, Time.deltaTime * 2f);
}
//Pitch 90-180;
else if ((0<= ax && ax<1.03f) && (-1.03f<=az && az< 0))
{
x = x+180;
Debug.Log("90-180 Rotation on X @ " + x);
Quaternion xangle = Quaternion.Euler(new Vector3(x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z));
transform.rotation = Quaternion.Slerp(transform.rotation,xangle, Time.deltaTime*2f);
}
The script works for angles between 0 -90. The rotation is smooth and as expected. However, when the angle is between 90 -180, the object rotation is not smooth. In the editor it looks like the object is jumping between two points. For example, if the angle is 110 (xangle), the object jumps between the angle 110 and 70. This happens for any angle between 90 - 180.
I need help to understand what I am doing wrong. Thanks for your time.