HTC Vive Tutorial for Unity

Learn how to use the HTC Vive with Unity! Grab and throw objects, shoot lasers and teleport around an area. By Eric Van de Kerckhove.

4.4 (19) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 2 of 5 of this article. Click here to view the first page.

Defining Actions

Since version 2.0 of SteamVR, Valve has replaced the traditional 1:1 hardware mapping of the input system with an action system. This adds a layer of abstraction between the hardware and Unity.
The action system allows you to think of user actions instead of what buttons or triggers need to be polled for input. SteamVR figures out what inputs to use for a given action.

Open the SteamVR Input window by selecting Window > SteamVR Input in the top menu.

The window below appears asking you if you want to copy some example files to the project. Select Yes:

This creates several JSON files in the root of the project:

The actions.json file contains all actions, action sets and references to the default bindings. The other JSON files contain the default bindings for each action. Luckily, you won’t have to edit these manually.

Look at the SteamVR Input window; this contains all of the action sets and actions:

This window contains a few important sections and buttons:

  1. The action sets: the buttons here act like tabs to switch between the sets.
  2. Actions: The default set of actions, both input and output. You can add, edit and delete the actions here.
  3. This button saves all action sets and actions to JSON and generates lots of helper classes for easy access to the actions.
  4. The button opens a locally hosted web page that allows you to bind the actions to hardware.

You can define multiple actions sets here, each containing a list of actions. You can categorize every input action as one of the following types:

  • Boolean: Actions that are either true or false, on or off. Example use: grab.
  • Single / Vector1: Actions that have a value between 0 and 1. Example use: movement speed.
  • Vector2: Actions that have an X and Y value between 0 and 1. Example use: direction.
  • Vector3: Actions that have an X, Y and Z value between 0 and 1. These actions are very uncommon.
  • Pose: Actions that represent a position and rotation in 3D space. These actions are used to track VR controllers and the HMD.
  • Skeleton: Actions that use the SteamVR Skeleton Input system to estimate where the player’s fingers are when holding the VR controller. This provides joint positions and rotations for every finger, regardless of the controller’s tracking fidelity.

Lastly, there’s also a single output action: vibration. You can use this action to make a controller vibrate.

Time to edit the actions!

Click platformer button and the minus button next to the mirrored drop-down to delete the platformer action set:

When the confirmation window appears, select Delete.

Do the same for the buggy action set; you only need the default action set for this tutorial.

Next, clean up the actions list by deleting the following actions. Select them one-by-one and click the minus button at the bottom right:

  • InteractUI
  • GrabPinch
  • Pose
  • Squeeze

Next, select the GrabGrip action and rename it to Grab by editing its Name field:

With all of the actions created, click Save and generate on the bottom-left to save the actions and generate a bunch of helper scripts and scriptable objects for easy access.

After a short while, the input system finishes and a new SteamVR_Input folder is added to the Assets folder:

This folder contains the files created by the generator. More specifically, scripts and scriptable objects.

Binding Actions

Now that you defined the actions, you can create the default hardware bindings for them.
Make sure SteamVR is running and your Vive’s controllers are turned on. Next, click Open binding UI on the bottom-right. A locally hosted web page opens in your default browser:

To start, if you have a gamepad or other compatible hardware connected, you’ll need to switch to the Vive Controller binding first. Click Gamepad, for example, and select Vive Controller to switch to the controller setup.

Next, below the Current Binding header, click Edit to enter the binding menu.

The binding editor now shows up on screen. Take a quick look around. You can visually bind the actions to the hardware here.
Thanks to mirroring, you only need to set up the left controller, and those settings will copy over to the other side. You can change this by unchecking the Mirror Mode checkbox at the bottom of the screen. Leave it mirrored for the sake of this tutorial though.

If you look at the left side of the screen and scroll down, you’ll notice there’s already some inputs linked to actions:

To start with a clean slate, hover over every defined input and click the trashcan icon to delete it. When a confirmation prompt appears, select Delete.

This is the result:

Now, define a new Button binding for the Grip by clicking the big plus button next to its name and selecting BUTTON in the window that pops up.

This lets you use the grip like a button (which it is). Now, click on None next to the Click event to link an action to it. A window pops up with a list of possible actions. Choose grab to link it.

That’s it when it comes to defining new inputs! Now, every time you press the grip button when the game is running, the grab action gets triggered. Pretty neat, right?

Click Apply on the bottom-left of the input entry to save it to the action set.

Now do the same for the Trigger and Trackpad: Create a new Button input for each, link the Trigger’s Click event to grab and the Trackpad’s Click event to Teleport. Be sure to save these when you’re done.

The result looks like this:

That’s it! The bindings are set up. Click Replace Default Binding to apply these settings to the default configuration file.

In the next window, click Save on the lower-left to save the default binding to disk.
When players start your game, this binding gets applied. If there are no bindings for the player’s specific hardware, they’ll be prompted to set these up themselves.

Close the Binding UI tab or window, return to the Unity editor and close the SteamVR Input window. Time to put this binding to good use!