title: Get Unity compass true north heading
body: <p>I'm currently trying to implement a location-based AR app for Android using Unity C# and ARCore. I have managed to implement an algorithm which gets latitude/longitude and translates it to unity meters. However, this is not enough to place objects at a GPS location since the orientation of the world will be off if you do not align the unity scene with the real world orientation. </p>
  
  <p>Anyway, the problem with the compass is that I cannot find a way to align an object with the true north.
  I have tried aligning an object with true north by using 
  <code>transform.rotation = Quaternion.Euler(0, -Input.compass.trueHeading, 0);</code></p>
  
  <p>However this causes the object to constantly change rotation based on where my phone's heading so in a way true north is always changing. 
  What I am currently trying is:</p>
  
  <pre><code>var xrot = Mathf.Atan2(Input.acceleration.z, Input.acceleration.y);
  var yzmag = Mathf.Sqrt(Mathf.Pow(Input.acceleration.y, 2) + Mathf.Pow(Input.acceleration.z, 2));
  var zrot = Mathf.Atan2(Input.acceleration.x, yzmag);
  
  xangle = xrot * (180 / Mathf.PI) + 90;
  zangle = -zrot * (180 / Mathf.PI);
  TheAllParent.transform.eulerAngles = new Vector3(xangle, 0, zangle - Input.compass.trueHeading);
  </code></pre>
  
  <p>This seems to be working better since the object will point towards a single direction with minimum shaking/jittering of the object due to erratic compass reading but points in one direction nonetheless. However, the problem with this is that it points to a different direction every time the app starts, so true north is never in one place according to the code. However the compass readings are fine as I have checked the readings against other GPS apps which implement a compass. </p>
  
  <ul>
  <li>I have also tried <code>transform.rotation = Quaternion.Euler(0, -Input.compass.magneticHeading, 0);</code> but this gives the same always-changing result as the first one. Any help would be appreciated.</li>
  </ul>