Reputation: 664
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?
Upvotes: 1
Views: 12796
Reputation: 183
Here is how to you can work with hands:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Valve.VR.InteractionSystem;
public class ShowControllers : MonoBehaviour
{
public bool showController = false;
// Update is called once per frame
void Update()
{
foreach (var hand in Player.instance.hands)
{
if (showController)
{
hand.ShowController();
hand.SetSkeletonRangeOfMotion(Valve.VR.EVRSkeletalMotionRange.WithController);
}
else
{
hand.HideController();
hand.SetSkeletonRangeOfMotion(Valve.VR.EVRSkeletalMotionRange.WithoutController);
}
}
}
}
Upvotes: 0
Reputation: 4389
Without installing any additional dependencies other than SteamVR, attach the script to one of the Hand
components. It's a child of the Player
prefab in the SteamVR asset folder.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grab: MonoBehaviour {
private Valve.VR.InteractionSystem.Hand hand;
void Start () {
hand = gameObject.GetComponent<Valve.VR.InteractionSystem.Hand>();
}
void Update () {
if (hand.controller == null) return;
// Change the ButtonMask to access other inputs
if (hand.controller.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("Trigger down")
}
if (hand.controller.GetPress(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("Trigger still down")
}
if (hand.controller.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("Trigger released")
}
}
}
Upvotes: 1
Reputation: 322
I would really suggest you to add Valve's free HTC.UnityPlugin on top of SteamVR integration, thus you can quickly access the controllers input with
using UnityEngine;
using HTC.UnityPlugin.Vive;
public class YourClass : MonoBehaviour
{
private void Update()
{
// get trigger
if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Trigger))
{
// ...
}
}
}
Upvotes: 2