Steam VR: Assigning inputs to OpenVR controllers in Unity

Creating spheres by pressing the trigger button.

Creating spheres by pressing the trigger button.


UNITY 2018.3.0f2 – OPEN VR INPUT

Assigning input to open controllers

Since I started dabbling in VR development I’ve been struggling with the apparently simple operation of assigning actions to VR touch controllers. What is a simple scripting operation for keyboard and mouse, became an exoteric guessing game when applied to VR controllers, one that involves Steam VR accounts, binding profiles and the creation of action sets.

The philosophy behind this convoluted process is to create a product agnostic input system, but I’m still baffled on why I can’t just press a button and assign the script I want for this. To complicate matters, because of the shifting sands of VR development, any software update can cause minor changes that make scripts stop working and trusty tutorials out of date.

Because of this I decided to make this guide on assigning Open VR inputs for the latest Unity version (2018.3). This is not intended to be a tutorial ( I don’t know if I’ll be able to make it work) but as a guide and journal of my process.

Open VR Inputs for Unity 2018.3

  • Downloaded SteamVR v2.2.0 assets from the github page.

    • Installing package: Assets – Import Packages – Custom Package – (steamvr2.2 folder)
  • Setup the scene: Player object, Input and Teleport System

    • Create a plane object.

    • Add a Steam VR Player gameobject (type “player” on the Project tab search bar to find it). Place it anywhere on the scene.

    • Disable the “Main Camera”

    • Activate the Input System: Windows – SteamVR Input – prompt will show up: “yes” -

      • The SteamVR Input windows is where you create actions that will be later assign to buttons on a different screen (I’ll check this later). For now press “save and generate”. This will activate the SteamVR sample commands to get started. Close the screen.
  • Add Teleport system:

    • Duplicate the Plane, rename it (exp “TeleportPlane”) and move it on the Y axis (up) just a little bit (exp: 0.05) – This plane will become the teleport area.

    • Add the SteamVR “Teleporting” game object to the scene – this will control the teleporting action.

    • Go to the copied plane (“TeleportPlane”) and “Add Component” – TeleportArea. – Now the plane will be transparent and limits the teleport area.

    • Press play and test the scene on your headset.

    • ! (For some bizarre reason, the headset wasn’t displaying anything. Again, I spent time looking for any errors until I pulled the classic “turn de pc off and on again” and it worked!).

  • Steam VR Key binding – I want to create a sphere on my hand’s position when I press the trigger button.

    • Window – SteamVR Input

    • Create a new input. Named my “DrawTrigger” and set it to “Boolean”. This means it can only be “on” or “off” true/false. If I wanted to use the analog trigger control in order to detect how much pressure I’m applying to the trigger, I had to choose “Vector1”. “Vector2” is for the analog stick 2d position and “Vector3” is for the controller gyroscope…I think.

    • Save and Generate

    • Go back to the SteamVR Input

    • Click “Open binding UI”. This will open a browser window with your steam account and the binding profile for this specific Unity project.

      • Press “edit” on your browser: It will open the “public binding for oculus touch in (unity project name)”.

      • Here you can assign SteamVR Inputs to specific buttons. It also have several tabs, with different button biding collections, like “platformer”, “buggy”…I’ll keep at the “default” tab, because this is where I created my action in Unity.

      • On “Trigger” press the “+” icon to create a new button assignment. Choose “Button”

      • The new “Button” has two modes: “Click” and “Touch”. I want “Click”

      • Click “none” next to “Click”. Choose the action you just created “drawtrigger” in my case.

      • Save by clicking “Save Personal Binding” at the bottom of the window.

      • Go back to Unity.

  • Code Key Command – With the button assigned, It is time to code what you want it to do. UNITY VERSION ALERT! – Open VR is always evolving, which means the coding seems to change all the time. Most tutorial I looked didn’t work on Unity 2018.3. That’s the main reason I’m doing this tutorial for myself.

    • Create Script – Create your new script. Ideally make a folder for it inside “Assets”. I created a “_Script” folder. I named my script “Draw”. Recapitulating: It will create a sphere where my hand position is. Open the script on your code editor.

    • !!!!1 – Add Valve VR Libraries. Before coding you have to add the libraries:

When you open the script it, the library area on top should look like this:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

Add the following

using Valve.VR;

using Valve.VR.InteractionSystem;
  • !!!!2 – Add Namespace. I banged my head on this one for a while.
    The `public class Draw : MonoBehaviour` must be inside the brackets of a namespace `namespace Valve.VR.InteractionSystem.Sample` like this:
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using Valve.VR;

using Valve.VR.InteractionSystem;

namespace Valve.VR.InteractionSystem.Sample

{

public class Draw : MonoBehaviour

{

}

}
  • Now the script is ready for you to code, otherwise it wouldn’t recognize any of the Steam VR inputs.

  • Calling the buttons and actions. Creating the Steam VR variables

  • Inside `public class Draw : MonoBehaviour` add

namespace Valve.VR.InteractionSystem.Sample

{

public class Draw : MonoBehaviour

{

//Create variables for the SteamVR actions

public SteamVR_Action_Boolean triggerPress;

//Call Rigidbody objects to be created.

public Rigidbody paintObject;

private void Update()

{

//when the left hand trigger button is pressed down, create object at the same
position and rotation of the current parent object.

if (triggerPress.GetStateDown(SteamVR_Input_Sources.LeftHand))

{

Rigidbody paintObjectClone = (Rigidbody)Instantiate(paintObject,
transform.position, transform.rotation);

}

}

}

}
  • Assigning objects to the variables on the Unity Inspector screen

The “public” variables called at the beginning of the scrip must be assigned “by hand” on the Unity Inspector screen. This allows the same script to be used with different objects at different contexts.

    • Create a “Sphere” 3D object and make it a prefab by dragging and dropping it inside the folder you assigned for prefabs. Named mine “_Prefab”. The sphere will be the “paint” created by the “Draw” script when I press the trigger button.
  • Add the “Draw” script to the object you want to control it – In this case SteamVROjbectsLefHand

  • On the “Draw” script component inside the LeftHand object assign the “DrawTrigger” SteamVR action to “Trigger Press” and the sphere prefab to Paint Object – as shown in the animation bellow:

Assigning the variable objects to the script on the Inspector window

Assigning the variable objects to the script on the Inspector window