Unity C# Android Gyro - Wrong Orientation

Hello,

I have a cube and a camera object with a script. The script only really says to follow the rotation of the gyroscope. I have followed the tutorials, and this works fine on my iphone, but with my android the objects are always either rotated on their side, or inverse rotation. In addition there is some slight ghost sliding in directions.

Here are my devices;------------------------------------------------------------------------------------------------------
-Unity 2018.2.7f1 (I’ve tried it with older versions as well)----------------------------------------------------
-Windows10 PC--------------------------------------------------------------------------------------------------------------
-Samsung Galaxy s7-------------------------------------------------------------------------------------------------------

Please help, thank you for your time.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Phone_Gyro : MonoBehaviour {

    /// <summary>
    ///  this script reads the phone's gyroscope (the angles it tilts)
    /// </summary>


    // this gameobject initial rotation
    private Quaternion initialRotation;
    // the phone's initial rotation
    private Quaternion gyroInitialRotation;

    // start
    private void Start()
    {

        Input.gyro.enabled = true;

      

        initialRotation = transform.rotation;
        gyroInitialRotation = Input.gyro.attitude;
        
    }// end of start

    // update
    protected void Update()
    {
        Quaternion offsetRotation = Quaternion.Inverse(gyroInitialRotation) * Input.gyro.attitude;  // ** right now everything is reverse
        offsetRotation = offsetRotation * new Quaternion(1, -1, 1, 1);
        transform.rotation = initialRotation * offsetRotation;

        //transform.rotation = Input.gyro.attitude;

    }// end of Update

}// end of phone gyro script

H

Update: I still haven’t solved it, but I’ve made some slight progress. With this code, I am able to get my object to accurately rotate its head Up and Down as I move the phone. However, the turn and tilt options are both backward and swapped. I’m not giving up hope, and I am posting my progress here.

/// <summary>
///  this script reads the phone's gyroscope (the angles it tilts)
/// </summary>

// this gameobject initial rotation
private Quaternion initialRotation;
// the phone's initial rotation
private Quaternion gyroInitialRotation;

// start
private void Start()
{
 
    // activate gyroscope
    Input.gyro.enabled = true;

    initialRotation = transform.rotation;
    gyroInitialRotation = Input.gyro.attitude;

    print("The Object's original rotation: " + initialRotation);
    print("The Gyro's original rotation: " + gyroInitialRotation);
    
}// end of start

// update
private void Update()
{
    print("The Gyro's current rotation: " +Input.gyro.attitude);

    Quaternion offsetRotation = Input.gyro.attitude;
    //offsetRotation = offsetRotation * new Quaternion(25, 120, -90, 8); // ** THIS works for looking up and down (backwards for the rest)

    offsetRotation = offsetRotation * new Quaternion(25, 120, -180, 0); // ** testing this

    transform.rotation =  offsetRotation;

    print("The Gyro's offset rotation: " + offsetRotation);

}// end of Update

The main problem is that Input.gyro.attitude returns a quaternion for a right-handed coordinate system, while unity has a left-handed coordinate system.

This is what worked for me to rotate an object on android according to how I rotate/tilt the device in my hand.

using UnityEngine;
 
public class GyroObject : MonoBehaviour
{
     private Quaternion _origin = Quaternion.identity;

     private void getOrigin()
     {
         _origin = Input.gyro.attitude;
     }
     
     private void Start()
     {
         Input.gyro.enabled = true;
         getOrigin();
     }
     
     void Update()
     {
         transform.rotation = ConvertRightHandedToLeftHandedQuaternion(Quaternion.Inverse(_origin) * Input.gyro.attitude);
     }
     
     private Quaternion ConvertRightHandedToLeftHandedQuaternion (Quaternion rightHandedQuaternion)
     {
         return new Quaternion (- rightHandedQuaternion.x,
             - rightHandedQuaternion.z,
             - rightHandedQuaternion.y,
             rightHandedQuaternion.w);
     }
 }