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

unity-htc-vive

Note: Eric Van de Kerckhove updated this tutorial for Unity 2018.3 and SteamVR 2.0.

The HTC Vive is a virtual reality headset developed by HTC and Valve Corporation. It lets you step into virtual worlds and experience it as yourself instead of through an on-screen avatar.

If you’re a Unity developer, making virtual reality games with the HTC Vive into your own games is easy — you might even say the HTC Vive and Unity are a match made in heaven.

In this HTC Vive Tutorial, you’ll learn how to integrate the HTC Vive into your own Unity games. Specifically, you’ll learn how to:

  • Download and configure SteamVR.
  • Handle the controller’s input.
  • Interact with physics objects in VR.
  • Make a laser pointer.
  • Teleport around an area.

At the end of this HTC Vive tutorial, you’ll have a sandbox that’s primed for further experimentation. It’s time to get started!

Note: Everyone reacts differently to movement and rotation while wearing a head-mounted display. If it’s your first time, go easy on yourself and take a break if you feel nauseous or uncomfortable. Most people get used to VR quickly. Don’t fret if you’re not feeling well the first few times — it’ll probably pass.

Getting Started

Before you dive into this tutorial, make sure you have the following:

Download the materials for the project using the Download Materials button at the top or bottom of this tutorial, unzip it somewhere and open the starter folder inside Unity. Look at the contents of RW in the Project window:

AssetFolders

Each folder serves as home base for specific assets:

  • Materials: Materials used for the scene including the blue bouncy balls.
  • Models: All the models.
  • Physics Materials: Bouncy ball physics material.
  • Prefabs: Loose object prefabs.
  • Scenes: The game scene is in here.
  • Scripts: All the scripts.
  • Textures: The single texture shared by all objects in the scene.

Open the Game scene. Look at the scene view and click play to try the game:

FirstSceneView

At the moment, not much is happening because there’s no VR rig in the scene yet. You’ll need to add SteamVR to the project to connect the Vive to Unity.

Setting Up SteamVR

The SteamVR SDK is an official library made by Valve that makes it easier to develop for all major VR headsets. It’s currently free on the Asset Store and supports the Oculus Rift and the HTC Vive to name a few.

Open the Asset Store by selecting Window > General > Asset Store in the top bar:

Once the store loads, type SteamVR in the search field at the top and click Enter. Scroll down a bit to see a selection of assets. Click SteamVR Plugin to open its store page:

Click Download and give it a moment. Once it’s done, click Import to open the package import dialog. Click Import at the bottom-right corner of this window to import the package:

ImportPackage

ApiUpdate

Click the button that reads I Made a Backup to let the editor update and recompile the scripts.

Note: At the end of the import, you may see the following message depending on your version of Unity:

ApiUpdate

Click the button that reads I Made a Backup to let the editor update and recompile the scripts.

You’ll get this window after a few seconds:

SteamVrSettings

This is part of the SteamVR plugin; it shows which editor settings you can improve to maximize performance and compatibility.

When you open a fresh project and import SteamVR, you’ll see quite a few entries. Since the starter project is already well optimized, the only recommendation here is to disable the resolution dialog.

Click Accept All to perform all of the recommended changes. Close the Asset Store and switch back to the Scene view. You’ll now have a new folder named SteamVR in your Project window:

Open the SteamVR folder and look at the folders inside. You’ll add some essential VR GameObjects from the Prefabs folder to the scene next.

SteamVrPrefabsFolder

Select both [CameraRig] and [SteamVR] and drag them to the Hierarchy:

DragVRPrefabs

[SteamVR] has a Steam VR_Render component attached that handles the rendering of all VR cameras.

[CameraRig] is more interesting because it controls the Vive’s headset and controllers. Select [CameraRig] and set its Position to (X:0, Y:0, Z:-1.1) in the Inspector to slide the whole rig just behind the table.

Delete the Main Camera from the Hierarchy because it’ll interfere with [CameraRig] and its embedded camera.

Put your HMD in reach, turn on the controllers and start the scene. Take both controllers and swing them around a bit. You’ll notice that you see the virtual controllers waving around in the Scene view:

ControllerMoveScene

Once the SteamVR plugin detects the controllers, it creates these virtual versions. The controllers are mapped to the two Controller children of [CameraRig]:

Now, while still running the scene, select Camera in the Hierarchy and carefully pick up your head-mounted display by the top strap. Move and rotate it a bit and watch the Scene view:

HeadMove

Camera is linked to the head-mounted display and precisely tracks every real-life movement and rotation.

Now, put the head-mounted display on your head, grab the controllers and look and walk around a bit to get a feel for the room.

If you try interacting with the objects, you’ll be disappointed because nothing happens. To add functionality beyond movement tracking, you need to do some scripting.

Handling Input

Take one of the controllers in your hand and give it a proper look. Each controller has the following inputs:

ViveControllerButtons

Besides the inputs above, each controller also has a velocity and rotational velocity as you move and rotate them; this proves especially handy when interacting with physics objects.