Htc Vive Tutorial Setup
Circuit Stream
Welcome to Section One of the HTC Vive Unity Virtual Reality Development Tutorial. The goal of this section is to get you up and running with Unity and the Vive so you can begin developing virtual reality apps. At the end of this tutorial, you’ll have a scene setup you can use with the Vive!
Before getting started, make sure you have the following:
- An HTC Vive Virtual Reality Headset
- A VR Ready Laptop or Computer
- Unity 5.5 or newer installed (the free personal edition)
- Steam and SteamVR Installed. For installing Steam and SteamVR visit this guide A basic understanding of Unity A basic understanding of scripting in Unity
If you're new to Unity, visit our courses page for Unity training and virtual reality classes.
Open the provided Unity Project
If you would like access to the project we're referencing so you can more easily follow along, you can download it here. Unzip the project and place it where you can find it. Then, in the next step, instead of creating a new project, simply use the 'Open' button. It will open a browsing prompt similar to the one pictured below. Navigate to your project folder that you unzipped, and click 'Select Folder'.
Create a new Project
If you're starting from scratch and don't want to follow along in the provided project (which is totally fine), start by creating a new project.
Import SteamVR
If you are working with in the provided project and did not start your own, you can skip this step! Otherwise, open up the asset store window in Unity by selecting Window > Asset Store and search for SteamVR. Click the Download button. Once it's finished downloading, you will see the Import Unity Package window. In the bottom right corner click Import and then click Accept All.
Once SteamVR has finished importing you will have a new folder named SteamVR in your Project tab.
New Scene
Regardless of whether you're starting a new project or using the existing one, you'll want your own scene to work in. Create and Save a scene for your VR Work. If you are starting fresh, I suggest creating a Scenes folder in in your project window for storing your scene files.
Set up the VR Camera Rig
Drag and Drop the [CameraRig] located in the SteamVR/Prefabs folder into your scene.
Delete or Disable ‘MainCamera’
You can’t have two conflicting cameras in the scene, so delete or disable the default ‘Main Camera’ in your scene.
Test
Make sure SteamVR is running (the application, not the plugin. I know it’s confusing).
If it is, you should be able to press ‘Play’ in your scene, and test out if the camera and hands are hooked up correctly. Nothing will be in your scene, so it might be weird, but you should be able to tell if the camera is working.
Camera Rig Overview
Now that we have the camera rig in our scene, let’s look at what options we have. There are a lot of things you can dive into, but for simplicity’s sake, let’s just look at the Steam VR_Play Area component on the [CameraRig] GameObject.
- Border Thickness is how thick you want the border around your playspace to be. Note that the border is NOT the floor space outlined by the player for their Vive, but a square determined by the ‘Size’ further below.
- Wireframe Height is how tall you want the wireframes around your playspace to be. This is not the wireframe that shows up in game to warn you of your boundaries, but a gizmo drawn in the scene view around your playspace. Does not show up in game.
- Draw Wireframe When Selected Only will make it so that the wires previously discussed will only show up when the camera is selected
- Draw In Game sets whether you can see the border in game. Wireframe does not show, even when this is checked.
- Size dictates the size of the border. Options are: 200x150, 300x225, and 400x300. Default is 300x225. Note that If you make your play space too big, you risk someone with a small play space not being able to reach things you put at the edge of your border.
- Color is the color of your border.
All of these things are guidelines for you to set up your scene with, and possibly your player to use (though most games just stick with the Vive default wire boundary that keeps you in your play space). It does not change the boundaries the player set when they set up their Vive.
Script Access Overview
The Vive headset and controllers, like any GameObject, can be accessed and updated via scripts. There are many components attached to the various GameObjects in the [CameraRig] prefab (which contains the GameObjects that will be tracking your headset and controllers), but there are only a few that you’ll be interacting with in the beginning.
Accessing Information on the Controllers
There is a class called SteamVR_Controller.Device class. This class holds a lot of information we need, such as which buttons are currently being pressed on our controller. In order to access the instance of this class associated with our controller, we need to use the SteamVR_Controller.Input() method, which will return to us the instance of SteamVR_Controller.Device that we need. However, this method requires a device index. The device index is stored in a component called SteamVR_TrackedObject, which is attached to both of the GameObjects representing our controllers (named Controller (left) and Controller (right)). So, if we want to access that Device class, we need to get the index from the component, pass it to the SteamVR_Controller.Input() method, and store the result.
- Get reference to the SteamVR_TrackedObject Component First, we need a reference to the SteamVR_TrackedObject component. Since the script we're creating will live on the controller GameObject, which we know also contains the SteamVR_TrackedObject component, we can simply use GetComponent method on awake to get our reference.
//Controller References protected SteamVR_TrackedObject trackedObj; void Awake() { trackedObj = GetComponent(); }
- When Device is asked for, get reference from SteamVR_Controller Class When we want input information, we call SteamVR_Controller.Input() and pass in our controller's device index, located in the SteamVR_TrackedObject component. To make this simpler to access, we can create a property that, on get, calls that method.
//Controller References protected SteamVR_TrackedObject trackedObj; public SteamVR_Controller.Device device { get { return SteamVR_Controller.Input((int)trackedObj.index); } } void Awake() { trackedObj = GetComponent(); }
Information About Properties and Button States
If you’re unfamiliar with properties, we like to think of them as a combination of method and variable. You use them just like a variable, but they’re actually a special kind of method called an accessor. So instead of directly manipulating the value contained in the variable, you can give these guys specific instructions for what they should do when anyone “gets” them (asks for their value) or “sets” them (assigns a value to them). So, in this case, instead of storing a value in device directly, we’re having it call Input method and returning the result any time we use the device property.
You can get information about button states using methods in the Device class:
- Device.GetPressDown(), Device.GetPressUp(), and device.GetPress() will return a bool letting us know if the button given is being pressed.
- Device.GetTouchDown(), Device.GetTouchUp(), and Device.GetTouch() will return a bool letting us know if the button given is being touched. This is similar to GetPress in some cases, for example the trigger, GetTouch will return true before the trigger button is all the way down.
- Device.GetAxis() returns the location of the user’s thumb on the touchpad (zero by default).
- Device.Velocity and Device.AngularVelocity are properties that the device has denoting the controller’s current velocity and angular velocity.
- Device.TriggerhapticPulse() can be used to send haptic feedback to the controllers.
It's okay if you don’t remember all these. We’re going to be using most of them later on, so you’ll get to see how they work!
That completes Section One of our virtual reality Unity tutorial for the HTC Vive! Visit the next post to learn about Pickup Interactions!
XR Development Course
Upskill in 10 weeks and get to developing your own AR and VR applications.