Head-gaze in Unity

Gaze is a primary way for users to target the holograms your app creates in Mixed Reality.

Implementing head-gaze

Conceptually, head-gaze is implemented by projecting a ray from the user's head where the headset is, in the forward direction they are facing and determining what that ray collides with. In Unity, the user's head position and direction are exposed through the Unity Main Camera, specifically UnityEngine.Camera.main.transform.forward and UnityEngine.Camera.main.transform.position.

Calling Physics.RayCast results in a RaycastHit structure which contains information about the collision including the 3D point where collision occurred and the other GameObject the head-gaze ray collided with.

Example: Implement head-gaze

void Update()
       RaycastHit hitInfo;
       if (Physics.Raycast(
               out hitInfo,
           // If the Raycast has succeeded and hit a hologram
           // hitInfo's point represents the position being gazed at
           // hitInfo's collider GameObject represents the hologram being gazed at

Best practices

While the example above demonstrates how to do a single raycast in an update loop to find the target the user's head points at, it is recommended to do this in a single object managing head-gaze instead of doing this in any object that is potentially interested in the object being gazed at. This lets your app save processing by doing just one head-gaze raycast each frame.

Visualizing head-gaze

Just like on the desktop where you use a mouse pointer to target and interact with content, you should implement a cursor that represents the user's head-gaze. This gives the user confidence in what they're about to interact with.

Head-gaze in the Mixed Reality Toolkit v2

You can access head-gaze from the Input Manager in MRTK v2.

See also