当我使用 SteamVR 2.0 在 Unity 上以编程方式在 Vive Wand 上触发触觉时,我在 Unity 上遇到了奇怪的行为。
我设法记录下来让你更好地理解它:https ://www.youtube.com/watch?v=wq4OJUFghNI
基本上,我所做的只是一个简单的射击游戏,当你扣动扳机时,它每秒发射 3 发子弹并触发触觉。问题是如果您在触发触觉代码后立即释放触发器,听起来控制器触觉会卡住并且您会听到烦人的声音。重新触发触觉会停止该声音。
我已经尝试过谷歌,但看起来没有人经历过,因为我没有找到任何东西。我试图改变频率、持续时间或赋予函数的强度,但它没有改变任何东西(持续时间越短,没有这种行为的机会就越大,但这不是解决办法)
射击行为.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
namespace RG
{
namespace VR
{
public class ShootingBehaviour : MonoBehaviour
{
private bool _IsShooting = false;
private bool _GrabPinchPressed = false;
public GameObject BulletPrefab;
public SteamVR_Action_Vibration HapticAction;
public SteamVR_Action_Boolean GrabPinch;
public SteamVR_Input_Sources InputSource;
private void Start()
{
if (InputSource == SteamVR_Input_Sources.Any)
{
if (CompareTag("LeftController"))
InputSource = SteamVR_Input_Sources.LeftHand;
else if (CompareTag("RightController"))
InputSource = SteamVR_Input_Sources.RightHand;
else
Debug.LogError("Tag on the controller GameObject is incorrect, check again if 'LeftController' or 'RightController' has been assigned.");
}
}
private void Update()
{
if (_GrabPinchPressed)
{
Debug.Log("Trigger is currently pressed");
if (!_IsShooting && GetComponent<InteractableBehaviour>().CurrentlyEquipped)
{
HapticAction.Execute(0, 0.1f, 80, 150, InputSource);
StartCoroutine(_Shoot());
}
}
}
private void OnEnable()
{
if (GrabPinch != null)
GrabPinch.AddOnChangeListener(_OnTriggerPressedOrReleased, InputSource);
}
private void OnDisable()
{
if (GrabPinch != null)
GrabPinch.RemoveOnChangeListener(_OnTriggerPressedOrReleased, InputSource);
}
private void _OnTriggerPressedOrReleased(SteamVR_Action_In action_In)
{
if (GrabPinch.GetStateDown(InputSource))
_GrabPinchPressed = true;
else if (GrabPinch.GetStateUp(InputSource))
_GrabPinchPressed = false;
}
private IEnumerator _Shoot()
{
_IsShooting = true;
GameObject weapon = GetComponent<InteractableBehaviour>().CurrentlyEquipped;
GameObject bullet = Instantiate(BulletPrefab, weapon.transform.Find("BulletStartPos"));
bullet.transform.SetParent(null);
bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * 300);
yield return new WaitForSeconds(0.3f);
_IsShooting = false;
}
}
}
}
InteractableBehaviour
只是将武器存放在手中,展示它并没有真正的帮助或任何东西。
HapticAction.Execute
此处正确触发了触觉,但有时出于某些原因,某些东西会导致触觉出现故障。有人知道为什么要这样做吗?