对于 Unity XR 输入的 CommonUsages.trigger,TryGetFeatureValue 始终为 0

问题描述 投票:0回答:2

我正在 Unity (2020.3.15f2) 中使用 XR Interaction Toolkit 包 (1.0.0-pre.5) 为我的 Oculus Quest 2 开发一款 VR 游戏。在开发的这个阶段,我正在尝试识别对分别使用控制器上的触发器和握持按钮来制作一些 3D 手部模型的动画。这是我为完成此任务而编写的脚本:

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();
      }
    }
}

我遇到的问题是

triggerValue
gripValue
的值始终为0。
targetDevice
的值看起来不错。我还尝试使用
triggerButton
gripButton
primaryButton
等,它们也始终为 0/false。手模型显示得很好,它们的运动与控制器的运动同步,但它们似乎不想记录任何按钮按下。

我已经在这个问题上坚持了几个小时,非常感谢任何见解,谢谢!

unity-game-engine virtual-reality oculus
2个回答
1
投票

您的项目设置是否使用(新)输入系统?我可以毫无问题地检测扳机和握力值。

还要确保目标设备实际使用触发器和握持功能,也许它是其他设备,例如 HMD。


0
投票

我想说是因为您期望输入的真/假或开/关数字响应。您期望触发器打开 (1) 或关闭 (0),而触发器也可以是返回的模拟值以及其他输入值。 (即“bool”变量类型将不起作用和/或将返回错误。)

明白了吗?

© www.soinside.com 2019 - 2024. All rights reserved.