- Home /
Amplify Gyro Input
So I'm getting a quaternion back from a gyroscope in my mobile device, and I'm using that to rotate an object in my game. What I'm trying to do is amplify the amount of rotation by some amount, so if the gyro is rotated 90 degrees clockwise, the object in game will actually rotate more like 120 degrees clockwise. Is there a recommended way to do this? Quaternion math is sort of a black box to me.
You can get EulerAngle with Quaternion.eulerAngles Unity Docs - Quaternion.eulerAngles which returns Vector3 containing x,y,z rotations and use it for calculation.
How would I go about using it for calculation? I've tried things like this
Quaternion quat = initRotation * RotateFromGyro();
Vector3 rotationThisFrame = quat.eulerAngles;
rotationThisFrame *= sensitivity;
quat = Quaternion.Euler(rotationThisFrame.x, rotationThisFrame.y, rotationThisFrame.z);
transform.rotation = quat;
which works if I assign quat directly to the transform.rotation without doing anything but doesn't work if I modify the rotation this frame
Edit: By doesnt work i mean the rotation gets really wonky, rotating in unanticipated axis at unanticipated speeds
I would read the acceleration values which go from -1 to 1. it's easier to convert that into a rotation with Quaternion.Euler(). and then just set it as the rotation for the object
Answer by lgarczyn · Mar 15, 2017 at 07:26 PM
DO NOT USE EULER ANGLES.
It's a terrible idea. Just don't do it. They can blow up in so many ways. As soon as you are doing rotation on more than 1 axis, Quaternions all the way.
There are tons of way to do this. If you simply want to double the rotation (moving 1 degree IRL moves 2 degree in game), just do:
transform.localRotation = initRotation * rotateFromGyro * rotateFromGyro;
if you actually want a factor, just do
transform.localRotation = initRotation * Quaternion.LerpUnclamped(Quaternion.identity, rotateFromGyro, ratio);
If the ratio is 0.5f, you'll get half the rotation, if it is 2f, you'll get twice the rotation. There might be unforeseen behaviors, but this should work.
you sir, win the accepted answer. Thanks this helps out a lot!
Hi. I'm beginner with unity and actually finding the way to amplify the rotational value. I'm stuck on this issue. Can you please tell me; what is initRotation and RotateFromGyro() in: transform.localRotation = initRotation RotateFromGyro() RotateFromGyro(); waiting for the reply asap. Thank you!
Hi, it's pretty simple, they are both private Quaternions of the script.
initRotation is the default rotation of your object:
if your object is not rotated at all, you can set it to "Quaternion.identity", or remove it.
if your object is rotated by, say (90,90,0), you can just set to Quaternion.Euler(90,90,0)"
if you want the object to keep the rotation from the editor as default, set it to "transform.localRotation" on Start()
transformFromGyro is simply Input.gyro.attitude, but you can use another source , such as another gyro library, or the default C# interface.
Thank you ceandros! Sorry for late reply. I need to increase the rotation speed of camera with the head rotation like if head is rotated 30 degrees then camera rotation must be rotated like 40 or 50 degrees. So the solution mentioned above would be suitable for this or not?
Your answer
Follow this Question
Related Questions
Gyroscope in Landscape mode snaps object to random rotation 0 Answers
Comparing quaternion values and extracting values from quaternions 0 Answers
Virtual Reality - Android Gyroscopic Camera - Pitch and Roll Issues. 1 Answer
multiplying gyro output 0 Answers
How to get the value of the an angle of the Gyroscope? 1 Answer