Search code examples
unity-game-enginesteamvr

SteamVR: Correct way to get the input device triggered by an action and then get it's corresponding Hand class?


I have an action that is mapped to both my left amd right hand triggers on my VR controllers. I would like to access these instances...

Player.instance.rightHand 
Player.instance.leftHand

...depending on which trigger is used but I can't fathom the proper way to do it from the SteamVR API. So far the closest I have gotten is this...

public SteamVR_Action_Boolean CubeNavigation_Position;

private void Update()
{
    if (CubeNavigation_Position[SteamVR_Input_Sources.Any].state) {

        // this returns an enum which can be converted to string for LeftHand or RightHand
        SteamVR_Input_Sources inputSource = CubeNavigation_Position[SteamVR_Input_Sources.Any].activeDevice; 
    } 
}

...am I supposed to do multiple if statements for SteamVR_Input_Sources.LeftHand and SteamVR_Input_Sources.RightHand? That doesn't seem correct.

I just want to get the input device that triggered the action and then access it using Player.instance.


Solution

  • I was also looking for an answer to this. I've for now done what I think is what you mean with the if-statements. It works, but definitely not ideal. You want to directly refer to the hand which triggered the action, right?

    With the 'inputHand' variable here I get the transform.position of the hand from which I will raycast and show a visible line. I could have put a separate instance of a raycastScript like this on each hand, of course, but I wanted to make a 'global' script, if that makes sense.

    private SteamVR_Input_Sources inputSource = SteamVR_Input_Sources.Any; //which controller
    public SteamVR_Action_Boolean raycastTrigger; // action-button
    private Hand inputHand;
    
    private void Update()
    {
        if (raycastTrigger.stateDown && !isRaycasting) // If holding down trigger
        {
            isRaycasting = true;
            inputHand = inputChecker();
        }
        if (raycastTrigger.stateUp && isRaycasting)
        {
            isRaycasting = false;
        }
    }
    
    private Hand inputChecker()
    {
        if (raycastTrigger.activeDevice == SteamVR_Input_Sources.RightHand)
        {
            inputHand = Player.instance.rightHand;
        }
        else if (raycastTrigger.activeDevice == SteamVR_Input_Sources.LeftHand)
        {
            inputHand = Player.instance.leftHand;
        }
        return inputHand;
    }