Forum Discussion
TrevJT
9 years agoProtege
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));
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));
3 Replies
Replies have been turned off for this discussion
- vrdavebOculus StaffHere'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);
}
} - sirine_jaballaHonored Guest
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
- jphilippProtegeVRDave, 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?
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device