Search Unity

Other How to make Quest 2 controller vibrate when shooting a gun.

Discussion in 'VR' started by jdscogin, Oct 9, 2022.

  1. jdscogin

    jdscogin

    Joined:
    Oct 26, 2014
    Posts:
    88
    This is a simple way to make the controller vibrate when shooting a gun.
    Just call Vib().
    You can change the length of the vibrate by changing the time variables in Invoke.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Buzz : MonoBehaviour
    6. {
    7.     public float time = 3f;
    8.     // Start is called before the first frame update
    9.  
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.        
    15.     }
    16.     public void Vib()
    17.     {
    18.         Invoke("startVib", .1f);
    19.         Invoke("stopVib", .4f);
    20.     }
    21.     public void startVib()
    22.     {
    23.         OVRInput.SetControllerVibration(1, 1, OVRInput.Controller.RTouch);
    24.     }
    25.     public void stopVib()
    26.     {
    27.         OVRInput.SetControllerVibration(0, 0, OVRInput.Controller.RTouch);
    28.     }
    29. }
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    Is this a question or guide?
     
  3. jdscogin

    jdscogin

    Joined:
    Oct 26, 2014
    Posts:
    88
    Guide
     
    DevDunk likes this.
  4. kig160

    kig160

    Joined:
    May 29, 2023
    Posts:
    3
    Hi @jdscogin
    from below reference i found that quest pro controllers have three haptics(LRA) position.
    https://www.uploadvr.com/quest-touch-pro-controllers-haptics-index-tracking/
    But overinput.contrller have only R touch I think
    public enum Controller
    {
    None = OVRPlugin.Controller.None, ///< Null controller.
    LTouch = OVRPlugin.Controller.LTouch, ///< Left Oculus Touch controller. Virtual input mapping differs from the combined L/R Touch mapping.
    RTouch = OVRPlugin.Controller.RTouch, ///< Right Oculus Touch controller. Virtual input mapping differs from the combined L/R Touch mapping.
    Touch = OVRPlugin.Controller.Touch, ///< Combined Left/Right pair of Oculus Touch controllers.
    Remote = OVRPlugin.Controller.Remote, ///< Oculus Remote controller.
    Gamepad = OVRPlugin.Controller.Gamepad, ///< Xbox 360 or Xbox One gamepad on PC. Generic gamepad on Android.
    Hands = OVRPlugin.Controller.Hands, ///< Left Hand provided by hand-tracking.
    LHand = OVRPlugin.Controller.LHand, ///< Left Hand provided by hand-tracking.
    RHand = OVRPlugin.Controller.RHand, ///< Right Hand provided by hand-tracking.
    Active = OVRPlugin.Controller.Active, ///< Default controller. Represents the controller that most recently registered a button press from the user.
    All = OVRPlugin.Controller.All, ///< Represents the logical OR of all controllers.
    }
    Can I know how to control each three part of the quest pro lra actuator?
     
    Last edited: Aug 30, 2023
  5. danwolf

    danwolf

    Joined:
    Sep 19, 2013
    Posts:
    1
    This is an old thread, however, it was my first google result, so I wanted to share a little working example for the MetaQuestPro using only Unity.XR.
    It will make the left controller vibrate for 1 second, I suppose in its maximum force.
    About the comment above regarding the different actuators, I suppose you might be able to control them independently by changing the channel to send the impulse, but I haven't tested that specifically yet.

    Code (CSharp):
    1.  
    2.                 UnityEngine.XR.HapticCapabilities capabilitiesL;
    3.                 InputDevices.GetDeviceAtXRNode(XRNode.LeftHand).TryGetHapticCapabilities(out capabilitiesL);
    4.                 if (capabilitiesL.supportsImpulse)
    5.                 {
    6.                     uint channel = 0;
    7.                     float amplitude = 1.0f;
    8.                     float duration = 1.0f;
    9.                     InputDevices.GetDeviceAtXRNode(XRNode.LeftHand).SendHapticImpulse(channel, amplitude, duration);
    10.                 }
    11.  
    Cheers,