Search code examples
unity-game-engineinputhtc-vivesteamvropenxr

How to get the SteamVR Inputs using OpenXR in Unity?


I enabled in Unity the OpenXR VR mode. enter image description here

Then I enabled the HTC Vive interaction profile and others enter image description here

After that I can see the world and I can get the Hand positions of the HTC Vive controller, but I can not get any input, How can I get for example the Trigger button press event?

enter image description here

I am using the following code:

private UnityEngine.XR.InputDevice inputDevice;

public bool isIndexTriggerPressed()
    {
        bool triggerValue = false;
        inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.triggerButton, out triggerValue);
        if ((!previousIndexTriggerPressed) && (triggerValue))
        {
            previousIndexTriggerPressed = triggerValue;
            return true;
        }
        previousIndexTriggerPressed = triggerValue;
        return false;
    }

Is this the right code?, How can I get the trigger press event?


Solution

  • The only way that I get it to work was using directly the SteamVR Unity plugin.

    I dragged the "[CameraRig]" object that is localized on the SteamVR/Prefabs directory, to my Scene.

    I used the following code to get the inputs:

    private GameObject controller;
    private SteamVR_Input_Sources inputDevice = SteamVR_Input_Sources.LeftHand;
    
    public void setInputDevice(SteamVR_Input_Sources inputDevice)
    {
        this.inputDevice = inputDevice;
    }
    
    public void setController(GameObject controller)
    {
        this.controller = controller;
    }
    
    public GameObject getController()
    {
        return controller;
    }
    
    public Vector2 getTrackPad()
    {
        return SteamVR_Actions.default_Trackpad.GetAxis(inputDevice);
    }
    
    public bool isHandTrigger()
    {
        return SteamVR_Actions.default_GrabGrip.GetState(inputDevice);
    }
    
    public bool isHandTriggerPressed()
    {
        return SteamVR_Actions.default_GrabGrip.GetStateDown(inputDevice);
    }
    
    public bool isHandTriggerReleased()
    {
        return SteamVR_Actions.default_GrabGrip.GetStateUp(inputDevice);
    }
    
    public bool isIndexTrigger()
    {
        return SteamVR_Actions.default_GrabPinch.GetState(inputDevice);
    }
    
    public bool isIndexTriggerPressed()
    {
        return SteamVR_Actions.default_GrabPinch.GetStateDown(inputDevice);
    }
    
    public bool isIndexTriggerReleased()
    {
        return SteamVR_Actions.default_GrabPinch.GetStateUp(inputDevice);
    }
    
    public bool isMenu()
    {
        return SteamVR_Actions.default_Menu.GetState(inputDevice);
    }
    
    public bool isMenuPressed()
    {
        return SteamVR_Actions.default_Menu.GetStateDown(inputDevice);
    }
    
    public bool isMenuReleased()
    {
        return SteamVR_Actions.default_Menu.GetStateUp(inputDevice);
    }
    

    Dont forget the bindings Window -> SteamVR Input

    enter image description here

    enter image description here