Skip to content

2. Tracking Head Movements

Gerald S edited this page May 20, 2017 · 1 revision

“Roll-a-ball” in its current state requires controller input from a computer’s keyboard. While the HoloLens includes keyboard support, the chosen approach to move the ball in the game was to incorporate head tracking. When a user wears the HoloLens and runs “Roll-a-ball”, moving one’s head will cause the ball to move along the plane.

To achieve this, the Update() method of the PlayerController script for the ball was modified. With the guidance of “Chapter 2 - Gaze” from the “Holograms 101” tutorial, it was identified that the mechanics of a gaze can be applied to moving the ball in the game.3 The changes made involved consuming the head movements as inputs and then applying them as a force on the ball to move it. The resulting code:

   void Update()
   {
        // Do a raycast into the world that will only hit the Spatial Mapping mesh.
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
        {
            // Move the ball through gaze
            float moveHort = hitInfo.point.x;
            float moveVert = hitInfo.point.z;
            Vector3 movement = new Vector3(moveHort, 0.0f, moveVert);
            rigBod.AddForce(movement * speed);
        }
    }
Clone this wiki locally