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

Question Oculus menu button press works locally but not when run on headset

Discussion in 'VR' started by JD_FVTC, Apr 14, 2022.

  1. JD_FVTC

    JD_FVTC

    Joined:
    Oct 20, 2017
    Posts:
    54
    When I press the menu button on the oculus left controller my code doesn't detect the press when run on the headset. When i press play in unity the button press is detected.

    This used to work in the older xr tool kit. I upgraded unity version and xrtoolkit sdk. No errors when run locally.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5. using UnityEngine.Events;
    6.  
    7.  
    8. public class InputListenerSecondaryButton : MonoBehaviour
    9. {
    10.  
    11.  
    12.     List<InputDevice> devices;
    13.     public XRNode controllerNode;
    14.  
    15.     [Tooltip("Event when the button starts being pressed")]
    16.     public UnityEvent OnPress;
    17.  
    18.     [Tooltip("Event when the button starts being released")]
    19.     public UnityEvent OnRelease;
    20.  
    21.     //keep track of whether we are pressing the button
    22.     bool isPressed = false;
    23.  
    24.     private void Awake()
    25.     {
    26.         devices = new List<InputDevice>();
    27.     }
    28.  
    29.     void GetDevice()
    30.     {
    31.         InputDevices.GetDevicesAtXRNode(controllerNode, devices);
    32.      
    33.     }
    34.  
    35.     // Start is called before the first frame update
    36.     void Start()
    37.     {
    38.         GetDevice();
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update()
    43.     {
    44.         GetDevice();
    45.         foreach (var device in devices)
    46.         {
    47.            // Debug.Log(device.name + " " + device.characteristics);
    48.  
    49.             if (device.isValid)
    50.             {
    51.                 bool inputValue;
    52.  
    53.                 if (device.TryGetFeatureValue(CommonUsages.menuButton, out inputValue) && inputValue)
    54.                 {
    55.                     if (!isPressed)
    56.                     {
    57.                         isPressed = true;
    58.    
    59.                         OnPress.Invoke();
    60.                     }
    61.  
    62.                 }
    63.                 else if (isPressed)
    64.                 {
    65.                     isPressed = false;
    66.                     OnRelease.Invoke();
    67.        
    68.  
    69.                 }
    70.             }
    71.            
    72.         }
    73.     }
    74. }
    75.