forked from oculus-samples/Unity-Phanto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHapticsDemoController.cs
79 lines (58 loc) · 2.21 KB
/
HapticsDemoController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Oculus.Haptics;
using TMPro;
using UnityEngine;
public class HapticsDemoController : MonoBehaviour
{
private const string Instructions1 = "Squeeze the hand grip to start the haptic effect.";
private const string Instructions2 = "* Pull trigger to increase amplitude.\n* Move thumbstick left and right to change frequency shift.\n\namp: {0:F2}\nfreq: {1:F2}";
[SerializeField] private HapticClip loopingClip;
[SerializeField] private OVRInput.Controller inputController = OVRInput.Controller.RTouch;
[SerializeField] private Controller hapticController = Controller.Right;
[SerializeField, Range(0, 5)] private float amplitudeShiftScale = 5.0f;
[SerializeField] private TextMeshProUGUI text;
private Transform _transform;
private HapticClipPlayer _loopingPlayer;
private bool _isPlaying;
private void Awake()
{
_transform = transform;
_loopingPlayer = new HapticClipPlayer(loopingClip);
_loopingPlayer.isLooping = true;
}
private void OnEnable()
{
text.text = Instructions1;
}
private void OnDestroy()
{
_loopingPlayer?.Dispose();
}
private void Update()
{
var trigger = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, inputController);
var grip = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, inputController);
var thumbstick = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick, inputController);
var shouldPlay = grip > 0.01f;
if (!_isPlaying && shouldPlay)
{
_loopingPlayer.Play(hapticController);
_isPlaying = true;
}
else if (_isPlaying && !shouldPlay)
{
_loopingPlayer.Stop();
_isPlaying = false;
text.text = Instructions1;
}
if (!_isPlaying)
{
return;
}
var amplitude = MathUtils.Remap(0, 1, 1, amplitudeShiftScale, trigger);
_loopingPlayer.amplitude = amplitude;
var freqShift = Mathf.Clamp(thumbstick.x, -1, 1);
_loopingPlayer.frequencyShift = freqShift;
text.text = string.Format(Instructions2, amplitude, freqShift);
}
}