- Home /
Change Gyroscope forward
Hi I've asked on gamedev stack (https://gamedev.stackexchange.com/questions/174107/unity-gyroscope-orientation-attitude-wrong) but no answer so far, here is my question:
I am using the Unity reference and example implementation here https://docs.unity3d.com/ScriptReference/Gyroscope.html
I struggle to fix an orientation problem. When my phone lies flat on the ground, screen facing upwards, unity thinks I am looking as if I'd take a photo of a landscape. When I actually hold the phone as if I'd take a landscape photo, the screen shows me the sky. Rotating then shows me an arc from sky to my left or right side.
What I want is: Holding the phone as if I'd take a landscape picture should do the same in Unity.
Here is a short video where the phone lies flat and then I pick it up and look left and right on a chair.
and this is the codesnipped responsible:
protected void Update()
{
GyroModifyCamera();
}
void GyroModifyCamera()
{
transform.rotation = GyroToUnity(Input.gyro.attitude);
}
// The Gyroscope is right-handed. Unity is left handed.
// Make the necessary change to the camera.
private static Quaternion GyroToUnity(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
Answer by TheCellCH · Jul 30, 2019 at 06:46 PM
I solved the problem with another Quaternion multiplication (rotation). Keep in mind: The Quaternion multiplication order does matter.
The Code is the following
private Gyroscope phoneGyro;
private Quaternion correctionQuaternion;
// Start is called before the first frame update
void Start()
{
phoneGyro = Input.gyro;
phoneGyro.enabled = true;
correctionQuaternion = Quaternion.Euler(90f, 0f, 0f);
}
// Update is called once per frame
void Update()
{
GyroModifyCamera();
}
// The Gyroscope is right-handed. Unity is left handed.
// Make the necessary change to the camera.
void GyroModifyCamera()
{
Quaternion gyroQuaternion = GyroToUnity(Input.gyro.attitude);
// rotate coordinate system 90 degrees. Correction Quaternion has to come first
Quaternion calculatedRotation = correctionQuaternion * gyroQuaternion;
transform.rotation = calculatedRotation;
}
private static Quaternion GyroToUnity(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
You have provided great code, but how can I calibrate the gyro so that it does not matter in which direction you start with the phone it will set that rotation as the initial start rotation?
From the top of my head: subtract the startup quaternion from the 90 degree quaternion and save it. For every calculation you multiply that in see line whith the comment about correction
I don't really know what you mean exactly. Could you maybe explain it with code?
Replace the correction quaternion withyoura calculated quaternion. Ins$$anonymous$$d of 90 degrees youll have to add more than 90 degrees based on the initial gyro position. But I'm just assu$$anonymous$$g, you'll need to try it out.
Can you please post the code to include how to set the initial rotation to where the virtual camera is looking in the editor?
Your answer
Follow this Question
Related Questions
Input.gyro.attitude not working on newer Android devices 3 Answers
3rd player orientation using gyro with offset 0 Answers
Android gyro 1 Answer
Clamp gyro rotation 0 Answers