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;
	}

}

No space. Also when it doesnt delete the clone the next one has a name Options(clone)(clone) and so on, if that helps,

6 Answers

6

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;

  1. 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.

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!!!

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.

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!

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
}

private bool GyroEnabled()
{
if (SystemInfo.supportsGyroscope)
{
gyro = Input.gyro;
Input.gyro.enabled = true;
return true;
}

    return false;
}

This kind of approach worked for me.

In my case I was using Unity Remote and I already had Input.gyro.enabled = true in place, but I was still getting gyroscope values equal to (0,0,0,0), so I did File > Build and Run and from that point on the gyroscope values got correctly updated.