-
Notifications
You must be signed in to change notification settings - Fork 304
/
XRHaptics.cs
94 lines (82 loc) · 3.5 KB
/
XRHaptics.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#if ENABLE_VR || UNITY_GAMECORE
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.XR;
using UnityEngine.UI;
public class XRHaptics : MonoBehaviour
{
public Image leftHapticDetected;
public Image leftTryingToRumble;
public Image leftTryingToRumbleHalf;
public Image rightHapticDetected;
public Image rightTryingToRumble;
public Image rightTryingToRumbleHalf;
public float m_RumblePeriod = 1f;
private RumbleState state;
private float m_Timer = 0f;
private enum RumbleState
{
Left,
LeftHalf,
Right,
RightHalf
}
public void Update()
{
var leftHandController = InputSystem.GetDevice<XRControllerWithRumble>(CommonUsages.LeftHand);
leftHapticDetected.color = leftHandController != null ? Color.red : Color.white;
var rightHandController = InputSystem.GetDevice<XRControllerWithRumble>(CommonUsages.RightHand);
rightHapticDetected.color = rightHandController != null ? Color.red : Color.white;
UpdateTimer();
}
// Swap rumble between left and right hand controllers about once per period.
private void UpdateTimer()
{
m_Timer += Time.deltaTime;
if (m_Timer >= m_RumblePeriod)
{
m_Timer -= m_RumblePeriod;
XRControllerWithRumble controller;
switch (state)
{
case RumbleState.Left:
controller = InputSystem.GetDevice<XRControllerWithRumble>(CommonUsages.LeftHand);
controller.SendImpulse(1f, m_RumblePeriod);
leftTryingToRumble.color = Color.red;
leftTryingToRumbleHalf.color = Color.white;
rightTryingToRumble.color = Color.white;
rightTryingToRumbleHalf.color = Color.white;
state = RumbleState.LeftHalf;
break;
case RumbleState.LeftHalf:
controller = InputSystem.GetDevice<XRControllerWithRumble>(CommonUsages.LeftHand);
controller.SendImpulse(0.5f, m_RumblePeriod);
leftTryingToRumble.color = Color.white;
leftTryingToRumbleHalf.color = Color.red;
rightTryingToRumble.color = Color.white;
rightTryingToRumbleHalf.color = Color.white;
state = RumbleState.Right;
break;
case RumbleState.Right:
controller = InputSystem.GetDevice<XRControllerWithRumble>(CommonUsages.RightHand);
controller.SendImpulse(1f, m_RumblePeriod);
leftTryingToRumble.color = Color.white;
leftTryingToRumbleHalf.color = Color.white;
rightTryingToRumble.color = Color.red;
rightTryingToRumbleHalf.color = Color.white;
state = RumbleState.RightHalf;
break;
case RumbleState.RightHalf:
controller = InputSystem.GetDevice<XRControllerWithRumble>(CommonUsages.RightHand);
controller.SendImpulse(0.5f, m_RumblePeriod);
leftTryingToRumble.color = Color.white;
leftTryingToRumbleHalf.color = Color.white;
rightTryingToRumble.color = Color.white;
rightTryingToRumbleHalf.color = Color.red;
state = RumbleState.Left;
break;
}
}
}
}
#endif