Search Unity

Question Taking Input from VR Controllers

Discussion in 'AR/VR (XR) Discussion' started by Cooleobrad, May 29, 2020.

  1. Cooleobrad

    Cooleobrad

    Joined:
    Dec 16, 2016
    Posts:
    15
    I've been trying to implement some basic code just to read button presses on my Oculus Rift controllers, using XR toolkit. Based on the Unity XR documentation, I implemented code that looks something like this
    Code (CSharp):
    1. public class ReadInput : MonoBehaviour
    2. {
    3.     //Assigned to the RightHand Controller
    4.     public XRController controller = null;
    5.     bool buttonValue;
    6.  
    7.     void Start (){
    8.  
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.  
    14. if(controller.inputDevice.TryGetFeatureValue(CommonUsages.primaryButton, out buttonValue) && buttonValue)
    15.         {
    16.             Debug.Log(" pressing a");
    17.         }
    18. }
    19.  
    However, when testing this I don't get anything popping up on my console regardless of what buttons I'm pressing. What am I missing from this code to get things to work properly?
     
  2. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
  3. Cooleobrad

    Cooleobrad

    Joined:
    Dec 16, 2016
    Posts:
    15
    I updated the code according to the website. Still I'm not getting any feedback in the debug log. Where am I going wrong?

    Code (CSharp):
    1. public class ReadInput : MonoBehaviour
    2. {
    3.     //Assigned to the RightHand Controller
    4.     public XRController controller = null;
    5.     InputDevice device;
    6.     void Start (){
    7.         device = Input.GetDeviceAtXRNode(controller.controllerNode);
    8.     }
    9.     void Update()
    10.     {
    11. if(device.TryGetFeatureValue(CommonUsages.primaryButton, out bool buttonValue) && buttonValue)
    12.         {
    13.             Debug.Log(" pressing a");
    14.         }
    15. }
     
  4. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Your code appears correct now, and should crash if something obvious goes wrong (e.g. if you disconnected the headset).

    So there's something with your project (or your hardware). You'll need to debug your setup - I recommend you run the debugger, look at what the actual values are at that line of code when it runs, and post here when you've found something that's wrong.