gyroscope enabled but not working on android device
Hello everyone,
I'm trying to have a camera wich rotates under control of the device gyroscope. After reading the documentation and some trials, I have written a script, attached to the camera, and it works pretty well when I enter play mode in my android device through Unity Remote 4. I can obtain the quaternions from the device and the camera rotates accordingly.
But the problem arises when I build the apk and then test it on my android device (Sony Xperia Z1). In that case, the gyroscope is enabled, but I can not get the readings from the gyroscope: the quaternion value of Input.gyro.attitude is always (0,0,0,0).
I tried a lot of things, but I can not find the problem. Please , can you help me?
Thank you very much in advance (and sorry for my english).
Here is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class rot : MonoBehaviour {
Quaternion initialQuat;
Quaternion attitudeFix;
Quaternion attitudeRel;
Text sondas;
void Start () {
Input.gyro.enabled = true;
initialQuat = Input.gyro.attitude;
sondas = GameObject.Find ("Sondas").GetComponent<Text> ();
}
void Update () {
attitudeRel=Input.gyro.attitude*initialQuat;
attitudeFix = new Quaternion (attitudeRel.x, -attitudeRel.z, attitudeRel.y, attitudeRel.w);
transform.rotation = attitudeFix;
sondas.text = "" + Input.gyro.enabled + attitudeFix;
}
}
Answer by eruizr · Oct 26, 2016 at 09:10 AM
Hi,
I don't know where was the problem exactly but now it is fixed. I did three things:
1) I updated to the last version of Unity.
2) Just after the initialization of the application I wrote:
Input.gyro.enabled = false;
Input.gyro.enabled = true;
3) I gave some time for the gyro of the device to start, so I wait 2 seconds before the first reading of the gyro.attitude.
Hope that this helps.
Hi,
I tried but it still failed, I saw your question that your device is Sony, have you test it on Ios device?
I really have no idea it still return 0001. Anyway, thanks for the help.
Hi again,
I have tested it on IOs devices and it works correctly. I am sorry that I could not help. I really don't know where was the problem in my case.
Regards
When u test the iOS device, have add anything on Xcode? I add coremotion lib, but it's still not working.
It's working now!!! I update to 5.5.0b9, and it works, my old version is 5.5.0b6. Thanks!!!!! Thank you!!!
Answer by Wenger95 · Oct 24, 2016 at 08:02 PM
Hi, Have you sort this? I have same issue, my Input.gyro.attitude always return 0,0,0,1. And I found out Input.gyro.enabled = true; only work once, after start it become to false.
Answer by Bruhpunzel · Nov 22, 2018 at 03:00 PM
On Awake I put;
Gyroscope m_Gyro;
void Awake()
{
//Set up and enable the gyroscope (check your device has one)
m_Gyro = Input.gyro;
m_Gyro.enabled = true;
}
void Update () { //transform.rotation = Input.gyro.attitude; Debug.Log(Input.gyro.attitude); Debug.Log(Input.isGyroAvailable); }
Reads and outputs fine for me now. Hope that helps!
Answer by doq · Feb 03, 2019 at 06:15 AM
I was getting zero data from attitude when I initialized in Awake or Start sometimes. I solved the issue by putting the gyro initialization in a coroutine.
void Start()
{
StartCoroutine(InitializeGyro());
}
IEnumerator InitializeGyro()
{
Input.gyro.enabled = true;
yield return null;
Debug.Log(Input.gyro.attitude); // attitude has data now
}
Answer by Musahan17 · Nov 02, 2020 at 10:03 AM
private bool GyroEnabled()
{
if (SystemInfo.supportsGyroscope)
{
gyro = Input.gyro;
Input.gyro.enabled = true;
return true;
}
return false;
}
This kind of approach worked for me.