- Home /
Android Gyroscope inaccuracy problems.
Hi, i'm making a game for android, where you are controlling a car from above and by rotating the phone you rotate the car in the other direction, so that it looks like the car is always facing the same direction in real world. I'm using the gyroscope for this purpose.
Input.gyro.enabled = true;
Input.gyro.updateInterval=0.01f;
Problem is, that both methods i was trying to use are really inaccurate.
First i tried to add the rotationRate to the current rotation.
player.transform.localEulerAngles += new Vector3 (0, Input.gyro.rotationRate.z, 0);
When i start the game, the player will rotate faster then it should. When i turn my phone 100° the player performs a full 360° turn. Maybe im missing out on some reference to the framerate. So i tried getting some multiplication value, by dividing Time.deltaTime with the updateInterval.
player.transform.localEulerAngles += new Vector3 (0, Input.gyro.rotationRate.z, 0)* (Time.deltaTime/Input.gyro.updateInterval);
But that made the car rotate even faster and out of controll or if i switch updateInterval and deltaTime it doesnt even move.
My other approach was setting it directly to the rotation of the device.
player.transform.localEulerAngles = new Vector3 (0, Input.gyro.attitude.eulerAngles.z, 0);
That works for a short momentan, but then the results are getting incredibly inaccurate, having the car skip more than 10° on fast movements.
I dont quite know wich of those is the best solution but since adding the rotationRate proved to be the smoother option i would like to try it that way, but i dont know with what to multiply it to my desired result.
Thanks in advance for any help, FlomoN
Answer by Pendantic · Mar 10, 2015 at 04:20 PM
I would recommend sticking with gyro.attitude but possibly adding a lerp to smooth out those jumps you have. Also if you dealing with angles you may not want to convert to euler as there can be some weird issues with that. It looks like you want the z rotation of the device to set the Y rotation which is a little tricky but can be done. This is how you could convert it
//this returns the direction use Vector3.right if Vector3.up doesn't work
Vector3 direction = Input.gyro * Vector3.up;
//this just will take away any up or down rotation
direction.y = 0;
Quaternion rot = Quaternion.LookRotation(direction,Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation,rot,5f*Time.deltaTime);
Okay main-reason i used euler is that i don't understand how Quaternion works and Quaterion also seems very abstract to me. One more problem is, that the player needs to have a certain starting rotation (-90, 90, 0) and i dont know how to apply that to the Quaternion.
I tried this
rot += new Quaternion(0.5f, -0.5f, 0.5f, 0.5f);
that quaternion seems to be my needed rotation according to some online converter, but it says, that you can't add quaternions together.
you may want to parent you gameobject so that it's initial world world rotation is 0,0,0 then in start get your initial gyro quaternion as above and then divide any further rotations you get by that initial value. With quaternions multiplying is equivalent to adding and dividing is equivalent to subtraction, don't ask me why
Doesn't really work, when i multiply that value the car is still facing the same direction as before.
Vector3 direction = Input.gyro.attitude * Vector3.up;
//this just will take away any up or down rotation
direction.y = 0;
Quaternion rot = Quaternion.LookRotation(direction,Vector3.up);
rot *= new Quaternion(0.5f, -0.5f, 0.5f, 0.5f);
player.transform.rotation = Quaternion.Lerp(transform.rotation,rot,5f*Time.deltaTime);
Sorry this is how you subtract quaternions
result = originalRotation * Quaternion.Inverse(subtractingValue)
the reason I do direction.y = 0 is so the if the user tilts the device to the side your car shouldn't freak out and roll over, although I forgot to ask if your game is 2D and uses the x and y coordinate system to move around in which case you would want to do direction.z = 0