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,
I don’t know where was the problem exactly but now it is fixed. I did three things:
I updated to the last version of Unity.
Just after the initialization of the application I wrote:
Input.gyro.enabled = false;
Input.gyro.enabled = true;
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, 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.
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
}
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.
No space. Also when it doesnt delete the clone the next one has a name Options(clone)(clone) and so on, if that helps,
– ccwiegreff