Gyroscope Magnetic North.

Hi there,
I asked a question recently but I believe it was a little confusing and went un answered. I believe I understand the situation a little bit better now and am unsure on how to do it. The gyroscopes attitude is based on the direction of a compass I am guessing So north is north etc. What I need to do I think, is somehow make the gyroscopes attitude relative to the magnetic north (North in relation to the device). So that when I tilt in relation to it, no matter where the Person/Humans rotation is in the world my light will rotate in the right direction. Here’s a link to the previous thread and I’ve posted my code below. Thank you :slight_smile: Gyroscope Tilting Rotation. Quaternions T_T Help. - Questions & Answers - Unity Discussions

 private Gyroscope gyro;
     public Quaternion rotation = Quaternion.identity;
     private Quaternion initialGyroRotation;
     private Quaternion initialRotation;
     private Quaternion baseRotation;
 
     void Start ()
     {
         gyro = Input.gyro;
         gyro.enabled = true;
         initialRotation = transform.rotation;
         baseRotation = transform.rotation;
         initialGyroRotation = Quaternion.identity;
         transform.rotation = gyro.attitude;
         Screen.orientation = ScreenOrientation.Portrait;
     }
 
     void Update ()
     {
         #if UNITY_IOS
         Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
         #endif
         #if UNITY_ANDROID
         Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, -gyro.attitude.z, -gyro.attitude.w);
         #endif
         Quaternion offsetRotation = Quaternion.Inverse (initialGyroRotation) * attitudeFix;
         rotation = initialRotation * offsetRotation;
         transform.rotation = rotation;
 }

hm, there may be some confusion here.

the gyroscope in devices like a smartphone has nothing to do with north or magnetic fields.
the gyroscope reports the angular velocity the device is rotating with.
eg, if your device is still, the gyroscope reports 0 deg/s for all 3 axes.
if your device is rotating around say the X axis, then the gyroscope’s X axis should indicate that.

for getting the device’s orientation relative to the earth’s magnetic field, you want to access the magnetometer. In unity, this has been nicely packaged up in a class called the Compass.