Search Unity

Resolved How to get which hand is grabbing an XR Grab Interactable object

Discussion in 'VR' started by ThePurpleSprinkles, Aug 5, 2020.

Thread Status:
Not open for further replies.
  1. ThePurpleSprinkles

    ThePurpleSprinkles

    Joined:
    Sep 22, 2018
    Posts:
    16
    This is a very simple question, but I couldn't find an answer anywhere in the documentation.

    I'm using the primaryButton to eject a magazine from a gun object that you can pick up, but the gun script doesn't have information on which hand is grabbing it, so it can know which primaryButton to listen for.

    How can I get the hand that is currently grabbing the GO?
     
    raitoryuukuu likes this.
  2. ThePurpleSprinkles

    ThePurpleSprinkles

    Joined:
    Sep 22, 2018
    Posts:
    16
    I finally found the answer, turns out you can't get what hand is holding the GO, but you can get whether the hand is holding a GO with a certain tag, through each hands' XRDirectInteractor.

    Here's my code:

    Code (CSharp):
    1.     public XRDirectInteractor rHand;
    2.     public XRDirectInteractor lHand;
    3.     public InputManager input;
    4.     [HideInInspector]
    5.     public bool ejectButton;
    6.  
    7.     void Update()
    8.     {
    9.         //testing for the selectTarget of each hand, which is what they are holding
    10.         if (rHand.selectTarget.tag == "Gun")
    11.             ejectButton = input.aButton;
    12.  
    13.         else if (lHand.selectTarget.tag == "Gun")
    14.             ejectButton = input.xButton;
    15.  
    16.         else
    17.             ejectButton = false;
    18.     }
     
Thread Status:
Not open for further replies.