I’m using this code to rotate an object bg on the z-axis using a gyroscope:
Quaternion eliminationOfXY = Quaternion.Inverse(
Quaternion.FromToRotation(referenceRotation * Vector3.forward,
GyroToUnity(Input.gyro.attitude) * Quaternion.Euler(0f, 0f, 90f) * Vector3.forward));
Quaternion rotationZ = eliminationOfXY * GyroToUnity(Input.gyro.attitude) * Quaternion.Euler(0f, 0f, 90f);
bg.transform.rotation = Quaternion.Slerp(bg.transform.rotation, rotationZ, 8f * Time.deltaTime);
Everything works well, but the problem is that if I take the smartphone in my hands and start rotating around its axis, the object will also rotate if I tilted the smartphone clockwise and counterclockwise. As I understand it, the gyroscope does not distinguish whether the rotation is the result of a change in the orientation of the device itself or a change in the orientation of the entire surrounding space.
How can I block the rotation in space and read only the change in orientation (rotation clockwise and counterclockwise)?