I made some game using Gyroscope in both iOS and Android.
I used Attitude in Gyroscope. But its results are too different.
var offsetRotation : Quaternion = Quaternion.Inverse(originAttitude) * curentAttitude;
var offsetRotationEulerAngles : Vector3 = offsetRotation.eulerAngles;
Are there some difference in Gyro between iOS and Android?
Sorry for my weak English.
I created a game using the gyro in Android and came across a ton of issues and inconsistent documentation about getting correct values. Conflicting advice I found included…:
- Rotate the gyro attitude reading by a
constant quaternion (0,0,1,0) and
rotate its parent object by a
constant angle of (90,180,0) for all
device orientations (Post from
“Aroha” in
http://forum.unity3d.com/threads/98828-sharing-gyroscope-controlled-camera-on-iPhone-4/page5)
- Rotate the gyro attitude by eulerangles (90, 0, 90) (Post by
“George Foot” in
http://forum.unity3d.com/threads/128493-Match-Unity-camera-with-iPhone-camera)
- Rotate the gyro attitude dependent on screen orientation – Z
rotation of –90 for Landscape Left,
+90 for Landscape Right, or 180 for Portrait Upside Down, and also invert
the z and w coordinates of the
attitude quaternion to be (x, y, -z,
-w) (http://blog.heyworks.com/how-to-write-gyroscope-controller-with-unity3d/)
In the end, I found that what worked for me on Android (tested on Nexus 10 and Sony Xperia S) was to invert the z and w values of the gyro attitude:
attitudeFix = new Quaternion(gyro.attitude.x, gyro.attitude.y, -gyro.attitude.z, -gyro.attitude.w);
Note that this only worked when the device was in Landscape Left mode (which was the only orientation I was trying to support). No idea whether this works on iOS as I don’t have a device to test, but I suspect you may need to adjust it. If you do find different values that work for iOS gyro, remember that you can add conditional preprocessor code to adjust depending on platform:
#if UNITY_IPHONE
...
#endif
#if UNITY_ANDROID
...
#endif