Search Unity

Help Wanted Check if Controller is Active

Discussion in 'XR Interaction Toolkit and Input' started by HeyBishop, Oct 27, 2020.

  1. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    169
    How do I check if a controller is present/in use?

    I'm developing for the Oculus Go, which only has one controller, and I'd like to disable the unused controller. I'm attempting to make this game forwards-compatible - so that two controller users can still play. And, of course, there's the fact that not all players have the use of both arms.
    Plus, it's just annoying having that raycast from the unused controller blasting!

    My plan is to do something like this:
    Code (CSharp):
    1. void ControlCheck()
    2.     {
    3.         if (BOOL TRUE IF THIS CONTROLLER IS IN USE)
    4.         {
    5.             Debug.Log(gameObject.name + " is in use");
    6.         }
    7.         else
    8.         {
    9.             gameObject.SetActive(false);
    10.             Debug.Log(gameObject.name + " is NOT not in use");
    11.         }
    12.     }
     
  2. FakeByte

    FakeByte

    Joined:
    Dec 8, 2015
    Posts:
    141
    Use the input system to get all input devices and then check their characteristics to see if the device is a HMD, left or right associated controller and depending on that info disable your gameobjects.
     
  3. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    169
    How does one get a list of all input devices?
    I'm not getting very far with google.
     
  4. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    169
    Yata!
    I found it!

    Code (CSharp):
    1.     public void CheckForControllers()
    2.     {
    3.         bool xrControllerFound = false;
    4.         bool leftFound = false;
    5.         bool rightFound = false;
    6.  
    7.         List<InputDevice> leftHandDevices = new List<InputDevice>();
    8.         InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.Left, leftHandDevices);
    9.  
    10.         if (leftHandDevices.Count > 0)
    11.         {
    12.             xrControllerFound = true;
    13.             leftFound = true;
    14.         }
    15.  
    16.         List<InputDevice> rightHandDevices = new List<InputDevice>();
    17.         InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.Right, rightHandDevices);
    18.         if (rightHandDevices.Count > 0)
    19.         {
    20.             xrControllerFound = true;
    21.             rightFound = true;
    22.         }
    23.  
    24.         if (!xrControllerFound)
    25.         {
    26.            // deal with this
    27.         }
    28.  
    29.         leftController.SetActive(leftFound);
    30.         rightController.SetActive(rightFound);
    31.     }
     
  5. FakeByte

    FakeByte

    Joined:
    Dec 8, 2015
    Posts:
    141
    You could get all devices at once with
    InputDevices.GetDevices(m_InputDevices);
    and then loop over them as an improvement, but I guess as you only run the code once it wont make any difference
     
unityunity