Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get an XR Grab Interactable to detect XR inputs from the controller which picked it up

Discussion in 'AR/VR (XR) Discussion' started by bengladwyn, Jun 9, 2020.

  1. bengladwyn

    bengladwyn

    Joined:
    Apr 11, 2019
    Posts:
    9
    Hi,

    If I have a XR Grab Interactable object, how do I get it to detect the inputs from the controller which has picked it up (the controller has an XRDirectInteractor). E.g. I want an object to change colour when I press a primary button and do something else when I press the secondary button. How do I get the object to take inputs from only the controller which has picked it up?
     
  2. tobaccoblonde

    tobaccoblonde

    Joined:
    Apr 18, 2017
    Posts:
    11
    i pulled my hair out trying to get something similar. in the end even following this didn't help https://docs.unity3d.com/Manual/xr_input.html

    so i check for the button press with the old input manager, and the name of the selectingInteractor on the XRGrabInteractable at the same time to find out which controller.
     
  3. bengladwyn

    bengladwyn

    Joined:
    Apr 11, 2019
    Posts:
    9
    How do you get the name of the selecting interactor, did you modify the XRGravInteractable script or is there an easier way of doing it?
     
  4. tobaccoblonde

    tobaccoblonde

    Joined:
    Apr 18, 2017
    Posts:
    11
    if you just need a cheap one shot.

    add named inputs to your project settings -> input manager. you can find all the "joystick button" values here, https://docs.unity3d.com/Manual/xr_input.html

    then do something like this.

    Code (CSharp):
    1. using UnityEngine.XR.Interaction.Toolkit;
    2. public class MyReallyCoolHandThing : XRGrabInteractable {
    3.     void Update () {
    4.         if (Input.GetButtonDown("VR_PrimaryButton_LeftHand") && selectingInteractor.name == "TheLeftHand") {
    5.             DoThing();
    6.         }
    7.     }
    8. }
     
    bengladwyn likes this.
  5. bengladwyn

    bengladwyn

    Joined:
    Apr 11, 2019
    Posts:
    9
    Thanks for the help so far. I get an error when using this, unity says :
    NullReferenceException: Object reference not set to an instance of an object
    referring to this line of the code:
    Code (CSharp):
    1. if (selectingInteractor.name == "Left Hand")
    Do you know what might be causing this? Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5. using UnityEngine.XR.Interaction.Toolkit;
    6.  
    7. public class Gun : XRGrabInteractable
    8. {
    9.  
    10.     private InputDevice targetDevice;
    11.  
    12.     void Start()
    13.     {
    14.         List<InputDevice> devices = new List<InputDevice>();
    15.         InputDeviceCharacteristics controllerCharacteristics = InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Right;
    16.         InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);
    17.  
    18.         if(devices.Count>0)
    19.         {
    20.             targetDevice = devices[0];
    21.         }
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         if (targetDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool primaryButtonValue) && primaryButtonValue)
    27.         {
    28.             if (selectingInteractor.name == "Left Hand")
    29.             {
    30.                 ammo = 14;
    31.                 ammoText.text = ammo.ToString();
    32.             }
    33.         }
    34.  
    35.     }
    36. }
     
  6. bengladwyn

    bengladwyn

    Joined:
    Apr 11, 2019
    Posts:
    9
    I fixed it! :)
    Thanks for all the help with this!
     
  7. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,924
    When it's picked up, you have a reference to the Interactor. That interactor has a Controller component on the same object (or should do, if you're using the default setup). That Controller lets you select the InputDevice:

    http://snapandplug.com/xr-input-toolkit-2020-faq/#FAQ:-How-do-I-get-an-InputDevice?

    ...and then you can do all the normal button-press handling, using that InputDevice, and it will only read from the controller that picked it up.

    http://snapandplug.com/xr-input-toolkit-2020-faq/#FAQ:-How-do-I-detect-button.IsPressed?

    (obviously you need to assign a reference to that InputDevice somewhere in your script in the callback for OnSelectEnter or similar)
     
    Threeyes and hhoffren like this.
  8. TylerMerker95

    TylerMerker95

    Joined:
    Feb 13, 2020
    Posts:
    1
    Thank lord I found this thread. I'd about given up hope.
     
  9. lunix42

    lunix42

    Joined:
    Oct 13, 2020
    Posts:
    3
    Hello, I realize this is a very late reply but I am getting the similar error "NullReferenceException: Object reference not set to an instance of an object".
    Can you explain how you fixed it? Thanks in advance
     
  10. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,924
    That's a generic error that could mean any line in any file from any part of your project has a bug.

    You need to read the rest of the error message and find out which line of code it was, and then figure out what's wrong with your code.
     
  11. Ikta

    Ikta

    Joined:
    Sep 30, 2019
    Posts:
    5
    I still don't quite understand. I have a wand object that I want to do something when primary button is pressed. This wand object can be carried by left or right hand. How do I detect if it's actually the correct hand (the hand that's holding the wand) that presses the primary button? I can get input for left hand, or right hand, but for either one input I don't seem to be able to do it