Skip to content

Instantly share code, notes, and snippets.

@mzandvliet
Created January 3, 2018 17:12
Show Gist options
  • Save mzandvliet/9821542e08b9c291ffd06d51d04c7ac3 to your computer and use it in GitHub Desktop.
Save mzandvliet/9821542e08b9c291ffd06d51d04c7ac3 to your computer and use it in GitHub Desktop.
OVR Haptics Example
using UnityEngine;
/*
* Haptics internal update (Process()) is triggered by OVRManager, which
* you need to have in your scene.
*/
public class HapticTest : MonoBehaviour {
private OVRHapticsClip _clip;
[SerializeField] private AnimationCurve _ampEnv;
private void Awake() {
Debug.Log(OVRHaptics.Config.MinimumBufferSamplesCount); // 1
Debug.Log(OVRHaptics.Config.MaximumBufferSamplesCount); // 256
Debug.Log(OVRHaptics.Config.OptimalBufferSamplesCount); // 20
Debug.Log(OVRHaptics.Config.SampleRateHz); // 320
Debug.Log(OVRHaptics.Config.SampleSizeInBytes); // 1
_clip = new OVRHapticsClip(160);
_clip.Count = _clip.Capacity;
// Random clip
//new System.Random().NextBytes(_clip.Samples);
for (int i = 0; i < _clip.Capacity; i++) {
float val = 0.5f + 0.5f * Mathf.Sin((i / 160f) * 5f * Mathf.PI * 2f);
val *= _ampEnv.Evaluate(i / 160f);
_clip.Samples[i] = (byte) (val * 255f);
}
}
public void Update() {
if (OVRInput.GetDown(OVRInput.Button.PrimaryHandTrigger)) {
OVRHaptics.LeftChannel.Preempt(_clip);
}
if (OVRInput.GetDown(OVRInput.Button.SecondaryHandTrigger)) {
OVRHaptics.RightChannel.Preempt(_clip);
}
}
}
@jackbrookes
Copy link

jackbrookes commented Jul 5, 2018

This no longer works in later versions. To fix,

Remove line 22,

And replace line 30 with

_clip.WriteSample((byte) (val * 255f));

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