SteamVR: Controller Input in 5 Minutes

Rushil Reddy
7/20/2016

If you’ve had trouble figuring out the difference between the various forms of input in SteamVR such as EVRButtonId’s, ButtonMasks, TrackedControllers, and TrackedObjects this guide will show you which to use in every situation you come across.

We are going to learn how to receive input from the Vive Controllers and taking action with this input. We will be covering all aspects of the controller including the trigger, grip, touchpad, and haptic feedback.

In the SteamVR heiarchy there are three types of Components. TrackedObjects, Controllers, and TrackedControllers. For basic input you only need to know about TrackedControllers.

Add a SteamVR_TrackedController script to your Controller(left) or Controller (right). Now add a new script to your controller and call it ControllerManager:

private SteamVR_TrackedController device;

void Start() {

     device = GetComponent<SteamVR_TrackedController>();

     device.TriggerClicked += Trigger;

}

void Trigger(object sender, ClickedEventArgs e)

{

     Debug.Log("Trigger has been pressed");

}

We just need to access the event handler for the actual button press and add on our new method to be called whenever the event fires(in this case when the user pushes the trigger). This method will work for all basic forms of input but what if we want to get specific information on touchpad input?

private SteamVR_TrackedObject trackedObject;

private SteamVR_Controller.Device device;

void Start(){

     trackedObject = GetComponent<SteamVR_TrackedObject>();

}

void Update(){

     device = SteamVR_Controller.Input((int)trackedObject.index);

     if(device.GetAxis().x != 0 || device.GetAxis().y != 0){

            Debug.Log(device.GetAxis().x + " " + device.GetAxis().y);

     }

}

}

Here, instead of using a TrackedController component you can see that we are using the default trackedObject component. The difference here is that TrackedObject is more abstracted and is meant for use on any object that is tracked by the lighthouse including the HMD. We use it to get an integer representation of our controller which we then assign to SteamVR_Controller.Device to get some controller specific methods for our usage. This way of getting input allows us more latitude in terms of what we can actually get out of our controller at the expense of being hard to read. If we wanted to get trigger input with this same method here is what we would have to do.

void Update(){

     device = SteamVR_Controller.Input((int)trackedObject.index);

     if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)){

            Debug.Log(“Trigger Pressed”);

}

}

Next, we will cover how to map the grip button in addition to adding haptic feedback on press.

private ushort pulsePower = 700;

private ulong gripButton = SteamVR_Controller.ButtonMask.Grip;

//The following should be in your Update() function under our previous code

if(device.GetPress(gripButton))

     {

           device.TriggerHapticPulse (pulsePower);

     }

The last method of getting input for those so inclined would be to get references to EVRButtonID’s such as in the following example:

private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;

We hope that this has been a useful primer on getting controller input using SteamVR. We will be covering ButtonMasks and EVRButtonID’s in more detail in future tutorials so be sure to check back soon.