Scripted controller input using Unity’s SteamVR Plugin 2.0
Premise
–You have a SteamVR plugin.
–The VR headset is connected.
Steps to get the input of the SteamVR controller in a script
To get input from the VR controller, follow the steps below.
- Create an Action Set
- Set Binding
- Create a script
- Adjust Inspector to match the script
1. Create an Action Set
First, create a list of controller operations to be acquired.
1.1 Open the SteamVR Input page
It can be opened with Window> SteamVR Input.
1.2 Create a new Action Set
Press [+] to the right of Action Sets
Create a suitable name.
1.3 Set Action
Press [+] in the In column of Actions and press
Enter the Input you want to get.
Most of the types are boolean and vector2.
boolean
is a menu button, trigger, etc.
vector2
is used with analog sticks.
Figure 1: Example of Action Set after setting
1.4 Save your settings
Press Save and generate
to save your settings.
2. Set Binding
2.1 Open the Binding settings screen
Press ʻOpen binding UI` at the bottom right of the SteamVR Input screen.
A new Widow will open, so press Edit under Current Binding.
Figure 2: Binding screen that opens when you press the Open binding UI
2.2 Switch to the Action Set tab to set
The controller screen will appear.
Open the tab with the name set in 1.2 in the upper tab.
2.3 Assign to a button
On this screen, assign a name for receiving input from each button or stick.
You can add assignments with [+].
For buttons and triggers, select click and
If you want to get a two-dimensional value such as a stick, select the position.
Here, assign the Action created in 1.3.
Figure 3: Example after allocation
2.4 Save your settings
When you’re done assigning, press Save Personal Bindings to save your settings.
3. Create a script
The object to which the script is attached is
An example script for getting controller input is:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
using UnityEngine.UI;
public class ControllerScript : MonoBehaviour
{
// action set
public SteamVR_ActionSet playerOperationActionSet;
//Hand to get (right hand or left hand or any hand)
public SteamVR_Input_Sources handTypeL;
public SteamVR_Action_Vector2 AnalogStickLeft;
public SteamVR_Action_Boolean LeftTrigger;
public GameObject player;
public float speed;
void Start()
{
playerOperationActionSet.Activate();
}
void Update()
{
Vector2 stickVector = AnalogStickLeft.GetAxis(handTypeL);
player.transform.position += speed * new Vector3(stickVector.x, 0, stickVector.y);
Debug.Log("stickVector: "+ stickVector.x+": "+stickVector.y);
Debug.Log("tringger: " + LeftTrigger.GetState(handTypeL));
}
}
The necessary elements are as follows.
–Set ActionSet to the one created in 1.2 and enable it
–Set SteamVR_Input_Sources
–Declare the Action set in 1.3
- get
3.1 Set the ActionSet to the one created in 1.2 and enable it
It can be enabled with SteamVR_ActionSet.Activate ();
.
By enabling this, the value can be obtained.
3.2 Set SteamVR_Input_Sources
Set SteamVR_Input_Sources.
Represents which hand controller.
3.3 Declare the Action set in 1.3
In 1.3, the one with boolean set is SteamVR_Action_Boolean
What is vector2 can be defined by SteamVR_Action_Vector2
.
3.4 Get
boolean is SteamVR_Action_Boolean.GetState (SteamVR_Input_Sources)
vector2 is SteamVR_Action_Vector2.GetAxis (SteamVR_Input_Sources)
You can get it at.
4. Adjust Inspector to match the script
Set the variables of the script written in 3. with Inspector.
4.1 SteamVR_Input_Sources
I want to use the controller on the left, so set LeftHand.
4.2 SteamVR_ActionSet
Select the Action Set set in 1.2.
4.3 SteamVR_Action_hoge
Apply the allocation set in 1.3 here.