0

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?

Shoko84
  • 11
  • 2
  • what is in that `_Shoot()` coroutine? / Where is `_IsShooting ` set? Also note that calling `GetComponent()` is quite inefficient and you should rather do it e.g. in `Awake` and store the result for later – derHugo Jan 04 '19 at 12:19
  • Also there I didn't find `HapticAction` in the [SteamVr sdk](https://github.com/ValveSoftware/steamvr_unity_plugin) so if this is something you implemeted .. could you add this as well please? – derHugo Jan 04 '19 at 12:24
  • I found however the [SteamVR_Action_Vibration](https://github.com/ValveSoftware/steamvr_unity_plugin/blob/master/Assets/SteamVR/Input/SteamVR_Action_Vibration.cs) if you mean that one. There it says for the third parameter `"amplitude">How intense the haptic action should be (0 - 1)` you are calling it with `150` ... ? – derHugo Jan 04 '19 at 12:32
  • I edited my question and added the whole `ShootingBehaviour.cs` file just "to be sure" nothing is missing, but I don't think it will help anything since the shooting behavior **is** working, what's not is just the haptics one. Not gonna link `InteractableBehaviour.cs` for about the same reasons I mentioned. The `HapticAction.Execute` is not a function of mine and is part of SteamVR 2.0 by the way (EDIT: oops yeah because it was a `SteamVR_Action_Vibration`) – Shoko84 Jan 04 '19 at 12:33
  • I just tried changing the `amplitude` parameter and it doesn't change anything (tried 1, 0.2f and 0). _That's a bit weird because I don't see any difference between 1 and 0.2f. 0 doesn't fire haptics, which is normal_ – Shoko84 Jan 04 '19 at 12:41

2 Answers2

0

Like Shoko84 states the amplitude parameter makes no difference. In fact none of the parameters seem to make any difference in calling HapticAction.Execute

0

I believe the paramaters have strange limits. Duration: 0 to .79 Frequency is 0 to 320 Amplitude is 0 to .25

If you go over any of these values they're just the max value. This is why you didn't see any difference between amplitude .2 and 1, they are nearly the same (.o5 difference in intensity)

Jeff
  • 1