Search Unity

Resolved Unity doesn't return any value for a trigger being pressed down.

Discussion in 'VR' started by jlorenzi, Sep 2, 2021.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5.  
    6. public class HandPresence : MonoBehaviour
    7. {
    8.     private InputDevice targetDevice;
    9.     void Start()
    10.     {
    11.         List<InputDevice> devices = new List<InputDevice>();
    12.  
    13.         InputDeviceCharacteristics rightControllerCharacteristics = InputDeviceCharacteristics.Right | InputDeviceCharacteristics.Controller;
    14.  
    15.         InputDevices.GetDevicesWithCharacteristics(rightControllerCharacteristics, devices);
    16.  
    17.         foreach (var item in devices)
    18.         {
    19.             Debug.Log(item.name);
    20.         }
    21.  
    22.         if (devices.Count > 0)
    23.         {
    24.             targetDevice = devices[0];
    25.         }
    26.      
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue);
    32.  
    33.         Debug.Log(triggerValue);
    34.         if (triggerValue > 0.1f)
    35.         {
    36.             Debug.Log("Trigger pressed " + triggerValue);
    37.             ButtonIsPressed.triggerIsDown = true;
    38.         }
    39.     }
    40. }
    41.  
    Somehow, this code doesn't return anything. Just so you know, I'm following this tutorial:

    Edit: I did the classic mistake of forgetting to put the script on a game object, oof. Anyways, it works now.
     
    Last edited: Sep 2, 2021
  2. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    Check this thread, I think they're going through the same tutorial:
    https://forum.unity.com/threads/problem-with-vr-controller-input.921563/#post-7466834

    If you're trying to use InputDevices.GetDevicesWithCharacteristics as soon as you enter playmode in the editor you're probably not getting any devices. You need to call InputDevices.GetDevicesWithCharacteristics for a few frames before it finds anything. (This is only true in the editor, if you built a standalone .exe the same code would probably work correctly)
     
  3. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    I tried this but Unity still doesn't detect a single device, here's my updated code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5.  
    6. public class HandPresence : MonoBehaviour
    7. {
    8.     private InputDevice targetDevice;
    9.     List<InputDevice> devices = new List<InputDevice>();
    10.     void Start()
    11.     {
    12.  
    13.         InputDeviceCharacteristics rightControllerCharacteristics = InputDeviceCharacteristics.Right | InputDeviceCharacteristics.Controller;
    14.  
    15.      
    16.         while (devices.Count == 0)
    17.         {
    18.             InputDevices.GetDevicesWithCharacteristics(rightControllerCharacteristics, devices);
    19.         }
    20.  
    21.         targetDevice = devices[0];
    22.      
    23.         foreach (var item in devices)
    24.         {
    25.             Debug.Log(item.name + item.characteristics);
    26.         }
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         Debug.Log(devices.Count);
    32.         targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue);
    33.         Debug.Log(triggerValue);
    34.         if (triggerValue > 0.1f)
    35.         {
    36.             Debug.Log("Trigger pressed " + triggerValue);
    37.             ButtonIsPressed.triggerIsDown = true;
    38.         }
    39.     }
    40. }
    41.  
    What's weird is that the debug statement that logs the amount of devices doesn't print anything, so I really have no idea what's going on.
     
    Last edited: Sep 2, 2021