The HTC Vive has remarkable immersion and tracking capabilities, but before you start programming specific functions to your controllers, there’s some required code you need to include. In this post, we’ll go over this necessary code and explain step-by-step how to get a shortcut setup for assigning controller and button functions for Vive.

Download our 10-week VR Development with Unity Course Syllabus


 

Skip to the Technical Steps

 

Load in SteamVR Plugin

 
In case you haven’t done this step, here’s how to load your controllers as something you can program in Unity (We cover getting set up to start making Vive apps in more detail in this article here). You’ll need to download the SteamVR plugin from the Asset store in Unity. Once you have this plugin set up, you can load in the CameraRig and SteamVR prefabs and access your controllers directly! Just make sure to delete the MainCamera from your hierarchy after loading in the CameraRig.

Write Behaviour-Specific Code

 
To get started creating with the controller, you’ll need to write some code. Afterward, you’ll be able to assign specific functions to each button and allow you to label the controller as simply “Controller” in your script. Basically, this will create a shortcut when it comes to assigning functions to buttons.

First, let’s head back to your Assets folders (where you’d find SteamVR, for example). Then, right-click in the grey area and go to Create > C# Script

You’ll name this new code behaviour something specific. Double-click it, and Visual Studio Code will open.

From here, you’ll be able to start coding functions! For anything dealing with the controllers, you’ll want to start with:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ViveGrab : MonoBehaviour {

where ViveGrab is the name of your script (make sure these match!).

Enable Assigning Functions to Controller

 
From here in your script, you’ll include:

     private SteamVR_TrackedObject trackedObj;
     private SteamVR_Controller.Device Controller
    {
        get
        {
                return SteamVR_Controller.Input((int)trackedObj.index);
        }
    }
     void Awake()
    {
        trackedObj = GetComponent();
    }

You don’t need to have any original functions here — this just primes your script to assign functions to your controller.

Apply Your Script to Controller

 
Now let’s add this script to your controllers. To do this, you’ll need to exit Visual Studio Code and head back into Unity. Select your controllers from the hierarchy tab, and drag your script to the left to be added as a component. Make sure to add it to both controllers.

That’s it! You’ve finished setting up a shortcut for assigning functions to your Vive controller.

Now when you want to input a function when you pull the trigger button, all you have to do is open up your script again and write:

     Controller.GetHairTriggerDown()

Or, for a function when you squeeze the grip buttons:

     Controller.GetPressDown (SteamVR_Controller.ButtonMask.Grip)

Or, when pressing down the touchpad by your thumb:

     Controller.GetPress (SteamVR_Controller.ButtonMask.Touchpad)

This is the base that your Vive controller scripts should begin with. We have other articles covering what script to include for grabbing objects or throwing objects, but you’re welcome to build from scratch from this point! Whatever script you write, just make sure to apply it to both SteamVR controllers in Unity.

You’re one step closer to creating for VR, and we can help you make that giant leap from beginner to expert in building virtual worlds through our 10-week VR course. We cover how to build for the HTC Vive and other VR/AR technologies. Check out our VR course syllabus for more information!

Download VR Course Syllabus
 

Technical Steps:

 

//==Required code to use Vive Controllers==
     private SteamVR_TrackedObject trackedObj;
     private SteamVR_Controller.Device Controller
    {
        get
        {
                return SteamVR_Controller.Input((int)trackedObj.index);
        }
    }
     void Awake()
    {
        trackedObj = GetComponent();
    }