- Home /
Having hard time working with rotations and gyro
Hi,
I need some help with rotations and quaternions:
I'm reading the Gyroscope of my mobile phone and the results I'm getting are not consistent with what is supposed to.
The X axis of Input.gyro.attitude is working good, I use Input.gyro.attitude.eulerAngles.x and 0 degrees is the north no matter how tilted the phone is. The problem is with the Y axis, that is supposed to be the tilt degrees, it varies the value if I move the X axis while keeping the same tilt.
I have been busy at this for days now and cannot find an explanation for this behavior.
Answer by ulissescad · Jul 29, 2016 at 12:29 PM
Try this:
I used some examples of reply and unity to made this code, and him works fine to me:
float yRotation; float xRotation; void Start{
Input.gyro.enabled = true; } void Update{ yRotation += -Input.gyro.rotationRateUnbiased.y; xRotation += -Input.gyro.rotationRateUnbiased.x;
transform.eulerAngles = new Vector3(xRotation, yRotation, 0); }
Answer by nicomaque.jette · Mar 06, 2015 at 03:04 AM
It's an old thread however if someone bumps on this one, the following thread helped me out a lot: http://forum.unity3d.com/threads/gyroscope-quaternion-to-unity-camera-help-please.75288/
These simple commands made the tricks for me (unless the phone is flipped):
Camera.mainCamera.transform.rotation = gyroQuaternion;
Vector3 ea = gyroQuaternion.eulerAngles;
Camera.mainCamera.transform.eulerAngles = new Vector3( -ea[0], -ea[1], ea[2]);
Answer by GameVortex · Sep 04, 2014 at 09:52 AM
This is because it is the EulerAngles representation of the rotation Quaternion which does not obey a normal degree rotation and will not always be what you expect even though they are correct for the rotation. I suggest you read a little up on Quaternions and how they work.
As a test: Have you tried rotating an object in your scene to match the rotation of the gyroscope, just to see if the rotation is correct? Example: transform.rotation = Input.gyro.attitute;
I have attached the gyro.attitude to the main camera and works good.
If I use gyro.attitude.x (which is a quaternion X axis) it gives the same values as gyro.attitude.y when pointing west and east when tilting the phone, that's why I'm converting them to Euler because the reading is not what I expect in quaternions.