# How to properly get the x-axis value among the data of the smartphone's gyro sensor in Unity

I want to load x, y, z data using the IMU sensor of the smartphone.

Input_gyro_attitude_eulerAngles = Input.gyro.attitude.eulerAngles - Offset; // gyro angle


Through the code above, the y and z axes are loaded well even though there is a little error.

But For the x-axis, only values ​​within the range of 270 to 90 degrees are recorded, and values ​​between 90 and 270 are not recorded as shown in the photo.

1. How can I get it to load properly?

2. And why are only the values ​​on the x-axis appearing strangely?

D = Quaternion.Euler(T, 0, 0)


This creates a unit quaternion, a point on a four-dimensional sphere with three of those dimensions being imaginary numbers. The four components of the quaternion x, y, z, and w are NOT angles.

D.x * 360f


This takes one of those imaginary axis values and multiplies it by 360. This does NOT make it an angle. It's geometrically meaningless.

D.x is the sine of an angle, but it may not be exactly the angle that you want at the end of the day. Because the total angle in the quaternion will always be -180 to +180, and that means if you have rotation on the y or z axes, the x alone won't hit those extremes.

To extract out "I just care about the rotation angle in this specific plane," you can do something a bit like this:

// Turn your Euler angle offset into a quaternion.
var offset = Quaternion.Euler(offset_angles);

// Reverse this quaternion to "undo" the offset.
var correction = Quaternion.Inverse(offset);

// Apply the correction to the quaternion - this behaves more consistently
// than trying to add/subtract Euler angles.
var correctedAttitude = Input.gyro.attitude * correction;

// Now we can use this attitude to transform the world up vector
// into the phone's local coordinate system.
var sensedUp = Quaterntion.Inverse(correctedAttitude) * Vector3.up;

// And you can use the components of that vector to discern an orientation
// angle in the plane of your choosing.
var degreeAngle = Mathf.Atan2(sensedUp.y, sensedUp.x) * Mathf.Rad2Dag;


Depending on which axis of rotation you care about, whether you want clockwise or counterclockwise rotation to be positive, and where you want 0 degrees to sit within the circle, you might choose a different reference vector or use different components inside Atan2.

• Thank you for answer. But it doesn't work properly. We found that the reason this is happening is the gimbal lock, and we are fixing it accordingly. Commented Aug 9, 2021 at 11:08
• Be sure to post an answer with the solution that works for you. Also, if you want users to improve their answers, you'll need to give them more feedback about what's going wromg. Commented Aug 9, 2021 at 12:05
• Currently, offset and correction variables are not used because it is not necessary to consider the offset. correctedAttitude = Input.gyro.attitude; and tried to output degreeAngle as it is. As a result, a specific axis? Even though it rotates about 10 degrees, it becomes -120 degrees... -60 degrees ... -10 degrees. It seems that I may have misunderstood something. I don't seem to have properly grasped what degreeAngle means Commented Aug 11, 2021 at 12:13
• You might be rotating around a different axis than what this code is set up to measure. Since I don't have access to the device you're testing with, I can't say exactly what changes you'd need to make, but the main candidates are using different components of sensedUp as arguments to the Atan2 function. Commented Aug 11, 2021 at 12:41
• I'll try it soon and comment! Thank you DMGregory Commented Aug 12, 2021 at 7:07