Welcome to the Oculus Developer Forums!

Your participation on the forum is subject to the Oculus Code of Conduct.

In general, please be respectful and kind. If you violate the Oculus Code of Conduct, your access to the developer forums will be revoked at the discretion of Oculus staff.
New to the forums? Click here to read the How To guide. -- Developers click here.

Touch vibration in unity(C#)?

TrevJTTrevJT Posts: 44
Brain Burst
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));

Answers

  • vrdavebvrdaveb Posts: 1,596 Oculus Staff
    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);
    	}
    }


  • sirine.jaballasirine.jaballa Posts: 1
    NerveGear

    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

  • imperativityimperativity Posts: 3,587 Valuable Player
    Translation for developer above:

    Hello everyone ,
    Hi, I start in the code, and I wanted to know how to write a script an action of (vibration) that lasts for example half a second, and that can be repeated, after a delay of one second?

    with Unity coded by C #

    I'm waiting for your answer, thanks in advance

  • jphilippjphilipp Posts: 11
    NerveGear
    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?
Sign In or Register to comment.