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!
Before you dive into this tutorial, make sure you have the following:
- A VR-ready Windows PC.
- A copy of Unity 2018.3 or newer installed on your machine.
- An HTC Vive or HTC Vive Pro with all of the hardware installed and software configured and updated.
- Steam and SteamVR installed. See this guide, which walks you through hardware set up and provides links to download Steam and SteamVR.
- Basic Unity knowledge: Introduction to Unity: Getting Started.
- A basic understanding of scripting: Introduction To Unity Scripting.
- An HTC Vive that’s powered on and connected.
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:
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:
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:
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:
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.
Select both [CameraRig] and [SteamVR] and drag them to the Hierarchy:
[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:
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:
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.
Take one of the controllers in your hand and give it a proper look. Each controller has the following inputs:
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.