I'm creating a game in Unity which takes input from the physical rotation of the device and turns it into a force applied to the main character (a ball). Using Input.Acceleration it currently works just fine, and I added a z-axis offset so you can choose what is the default "up" for it, say 45 degrees from the table in front of you. The problem is, Input.acceleration gives values which repeat themselves, like sine curve between 0 (flat on table) and 1 (90 degrees to the table), but the y and z axis both do this at different intervals (like cos/sin sorta pattern). I'm not quite sure how to explain this better.. but I'll list what they values for y and z are for each 90 degrees, I somehow need to convert them into a 0-4 value, or an angle in 360 degrees - just some sort of linear increasing value for the z rotation.
0 degrees (flat on table) : y = 0, z = -1
90 degrees (facing towards you) : y = -1, z = 0
180 degrees (flat on table face down) : y = 0, z = 1
270 degrees (facing away from you) : y = 1, z = 0
Update: Using the equation {((Input.acceleration.y * 2) + Input.acceleration.z + 1) * 90f} it gives an angle between 0, and 360 at a full rotation of the device, but in between 0 and 90 for example, it gives a value of 120 degrees before going back down to 90 at the first quarter rotation?
Update2: I found a website which shows how to use the gyroscope to give an angle for the device rotation (instead of Input.Acceleration) using this code (C#)
void GetZRotation ()
{
Quaternion referenceRotation = Quaternion.identity;
Quaternion deviceRotation = new Quaternion(0.5f, 0.5f, -0.5f, 0.5f) * Input.gyro.attitude * new Quaternion(0, 0, 1, 0);
Quaternion eliminationOfXY = Quaternion.Inverse(
Quaternion.FromToRotation(referenceRotation * Vector3.forward,
deviceRotation * Vector3.forward)
);
Quaternion rotationZ = eliminationOfXY * deviceRotation;
float roll = rotationZ.eulerAngles.z;
return roll;
}
I would be able to solve this just as easily if I knew how this worked, and how to get the Y value instead of Z (I tested it and it gives a linear value between 0 and 360 tilting the phone left to right, but I need the angle forwards-backwards)
https://mcmap.net/q/10628/unity-how-to-obtain-roll-tilt-angle-from-gyroscope