1
\$\begingroup\$

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.

enter image description here

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);
}
\$\endgroup\$
2
  • \$\begingroup\$ What happens if you omit the gyro to Unity function and use the Input.gyro.attitude directly? \$\endgroup\$
    – DMGregory
    Jul 27 '19 at 17:59
  • \$\begingroup\$ the gyro to unity does translate from right handed to left handed. Tried to omit it but did not end up solving anything :/ \$\endgroup\$
    – TheCell
    Jul 27 '19 at 18:26
2
\$\begingroup\$

I solved the problem with another Quaternion multiplication (rotation). Keep in mind: The Quaternion multiplication order does matter.

enter image description here

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);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .