Skip to content

Instantly share code, notes, and snippets.

@corycorvus
Created May 19, 2020 18:46
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corycorvus/2b0788719f06fc162a8d5466ba58ac4d to your computer and use it in GitHub Desktop.
Save corycorvus/2b0788719f06fc162a8d5466ba58ac4d to your computer and use it in GitHub Desktop.
Haptic Vibrate Controller SteamVR and Oculus
using UnityEngine;
using UnityEngine.XR;
using Valve.VR;
// simple library and examples for XR haptics
// note: controllers must be tracking to use haptics
// other examples: https://vrtoolkit.readme.io/docs/vrtk_interacthaptics
public static class HapticPulse
{
public static void PulseAll() { }
public static void PulseLeft() { }
public static void PulseRight() { }
// https://valvesoftware.github.io/steamvr_unity_plugin/tutorials/SteamVR-Input.html
static void HapticPulseSteam()
{
Debug.Log("Haptic SteamVR");
SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 1, 10, 1);
SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 1, 10, 1);
//SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0f, 0.5f, 160, 0.5f);
}
// https://docs.unity3d.com/2019.1/Documentation/ScriptReference/XR.InputDevice.SendHapticImpulse.html
// https://docs.unity3d.com/2018.3/Documentation/Manual/xr_input.html
// todo: ifdef for unity xr
static void HapticPulseUnity()
{
Debug.Log("Haptic Unity");
InputDevice device = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
HapticCapabilities capabilities;
if (device.TryGetHapticCapabilities(out capabilities))
if (capabilities.supportsImpulse)
device.SendHapticImpulse(0, 0.5f, 1.0f);
device = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
if (device.TryGetHapticCapabilities(out capabilities))
if (capabilities.supportsImpulse)
device.SendHapticImpulse(0, 0.5f, 1.0f);
}
#if PLATFORM_OCULUS
// https://developer.oculus.com/documentation/unity/unity-haptics/
static void HapticPulseOculus()
{
Debug.Log("Haptic Oculus");
OVRInput.SetControllerVibration(1, 1, OVRInput.Controller.RTouch);
OVRInput.SetControllerVibration(1, 1, OVRInput.Controller.LTouch);
}
#endif
}
@michelepeixoto
Copy link

Hey thanks so much for sharing, I used part of your Unity function and it worked perfectly!

@LizanoLeo123
Copy link

This works like a charm, thank you so much for sharing ✨

@Lewyz
Copy link

Lewyz commented May 9, 2023

gracias a mi tambien me funciono una parte, gracias.

@xXExilXx
Copy link

xXExilXx commented Aug 2, 2023

for input_actions in not avalible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment