Unity's Open XR inputs TryGetFeatureValue always 0 for all Unity XR Input's CommonUsages

789 Views Asked by At

So I have been debugging for hours at this point to no avail. I call my function in another class and it keeps returning 0 I have tried logging everything to see if there is an error with it, and I can't find one. The target device prints out as UnityEngine.XR.InputDevice and nothing errors or warns. please if anyone has any insight. here's how I call it

Debug.Log(RightHand.AButtonDown());

and here is my code for the functions

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

public static class RightHand
{
    private static InputDevice targetDevice;

    static RightHand()
    {
        
        TryInitialize();
    }

    private static void TryInitialize()
    {
        Debug.Log("ran inital");
        List<InputDevice> devices = new List<InputDevice>();

        InputDeviceCharacteristics rightControllerCharacteristics = InputDeviceCharacteristics.Right | InputDeviceCharacteristics.Controller;

        InputDevices.GetDevicesWithCharacteristics(rightControllerCharacteristics, devices);

        foreach (var item in devices)
        {
            Debug.Log("ran log");
            Debug.Log(item.name + item.characteristics);
        }
        
        Debug.Log("right controler characteristics" + rightControllerCharacteristics);

        if (devices.Count > 0)
        {
            targetDevice = devices[0];
        }

        Debug.Log(targetDevice);
    }

    public static bool AButtonDown()
    {
        targetDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool primaryButtonOut);
        if (primaryButtonOut)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
1

There are 1 best solutions below

0
On

My best guess is the list of devices needs to be updated each time a device value is accessed. The method I use uses Unity's XR Toolkit and had the same problem as yours until I moved InputDevice assignment into Update.

using UnityEngine.XR;
public class InputDeviceSample : MonoBehaviour
{
    InputDevice left;
    InputDevice right;

    void Start()
    {

    }


    void Update()
    {
        // needs to be in Update
        right = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);

        // left = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
        // more https://docs.unity3d.com/ScriptReference/XR.XRNode.html

        // assigns button value to out variable, if expecting Vector3 replace bool
        right.TryGetFeatureValue(CommonUsages.triggerButton, out bool isPressed);

        Debug.Log(isPressed);
    }
}

CommonUsages is the list of input actions you can read in, here's a list with brief descriptions: https://docs.unity3d.com/ScriptReference/XR.CommonUsages.html