- Home /
Android Gyroscope not working ?
Hello there !
I'm currently working on an Android project and for the very first time I have to use Gyroscope. So far, touches have been working pretty well, the same does not go for Gyroscope though. In fact, whether I run my application through Unity Remote or build it to my phone (an Xperia U), it just does not work.
Here is my test code :
using UnityEngine;
using System.Collections;
public class GyroscopeDataDisplayer : MonoBehaviour {
Gyroscope m_gyroscope;
// Use this for initialization
void Start () {
m_gyroscope = Input.gyro;
m_gyroscope.enabled = true;
}
void OnGUI()
{
GUILayout.Label("Gyroscope attitude : " + m_gyroscope.attitude);
GUILayout.Label("Gyroscope gravity : " + m_gyroscope.gravity);
GUILayout.Label("Gyroscope rotationRate : " + m_gyroscope.rotationRate);
GUILayout.Label("Gyroscope rotationRateUnbiased : " + m_gyroscope.rotationRateUnbiased);
GUILayout.Label("Gyroscope updateInterval : " + m_gyroscope.updateInterval);
GUILayout.Label("Gyroscope userAcceleration : " + m_gyroscope.userAcceleration);
}
}
Am I doing something wrong ?
Thanks in advance for your help ! :)
Answer by programmrzinc · Nov 15, 2012 at 02:33 PM
I believe you need to use the Input.acceleration command instead of the Input.gryo. I use it and it works fine.
function Update () {
var dir : Vector3 = Vector3.zero;
// we assume that the device is held parallel to the ground
// and the Home button is in the right hand
// remap the device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
Yup, tried that and it works fine.
Concerning gyroscope, I searched the technical specs of my Xperia U and... it doesn't have one : now that make sense ! I wanted to try some augmented reality stuff, whatever I'll just forget that idea and try to work on something else.
Thanks for your answer BTW :)
Yes, i thought i'Ve read somewhere that the Gyroscope class only works on iPhone, but i can't remember where. We also just use Input.acceleration and it works pretty well.
As a side note: It seems that Unity (or android itself) rotates the vector according to the current screen-orientation when you build for Android. The iPad seems to have a fixed coordinate system, so when you play upside down, it doesn't flip automatically.
Excuse me, can you help me, please ? Why your code is not working for me ?
I'm used @DocteurCox and @programmrzinc code but still not working for my Android, the script i paste in $$anonymous$$ain Camera, how i must do ? :(
Please don't post questions as answers. Start a new question and explain precisely what the problem is. "Not working for you". Why not? Error message? Black screen? Incorrect values?
I want to make project like this http://www.youtube.com/watch?v=XN8x9sUHSC4 (time 0:50)
I used this script :
var speed = 10.0;
function Update () {
var dir : Vector3 = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqr$$anonymous$$agnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate (dir * speed);
}
But it's only moved the camera, not rotated, how to if i want to make like that sample video ?
Answer by taxvi · Sep 10, 2015 at 08:06 AM
Put this in your Start() function
Input.gyro.enabled = true;
Setting Input.gyro.enabled to true works for me on my old Xperia with Unity 5. I think this should be the accepted answer, as the accelerometer and the gyroscope are totally different sensors and may be used for different purposes (accelerometer gives translation acceleration information and gyroscope gives rotation speed information). Thanks @taxvi
While this answer is correct now, it has no explanation on the issue. It just states to do something that's already in the question. The problem here was that Unity didn't support the gyroscope on android in the beginning, only on iOS. I can't remember which version introduced the support for Android. The question is quite old. Which answer gets accepted is up to the OP and the accepted answer helped the OP back then.
Any kind of information on the internet has to be checked for date. It's an old question with the correct old answer. The answer of taxvi doesn't make sense since it doesn't answer the question. Because if someone has the same problem (which means someone uses the same code as the OP) the answer doesn't help at all since they already have that line inside Start. This question and accepted answer belongs to a problem of the past that doesn't exist anymore in the current Unity version.
Oh, sorry I read the question too fast and went directly to the answers. I didn't notice enabled was set to true in the question as it's written on two lines, and neither did I realize that there was a 3 years gap between the 2 answers. $$anonymous$$y apologies. Anyway, in my use case, replacing gyroscope by accelerometer is not an option, hence my comment. I'm pretty new to Unity and this is the first thread I found on the subject after a google search. I guess I should have read the docs before. Glad to see that it works as expected now.
Hi, I add Input.gyro.enabled = true; on void start(), but I don't know why in void update() Input.gyro.attitude still return me (0,0,0,1). And I found out Input.gyro.enabled still equal false, $$anonymous$$y device is iphone6.
Quaternion Device$$anonymous$$ = Quaternion.identity;
void Start () { if (!Input.gyro.enabled) { Input.gyro.enabled = true; Device$$anonymous$$ = Input.gyro.attitude; } //Input.location.Start (); }
void Update () {
Device$$anonymous$$ =Input.gyro.attitude;
Debug.Log("The Device$$anonymous$$ X is "+Device$$anonymous$$[0]+" The Device$$anonymous$$ Y is "+Device$$anonymous$$[1]+" The Device$$anonymous$$ Z is "+Device$$anonymous$$[2]+" The Device$$anonymous$$ W is "+Device$$anonymous$$[3]);
}
I delete some code, but it shouldn't matter.
Can you help me?