Gyroscope Z axis not working (Solved)

Hey everybody,
I need to counter rotate the main camera’s local forward facing axis (z) relative to an android upward facing axis while laying flat on a table. This counter rotation should keep the game world locked on one fixed, real world axis, so that the player can spin their phone around and the game will remain facing them. I figured that the compass should do the trick but nothing I’ve tried (neither anything I could find online,) worked properly to get the screen’s outward axis. Does anyone have a solution?
This is my current attempt:
Real world up seems to be X
Game world up is Z
Camera angle: Top down

using UnityEngine;

public class Rotate_Gyro : MonoBehaviour
{
    /*void Start()
    {
        //DeviceRotation.InitGyro();
        //Input.gyro.enabled = true;
        //Input.compass.enabled = true;
    }*/
   
    void Update()
    {
        Quaternion gyro = DeviceRotation.Get();
        Vector3 rot = transform.rotation;
        rot.z = gyro.eulerAngles.x; //is X up while the phone is flat? Z is up in THIS game world.
        transform.rotation = Quaternion.Euler(rot);
    }
}
using UnityEngine;
public static class DeviceRotation {
    private static bool gyroInitialized = false;
   
    public static bool HasGyroscope {
        get {
            return SystemInfo.supportsGyroscope;
        }
    }
   
    public static Quaternion Get() {
        if (!gyroInitialized) {
            InitGyro();
        }
       
        return HasGyroscope
            ? ReadGyroscopeRotation()
                : Quaternion.identity;
    }
   
    private static void InitGyro() {
        if (HasGyroscope) {
            Input.gyro.enabled = true;                // enable the gyroscope
            Input.gyro.updateInterval = 0.0167f;    // set the update interval to it's highest value (60 Hz)
        }
        gyroInitialized = true;
    }
   
    private static Quaternion ReadGyroscopeRotation() {
        return new Quaternion(0.5f, 0.5f, -0.5f, 0.5f) * Input.gyro.attitude * new Quaternion(0, 0, 1, 0);
    }
}

Thank you,
Alec Maier.

I solved this with the following code:

using UnityEngine;

public class Rotate_Gyro : MonoBehaviour
{
    void Start()
    {
        //DeviceRotation.InitGyro();
        Input.gyro.enabled = true;
        //Input.compass.enabled = true;
    }
    [SerializeField]
    UnityEngine.UI.Text _text;
    void Update()
    {
        Quaternion gyro = Input.gyro.attitude;
        //gyro = Quaternion.Euler(gyro.eulerAngles + new Vector3(90, 0, 0));
        Vector3 rot = gyro.eulerAngles;
        rot.x = 0; //is X up while the phone is flat? Z is up in THIS game world.
        rot.y = 0;
        transform.rotation = Quaternion.Euler(rot);//Quaternion.Lerp (transform.rotation, gyro, Time.time * 5);
        _text.text = "X: " + transform.rotation.x + ", Y: " + transform.rotation.y + ", Z: " + transform.rotation.z;
    }
}

To add to that, I was able to do a GPS like direction pointer with the Gyro scope using this idea. I had a flat mesh plane as an example. Basically swapping the gyro’s z with Unity’s y.

rot = gyro.attitude.eulerAngles;
Vector3 newY = new Vector3( 0, rot.z, 0);
transform.localRotation = Quaternion.Euler (newY);

So “spinning” your phone would spin the mesh