Skip to content

hamdanbasri/HapticController-UnityXRInteractionToolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo

Custom Haptic Controller to assign haptic feedback on gameobjects.



Table of Contents
  1. About
  2. Functions
  3. Getting Started
  4. Code
  5. Example
  6. Contact

About

Made with Unity C#

Do you want each specific object to have their own custom haptic feedback when interacted in VR?

These codes are here just to do that.

For and in-depth video Justin P Barnett has that covered https://www.youtube.com/watch?v=-5tiV-lyYP8&ab_channel=JustinPBarnett


Project Requirements

  1. Unity project is set up for VR Games.
  2. Or follow along this tutorial > (https://www.youtube.com/watch?v=yxMzAw2Sg5w&ab_channel=JustinPBarnett) on how to setup.

Package Manager Requirements

  1. Open XR Plugin [Tested on 1.4.2]
  2. XR Interaction Toolkit [Tested on 2.1.1]
  3. XR Plugin Management [Tested on 4.2.0]
OpenXRPluginVersionImage XRInteractionToolkitVersionImage XRPluginManagementVersion

Functions

  • Control the haptic feedback's amplitude and duration on any gameobject.

Getting Started

Visitors

  • Add XROrigin to the scene.
  • Attach the HapticController.cs script on both the LeftHand Controller and RightHand Controller [XR Origin > Camera Offset > LeftHand Controller & RightHandController].
  • Add a SphereCollider on both the LeftHand Controller and RightHand Controller and set the trigger to true.
  • Create a Cube in the scene and rename it to HapticCube.
  • Add a Rigidbody component to the HapticCube, disable the gravity and enable the isKinematic.
  • Create a tag called HapticObject and assign the tag to the HapticCube.
  • Add a XR Simple Interactable component to the HapticCube.
  • Add the HapticObject.cs script to the HapticCube.
  • Adjust the Amplitude and Duration.
  • Press Play, put on your headset, reach out towards the HapticCube using your controller and test the feedback.

Pro-Tip:

You can right click on the HapticController component and select Haptic Test when playing. This will give you a sense of how strong do you want the feedback to be.

Known-Issues:

Duration of the haptic feedback is limit to 2 seconds. Let me know if there is a way to increase the duration.

Codes

Haptic Controller

using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

public class HapticController : MonoBehaviour
{
    public XRBaseController vrController;
    [Header("Right Click on this component and select Haptic Test to the feedback strength and duration.")]
    [RangeAttribute(0f,1f)]
    public float defaultAmplitude = 0.5f;
    public float defaultDuration = 0.3f;

    private void Start() {
        vrController = GetComponent<XRBaseController>();
    }

    [ContextMenu("Haptic Test")]
    public void SendHaptics()
    {
        vrController.SendHapticImpulse(defaultAmplitude, defaultAmplitude);
    }

    public void SendHaptics(XRBaseController controller, float amplitude, float duration)
    {
        controller.SendHapticImpulse(amplitude, duration);
    }

    private void OnTriggerEnter(Collider other) {
        if(other.CompareTag("HapticObject"))
        {
            other.GetComponent<HapticObject>();
            SendHaptics(vrController, other.GetComponent<HapticObject>().amplitude, other.GetComponent<HapticObject>().duration);
        }
    }
}

Haptic Object

using UnityEngine;

public class HapticObject : MonoBehaviour
{    
    [RangeAttribute(0f,1f)]
    public float amplitude = 0.5f;
    public float duration = 0.3f;
}

Example

Sample

Attached on both the LeftHand Controller and RightHand Controller.


Variable Annotation
VR Controller Automatically find the XR Base Controller in your GameObject component
Default Amplitude Set the default strength of the haptic feedback
Default Duration Set the default duration of the haptic feedback

Sample

Attached to the Haptic object.


Variable Annotation
Amplitude Set the strength of the haptic feedback on this GameObject
Duration Set the duration of the haptic feedback

Contact

Twitter LinkedIn

(back to top)

About

Custom haptic feedback for VR using XR Interaction Toolkit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages