4

I am developing a VR game in Unity (2020.3.15f2) using the XR Interaction Toolkit package (1.0.0-pre.5) for my Oculus Quest 2. At this stage in my development, I am trying to recognize presses to the trigger and grip buttons on the controllers respectively in order to animate some 3D hand models. Here's the script I've written to accomplish this:

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

public class HandPresence : MonoBehaviour {
    public InputDeviceCharacteristics controllerCharacteristics;
    public GameObject handModelPrefab;
    private InputDevice targetDevice;
    private GameObject spawnedHandModel;
    private Animator handAnimator;

    void Start() {
      TryInitialize();
    }

    void TryInitialize() {
      List<InputDevice> devices = new List<InputDevice>();
      InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);

      if (devices.Count > 0) {
        targetDevice = devices[0];
        spawnedHandModel = Instantiate(handModelPrefab, transform);
        handAnimator = spawnedHandModel.GetComponent<Animator>();
      }
    }

    void UpdateHandAnimation() {
      if (targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue)) {
        handAnimator.SetFloat("Trigger", triggerValue);
      } else {
        handAnimator.SetFloat("Trigger", 0);
      }
      if (targetDevice.TryGetFeatureValue(CommonUsages.grip, out float gripValue)) {
        handAnimator.SetFloat("Grip", gripValue);
      } else {
        handAnimator.SetFloat("Grip", 0);
      }
    }

    void Update()
    {
      if (!targetDevice.isValid) {
        TryInitialize();
      } else {
        spawnedHandModel.SetActive(true);
        UpdateHandAnimation();
      }
    }
}

The issue I'm experiencing is that the values of both triggerValue and gripValue are always 0. The value of targetDevice looks fine. I also tried using triggerButton, gripButton, primaryButton, etc. and they are always 0/false as well. The hand models show up just fine and their movement is in sync with the movement of the controllers, but they just don't seem to want to register any button presses.

I've been stuck on this one for hours and would very much appreciate any insight, thank you!

robbymarston
  • 344
  • 3
  • 16

1 Answers1

1

Is your project setup with the (new) Input System? I have no problem detecting there trigger and grip values.

Also make sure the targetDevice actually uses trigger and grip features, maybe it is another device such as the HMD.