Search code examples
c#androidunity-game-enginegoogle-cardboard

How can I find the rotation of the head in Google Cardboard Unity3D?


Hey guys how do I find the rotation of the phone with Google Cardboard SDK in Unity3D, like the way the persons head is facing? I need to find if it is facing more towards the east, the west, or the north. Do I find the rotation of the head, or the parent Main camera?


Solution

  • The Cardboard class contains a property called HeadRotation which is a Quaternion.

    Quaternion crtRot = youCardboardController.HeadRotation;
    

    To use its rotation like you'd in Unity with directional vectors you may simply multiply it by Vector3.forward.

    Vector3 lookDir = crtRot * Vector3.forward;
    

    Visualizing the vector in editor might help narrow down issues

    void Update () {
      // ..
      Debug.DrawRay( pos, lookDir * 100, Color.blue );
      // ..
    }
    

    From here you only need to know where North is, in meaning of the facing vector. Do a Vector3.Angle() and you have the angle between your avatar's and North's vector. You might want to evaluate both vectors with their y axes set to 0.

    You could also use Vector3.Dot() (the dot product) to determine how much both vectors look into the same direction.

    I had an issue with HeadPosition, which wasn't updated properly. So if you operate from HeadPosition keep in mind it may stick to 0,0,0 .