1
votes

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 quantime.

1
Try to get the quaternion output of the sensor and pass it to Unity without ever using angles (stay away from using angles). They are known to cause jumping.Kamran Bigdely

1 Answers

0
votes

The problem is probably at x = x + 180. According to Quaternion.Euler's documentation:

Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis.

So, if x goes from 0 to 90 then 90 to 180, the targeted rotation will be: 0 to 90 and then 270 to 360.

In order to better understand the problem, you can post how the x is calculated.

Since now x seems to me is simply the angle pitch, the solution would be just removing the x = x + 180.