Virtual Reality Interactions with SteamVR

Kristin Dragos — Artist
4 min readOct 7, 2018

--

Recently, I posted about the changes to SteamVR and their teleportation system. Their interaction system has changed quite a bit as well. Previously, we needed to write some code to track our controllers and the button presses, touch inputs, etc. Something like this:

Recent changes to the SteamVR plugin have removed the highlighted classes entirely. The result is a layer of abstraction making some of the code we used to write unnecessary. Let’s take a look at a couple of examples of how it’s used.

New Button Binding UI

The SteamVR Input window is where the button inputs and actions will be defined. The defaults seem appropriate for basic VR interactions like teleporting, grabbing, interacting with UI, etc. If you wanted to change or remove any of these, you’d have to go to the new button binding UI. To do so, go to the SteamVR Input window (Window > SteamVR Input) and then click “Open Binding UI” at the bottom. This will open up a web browser to this fancy little UI.

From here, you can change any buttons you’d like. If you aren’t sure what you will need, I recommend just sticking with the defaults for now.

Grabbing and Throwing Using the Provided Scripts

Implementing grabbing and throwing mechanics is really simple with the recent changes.

  1. Drag the “Player” prefab into the scene.
  2. Create an object you want to be interactable.
  3. Add the “Interactable” component to it.
  4. Add the “Throwable” component.

When you play the scene and you’re using the default button mapping, use the grip buttons (Vive) or hand trigger (Rift) to pick up and release to throw the object.

The Interactable component doesn’t do much. It seems to function more like a tag or label than anything else. The Throwable component is what enables users to actually pick up and move the object around.

Coding to Button Presses

At some point, you’ll likely want to add some code that runs when some buttons are pressed. In your custom scripts, you’ll want to define which button you’re listening for. For this example, I was using the Teleport button that’s in the default UI mappings.

Now, you can simply get the state of the button. You can use methods like GetState(), GetStateDown(), or GetStateUp(). This is similar to the device.GetPressUp and device.GetPressDown methods in the older way of doing things.

Custom Button Actions and Inputs

I wanted to know how to go about adding custom inputs and actions with the new system. I put together this little game of air hockey. My goal was to make it so that moving the thumb stick would move the paddle left or right.

The first step was to add an action that can be mapped to a button. In the SteamVR Input window, I added a “MovePaddle” action by pressing the little plus sign. This essentially sets up an action that I can use in the button mapping UI. I set the type to be a Vector2 because I wanted to use the x axis to move the paddle. Be sure to click “Save and Generate” at the bottom to save your changes.

Next, I opened the binding UI. Scroll down to the Joystick as that’s the button I wanted to map. Click the plus sign to add a button mapping. I chose Joystick. This sets up the button mapping, so all that’s left is to set the action to the button. Click the pen icon to edit the “Use as Joystick” mapping. For position, click on where it says “None”. This will open up a UI that shows all the available options for what can be done with that button. Select the new action we set up previously.

Now that we have our action and button mapping setup, all that’s left is the code to actually run when that event or action is happening.

In my script, I set a variable to hold the action that I want to listen for. (Lines 9 and 10) Then, I am able to read that data from the controllers on lines 20 and 21.

In the inspector, there will be a drop down with a list of the actions that have been defined.

I hope this helps explain some of the changes and makes it easier to get started with SteamVR.

--

--

Kristin Dragos — Artist

I'm a self taught artist. www.paintpourium.com Previously a software engineer and teacher, finally starting to figure out what I want to be when I grow up.