Gaze in Unity

Gaze is the first input mechanism on HoloLens. It's the first "G" in Gaze, Gesture, and Voice input models on HoloLens. However, there is no explicit API or Component exposing Gaze in Unity.

Implementing Gaze

Conceptually, Gaze is implemented by projecting a ray from the user's head where the HoloLens 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 gaze ray collided with.

Example: Implement 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 Gaze target, it is recommended to do this in a single object managing gaze instead of doing this in any object that is potentially interested in the object being gazed at.

Visualizing Gaze

Just like on a personal computer you use a mouse pointer to target and interact with content, you can implement a cursor in HoloLens that represents the user's gaze.

Example: Visualize Gaze

You can use the GazeManager.cs from the HoloToolkit-Unity repository on GitHub to see a more detailed example with assets. Cursor.prefab and CursorWithFeedback.prefab shows you how to visualize your Gaze using Cursors.

See also