Hi there,
I’m using the gyroscope to get rotation data so I can find out when my mobile is being tilted up/down & left/right. This works well. But as soon as you lets say rotate to the left in your chair. Up and Down Become the Left and Right tilt and Left and Right tilt become the up and down. If you did a 180 in your chair everthing becomes inverted. Up becomes down. vice versa.
I need Up and Down to always be up and down, and left and right to always be left and right tilt.
My current code:
private Gyroscope gyro;
public Quaternion rotation = Quaternion.identity;
private Quaternion initialGyroRotation;
private Quaternion initialRotation;
private Quaternion baseRotation;
void Start ()
{
gyro = Input.gyro;
gyro.enabled = true;
initialRotation = transform.rotation;
baseRotation = transform.rotation;
initialGyroRotation = Quaternion.identity;
transform.rotation = gyro.attitude;
Screen.orientation = ScreenOrientation.Portrait;
}
void Update ()
{
#if UNITY_IOS
Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
#endif
#if UNITY_ANDROID
Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, -gyro.attitude.z, -gyro.attitude.w);
#endif
Quaternion offsetRotation = Quaternion.Inverse (initialGyroRotation) * attitudeFix;
rotation = initialRotation * offsetRotation;
transform.rotation = rotation;
}