I encountered a weird behavior on Unity when I'm firing haptics on my Vive Wand programmatically on Unity with SteamVR 2.0.
I managed to record it for letting you understand it better: https://www.youtube.com/watch?v=wq4OJUFghNI
Basically, what I did is just a simple shooter, when you pull the trigger, it shoots 3 bullets each second and triggers haptics. The issue is if you release the trigger right just after the haptic code is fired, it sounds like that the controller haptics stays stucked and that you can hear an annoying sound. Retriggering the haptics stops that sound.
I already tried to Google that but looks like nobody experienced it since I didn't find anything. I tried to change the frequency, duration or the intensity given to the function but it doesn't change anything (with a smaller duration, there is a higher chance to not have this behavior, but that's not a fix)
ShootingBehaviour.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
is just storing the weapon in hand, showing it there would not really help or anything.
The HapticAction.Execute
here is correctly triggering haptics but looks like something is glitching up the haptics sometimes for some reasons. Does someone have an idea why it is doing that?