- Home /
How to get controller input when using the SteamVR Interaction System
I want to just simply get controller input from the user in my VR game, and i also want to use the SteamVR interaction system so that I can have easy UI stuff be implemented. However, I cannot get input from the controller off of the Hand script.
All that I did was drag in the "Player" prefab, and then write a script to go on the Hand object to get input from the triggers.
private Hand _hand; // The hand object
private SteamVR_Controller.Device controller; // the controller property of the hand
void Start ()
{
// Get the hand componenet
_hand = GetComponent<Hand>();
// Set the controller reference
controller = _hand.controller;
}
void Update ()
{
// === NULL REFERENCE === //
if ( controller.GetHairTrigger())
{
Debug.Log ("Trigger");
}
}
This gives me a null ref exception to the "controller" object. I have also tried getting the controller component in OnEnable()
and Awake()
and that didn't work either. Even in Update()
. So for some reason the Hand
class of SteamVR does not hold a reference to the controller. Am I doing something wrong?Am I missing some sort of index specification when i get the controller?
I am able to get controller input like this:
private SteamVR_TrackedObject trackedObj; // The tracked object that is the controller
private SteamVR_Controller.Device Controller // The device property that is the controller, so that we can tell what index we are on
{
get { return SteamVR_Controller.Input((int)trackedObj.index); }
}
private void Awake()
{
// Get the tracked object componenet
trackedObj = GetComponent<SteamVR_TrackedObject>();
}
void Update()
{
if(Controller.GetHairTrigger()){
Debug.Log("hey trigger");
}
}
But then I cannot use the Interaction system. Anyone have a clue?
Answer by TheLowestAnimal · Oct 07, 2017 at 07:57 PM
I'm sure you solved this by now, But... for anyone else that stumbles across having this issue:
You need to add the library for the interaction system to your code.
using Valve.VR.InteractionSystem;
This sets up references to their classes for your code
Answer by Postur · Apr 23, 2018 at 03:38 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.ObjectModel;
using Valve.VR.InteractionSystem;
public class SAHandInput : MonoBehaviour {
private Hand hand; // The hand object
void Start()
{
// Get the hand componenet
hand = GetComponent<Hand>();
}
void Update()
{
// === NULL REFERENCE === //
if (hand.controller.GetHairTriggerDown())
{
Debug.Log("Trigger");
}
}
}
This works. I don't know why.