title: Quaternion difference between real world objects -> Unity
body: <p>I'm creating an unity app on PC that reads a mobile device's orientation and rotates a game object based on the phone's angle with the PC screen.</p>
  
  <pre><code>void Update () {
      // Set main screen orientation when left trigger is pressed
      if(leftPad.GetTrigger())
          mainScreenOrientation = InputState.orientation;
  
      Quaternion delta = InputState.orientation * Quaternion.Inverse(mainScreenOrientation);
      transform.localRotation = delta;
  }
  </code></pre>
  
  <p>InputState.orientation is the orientation of the phone and is updated every frame. When the user first starts the app, the phone has to be pointed at the screen to register the mainScreenOrientation.</p>
  
  <p>The delta between the current phone and PC screen orientation is calculated and set on the game object's transform.localRotation</p>
  
  <p>All quaternions are defined with y pointing magnetic north, x pointing east and z pointing up
  <img src="https://i.stack.imgur.com/eqfPg.png" alt="enter image description here"></p>
  
  <p>The orientation of the game object is correctly set to identity after the registration but the object seems to be rotating in the wrong axis when the phone is rotated. Any idea what is wrong with this?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Turns out there are a few problems with the code</p>
  
  <ol>
  <li><p>As Jerome mentioned, the orientations had to be remapped as the phone was being used in landscape mode.</p>
  
  <p>Quaternion.Euler(0, 0, 90) * InputState.orientation</p></li>
  <li><p>Unity uses left-hand coordinates while the input read by the sensor is right. Negate z to convert.</p></li>
  </ol>
  
  <p>The last problem I have not solved with this implementation is a sudden jump in rotation past a certain angle. Raising the elevation of the phone in landscape mode above the horizon causes the object to rotate by 90 degrees along the z axis.</p>
  
  <p>Not sure why this is happening as I am dealing only with quaternions and there should not be any discontinuity.</p>