- Home /
Input.gyro.attitude returns zero values when tested with Moto G 4th generation device
Hi, being fairly new to unity, am using unity 5.4.0f3. I am working on a project which makes use of device gyroscope, if it exists, to give rotation to the main camera. I have tested the application on few devices, out of which Moto G 4th generation phone is the one, that I came across as of now, which even if has a gyroscope on it, gives zero values for Input.gyro.attitude. It is a device with 6.0.1 android and as mentioned 'https://issuetracker.unity3d.com/issues/input-dot-gyro-dot-attitude-doesnt-output-data-from-orientationsensor', there was one bug reported in unity about issues with getting gyro attitude values in newer devices(Android 6 or later)
I tried below code to debug the gyro values in phones:
using UnityEngine; using System.Collections;
public class gyro : MonoBehaviour {
Gyroscope gyr;
// Use this for initialization
void Start () {
gyr = Input.gyro;
if (SystemInfo.supportsGyroscope)
{
gyr.enabled = true;
Debug.LogWarning("Gyro Enabled");
}
}
// Update is called once per frame
void Update () {
Debug.Log(gyr.attitude);
}
}
Is the issue really solved or am I doing anything wrong here? Is there a workaround that I should be looking at for newer devices like this? Any help or suggestion would be great.
Hi, I am facing the same issue. Could anyone please help us out here?
Answer by anaeronbaggins · May 09, 2018 at 06:01 AM
@sanketkale The Moto 4th generation phones and lower generation(i am not that sure about the lower gen phones, but sure about the 4th gens, since I have a moto G4 plus) do not have a compass or magnetometer, which is what Input.gyro.attitude uses. If you want to use the gyroscope for AR, you need the compass/magnetometer, else the accelerometer will have to do. There are some obvious trade-offs between the gyroscope and the accelerometer, but then, it's a necessity. @yuk27 how did the code work if the phone doesn't have a compass/magnetometer? Do let me know, i would love to know. :)
Thanks! :D, sorry if the answer is wrong or something, i just posted my own experience. If someone has a change, do let me know, bye <3
Yes, $$anonymous$$otoG 4th generation phones lack magnetometer and gyro.attitude uses magnetometer's values in conjunction with gyroscope's values for giving a directional orientation quaternion. I have confirmed this with people from unity back in 2017. So, @yuk27, this is not a bug with unity, it's just how gyro.attitude works. And yes, your code could be used to get a directionless orientation quaternion from rotationRateUnbiased which takes values from just gyroscope sensor. So if you just want to rotate main camera based on phone's orientation irrespective of what direction the phone is from magnetic north, then you should go for gyro.rotationRateUnbiased. Well anyways, thanks for the answer @anaeronbaggins.
Answer by yuk27 · Jan 25, 2017 at 08:15 PM
For anybody having this problem, I face it for a long time, in theory this problem was a bug on Unity that was resolved:
But even after that, the problem with gyro.attitude continued.
I found this thread that was really helpfull for me:
https://forum.unity3d.com/threads/gyroscope-quaternion-to-unity-camera-help-please.75288/
Where I found this code (Created by ulissescad):
float yRotation;
float xRotation;
void Start{
Input.gyro.enabled = true;
}
void Update{
yRotation += -Input.gyro.rotationRateUnbiased.y;
xRotation += -Input.gyro.rotationRateUnbiased.x;
transform.eulerAngles = new Vector3(xRotation, yRotation, 0);
}
($$anonymous$$oto G4 plus) It kind of Worked, Should it be tweaked? Because when I run it, Say I make a 360 degree Turn, On the game I check Half a turn (Not exactly half) PLEASE Ive been so many hours trying to make it work