- Home /
Using Gyroscope with Android
Hi
I'm making a game that can be played with both iOS and Android devices and the gyrescope is an important part af that game. When a button is pushed I lock the current orientation of the device like this:
// Lock gyro-coordinates
gyrXLocked = Input.gyro.attitude.x;
gyrYLocked = Input.gyro.attitude.y;
gyrZLocked = Input.gyro.attitude.z;
Afterwards I have a light that lights up every time the device has this sepcific orientation (+/- a float value). It works perfect on iOS but on Android the light seems to light up kind of random and the orientation where the light lights up seems to be changing over time. Sometimes it seems to be affected by the physical position in the world and not the device orientation.
I have searched the web and found several posts saying gyroscope doesn't work on android. Though, these posts have been 2-3 years old so I would think a gyroscope would be implemented in most android devices by now!? Others have found a solution by using the accelerometer instead, but since I have to use a specific orientation that stays the same over time, that wouldn't work for me..
Does anyone know why the gyrescope is working differently on iOS and Andriod - and more importantly - does anyone have a solution to my problem?
Thanks
/Moeskjaer
Answer by tanoshimi · Nov 25, 2014 at 01:12 PM
I had this exact problem last year and wrote up a solution that worked for me at https://alastaira.wordpress.com/2013/08/24/the-7dfps-game-jam-augmented-reality-and-spectral-echoes/ (look for the heading Day 2: Accessing Sensor Data in Unity. A.K.A “Fun with Quaternions”).
Essentially, I had to create a parent gameobject rotated 90 degrees about Vector3.right, and then invert the z and w components of the gyroscope's attitude quaternion. Why? I have no idea. This was Unity 4.2, and worked on Android 4.1.2 on a Sony Xperia S and Android 4.3 on a Nexus 10 when using Landscape Left orientation.
Your answer
Follow this Question
Related Questions
Jet Accelerometer Script 1 Answer
Android Gyro explanation? 2 Answers
Input.gyro.attitude Accuracy 1 Answer
How should I interpret the gyro's information? 0 Answers
Is Gyroscope supported on all mobile Android iOS devices with a gyroscope? 3 Answers