VR Gaze Input

Rushil Reddy
7/4/2016


A popular format for menu selection as well as in game actions is gaze input(or look based-input)

If you are using SteamVR Import the camera rig prefab and add a new script called GazeManager to the Camera Rig>Camera(head)>Camera(eye)

If you are developing for the Oculus add a new script called GazeManager to the Main Camera.

using UnityEngine;

using System.Collections;

public class GazeManager : MonoBehaviour {

      public float sightlength =100.0f;

      public GameObject selectedObj;

     void FixedUpdate()

     {

            RaycastHit seen;

            Ray raydirection = new Ray(transform.position, transform.forward);

            if (Physics.Raycast(raydirection, out seen, sightlength)){

                 if (seen.collider.tag == "Ground") {

                       if (selectedObj != null) {

                             if (seen.transform.gameObject.GetComponent()) {

                                   if (selectedObj.GetComponent() != seen.transform.gameObject.GetComponent()) {

                                         selectedObj.GetComponent().hideLines();

                                   }

                             }

                       }

                       selectedObj = seen.transform.gameObject;

                       if (selectedObj) {

                             selectedObj.GetComponent().showLines();

                       }

                  }            

else if (seen.collider.tag == "UI") {

                             Debug.Log("Hello");

                       }

                  }

           }

     }

The only things you need for this script are a corresponding draw script on any object you wish to highlight.

To make the highlight, save the object you want to select as a prefab with a child game object consisting of four white quads to give the appearance of selection.

using UnityEngine;

using System.Collections;

public class Draw : MonoBehaviour {

     public GameObject lines;

     public void showLines()      {

           lines.SetActive(true);

     }

      public void hideLines()      {

            lines.SetActive(false);

     }

}

Easy enough. We’ll cover an implementation of this in a menu system in a later tutorial.