The initial setup

To my surprise both SteamVR and VRTK are picky about what Unity version you use when trying to set everything up. Both kits will work on the latest stable Unity release and support several stable releases that came out before the current one. Using a beta release is a crapshoot however. I initially tried to use Unity 5.6.0b3, but I had to go back to 5.5.1f3 in order to get the Vive controllers working at all.

Both VRTK and SteamVR can be found in the Asset Store. Simply create a blank project and import them and you should be ready to start.

Sorry for the mouse pointer.

After importing the SteamVR package this window will most likely show up, asking you to use its recommended project settings. You can choose which settings you want to switch, or accept/decline them all. I recommend you follow what this window says by clicking accept all.

With the two assets imported, head into the Scene view, delete Main Camera, and add a [CameraRig] prefab to the scene. This is in the SteamVR/Prefabs folder (alternatively, you can search for it in the asset window).

The current scene hierarchy

The CameraRig prefab contains all you need to get the Vive working in a scene. The base [CameraRig] object contains scripts for managing the controllers and setting up the Play Area. The two Controller objects contain scripts that let SteamVR know to track their positions in the game. The Controllers also hold a Model object that allows the player to see virtual controllers in game. You will now be able to hit play in the editor and look around using the Vive headset.

Adding VRTK to the mix

Create a new GameObject at the root of the scene hierarchy and name it VRTK. Select the VRTK object and give it the VRTK_SDK Manager component to it.

VRTK_SDK Manager is needed to add VRTK to the game.

VRTK needs an object in the scene to have this script in order for it to start tracking the VR headset and controllers. The script needs to know what VR SDK is being used before it can do so, however. In the SDK Selection part of the script, select SteamVR for the system, boundaries, headset, and controller SDK options. Then click “Auto Populate Linked Objects” and VRTK should fill up every GameObject field aside from Script Alias Left/Right Controller.

VRTK’s controller inputs allows you to organize your controller input scripts in a number of different ways. You can attach the input scripts in one of 3 ways:

Attach the input scripts to the left and right controller objects located in the CameraRig prefab, Attach the input scripts to a controller alias object, and then specify these objects in the VRTK_SDK Manager script, or Place the input scripts on any GameObject you want, and give them a reference to a GameObject with a VRTK_Controller Events script(!!!)

That third point is important. When the scene is loaded VRTK will look at the “Actual Left/Right Controller” field in the scene’s VRTK_SDK Manager script and will start setting itself up internally. For a script using VRTK’s input system to work it needs to reference a VRTK_Controller Events script in order for it to begin listening for input events. All 3 options are functionally the same; their differences lie solely in script organization. For option 1), the actual left and right controller objects need to have VRTK’s controller event script; for option 2), the controller alias object needs to have it. If an input script is added to an object with the controller event script then you can simply call GetComponent() to add event listeners, otherwise you will need to give it an object with one through the Unity Editor. For the sake of this tutorial I’ll be handling input through option 2).

Add 3 GameObjects as children to the VRTK object. Name them LeftController, RightController, and PlayArea. Give the VRTK_SDK Manager script a reference to the left and right controller objects we just made. In both controller objects add the VRTK_SDKManager component.

Testing controller input

Now let’s test input using VRTK’s input event system. I created a script called ControllerInputTest and used this script to test it out:

How the LeftController object should look at this point

using UnityEngine;

using VRTK;

public class ControllerInputTest : MonoBehaviour { // Use this for initialization

void Start ()

{

if(GetComponent<VRTK_ControllerEvents>() == null)

{

Debug.LogError("Need to have VRTK_ControllerEvents component attached to the controller");

return;

}

GetComponent<VRTK_ControllerEvents>().AliasMenuOn += new ControllerInteractionEventHandler(TestInput);

} private void TestInput(object sender, ControllerInteractionEventArgs e)

{

Debug.Log("Seems to be working!");

}

}

Let’s look at each part of this script to see what’s going on.

if(GetComponent<VRTK_ControllerEvents>() == null)

As I said earlier, any VR input scripts need a reference to a VRTK_ControllerEvents component in order to begin listening for inputs. Since I’m handling input through an alias controller object I make sure that this script is only attached to an object with the controller events component. For good measure I include this line in to make sure that the script is getting the component it needs.

GetComponent<VRTK_ControllerEvents>().AliasMenuOn += new ControllerInteractionEventHandler(TestInput);

Now that I know for sure that the script is attached to an object with a controller events component I can begin to add an event listener. In this case I make my TestInput function listen to the AliasMenuOn event, which VRTK will fire when the “Menu” button is pressed. Which button on the Vive controller is the menu button, you ask? It’s whatever this is:

One useful feature about VRTK_ControllerEvents is that it supports alias for button input, similar to Unity’s input handling. Instead of hardcoding actions to set buttons I can instead code actions to an alias, the exact button of which can be set in either the editor or at runtime. Looking at this component we can see that “Menu” is currently referring to “Button One”, which is the button that is directly above the touchpad on the Vive controller.

private void TestInput(object sender, ControllerInteractionEventArgs e)

Functions that are listening for an event need to accept the object that sent the event and the arguments related to the event. This is more of a C# feature regarding events so I won’t go farther than this explanation. In the TestInput function I simply call Debug.Log to report that the event was fired correctly.

That should give you a brief introduction to getting started with VRTK inputs. Inputs aren’t too hard but the complexity of both VRTK and SteamVR made it confusing for me to get started. The creators of VRTK have an excellent YouTube channel that goes over the features provided in the toolkit, such as teleporting, grabbing objects, and getting the controllers to interact with UI components.