Skip to main content
Inspiring
June 1, 2017

Touch vibration in unity(C#)?

Hi,

I just keep crashing my game trying out different ways to enable touch vibration, does anyone know how to do it correctly in Unity 5?

Start()  HapticsClip = new OVRHapticsClip(VibeClip);

RHit()   OVRHaptics.RightChannel.Preempt(HapticsClip);

^ In this example VibeClip is less than a second long audio clip

^This crashes unity. so does trying 

OVRHaptics.Channels[1].Mix(new OVRHapticsClip(VibeClip));
This topic has been closed for replies.

3 replies

Inspiring
June 3, 2017
Here's an example you can use to play a sound and corresponding vibration on contact.

using System.Collections.Generic;
using UnityEngine;

public class PhysicsSound : MonoBehaviour
{
public bool useHaptics = false;
public bool useSound = true;

public OVRInput.Controller controller;

private AudioSource cachedSource;
private OVRHapticsClip hapticsClip;
private float hapticsClipLength;
private float hapticsTimeout;

void OnTriggerEnter(Collider c)
{
if (useHaptics)
PlayHaptics(c);

if (useSound)
PlaySound(c);
}

void OnCollisionEnter(Collision c)
{
if (useHaptics)
PlayHaptics(c.collider);

if (useSound)
PlaySound(c.collider);
}

void PlayHaptics(Collider c)
{
var source = c.GetComponent<AudioSource>();
if (source == null)
return;

if (source != cachedSource)
{
hapticsClip = new OVRHapticsClip (source.clip);
hapticsClipLength = source.clip.length;
cachedSource = source;
}

if (Time.time < hapticsTimeout)
return;

hapticsTimeout = Time.time + hapticsClipLength;

if (controller == OVRInput.Controller.LTouch)
OVRHaptics.LeftChannel.Preempt(hapticsClip);
else
OVRHaptics.RightChannel.Preempt(hapticsClip);
}

void PlaySound(Collider c)
{
var source = c.GetComponent<AudioSource>();
if (source && !source.isPlaying)
source.PlayDelayed(0.1f);
}
}


New Participant
April 20, 2018

bonjour a tous ,
Salut, je débute dans le code, et je voulais savoir comment on écrit un script une action de (vibration ) qui dure par exemple demi seconde, et qui peut être répétée, après un délai d'une seconde?

avec Unity codé par c#

j'attend votre réponse ,merci d'avance

Up and Coming
November 10, 2018
VRDave, thanks for the code sample, and strange, I don't get any vibration when I try. I'm on all latest Unity & Oculus sdk, did something change in the meantime or am I doing something wrong?
- Anyland dev -