-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHapticFeedback.cs
69 lines (56 loc) · 4.11 KB
/
HapticFeedback.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
// ------------------------------------------------------------------------------- HAPTIC STRUCT ---------------------------------------------------------------------------------------------//
// Defined as class instead so that it may appear on Unity Inspector
[System.Serializable]
public class Haptics
{
[Range(0.0f, 1.0f)] [Tooltip("The intensity of the haptic feedback")]
public float amplitude; // The intensity of the haptic feedback
[Range(0.0f, 1.0f)] [Tooltip("The duration (in seconds) of the haptic feedback")]
public float durationSeconds; // The duration (in seconds) of the haptic feedback
public bool isActive = false; // If that particular haptic is active i.e. is being sent
public Haptics(float amplitude, float durationSeconds) // Constructor - Sets amplitude and durationSeconds as input it is initialised with
{
this.amplitude = amplitude;
this.durationSeconds = durationSeconds;
}
}
// ------------------------------------------------------------------------------- UPDATE() ---------------------------------------------------------------------------------------------//
public class HapticFeedback : MonoBehaviour
{
// OBJECT REFERENCES ----------------------------------------------------------------
public XRBaseController leftController, rightController;
// PUBLIC MEMBERS -------------------------------------------------------------------
[Header("Haptic Feedback Attributes")]
public bool hapticFeedbackOn = true; // Global setting for Haptic Feedback being on at all
public Haptics touch = new(0.01f, 0.05f); // Profile for if landscape is touched and not interacted with i.e. manipulated
public Haptics interact = new(0.3f, 0.05f); // Profile for if the landscape is interacted with
/* ------------------------------------------------------------------------- METHODS ---------------------------------------------------------------------------------- //
- SendHaptics(): Sends haptics according to the 'action' i.e. profile it is called with. Sends to Right controller by default
- SendHaptics(): (Overload) Called by specifying the controller in addition to the profile
- SendHaptics(): (Overload) Called with custom amplitude and durationSeconds values if the instantiated profiles don't fit the purpose
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// --------------------------------------------------------------------------
public void SendHaptics(Haptics action) // Defaults to rightController. Called as SendHaptics(touch)
{
rightController.SendHapticImpulse(action.amplitude, action.durationSeconds);
}
// --------------------------------------------------------------------------
public void SendHaptics(XRBaseController controller, Haptics action) // Additional option of controller. Called as SendHaptics(rightController, touch)
{
controller.SendHapticImpulse(action.amplitude, action.durationSeconds);
}
// --------------------------------------------------------------------------
public void SendHaptics(XRBaseController controller, float amplitude, float durationSeconds) // Additional options of custom amplitude and duration with defaults equal to interact Haptics object. Called as SendHaptics(rightController, 0.7f, 0.3f)
{
controller.SendHapticImpulse(amplitude, durationSeconds);
}
public void SendHaptics(Haptics action, float amplitude) // Send to right hand with a specific amplitude proportional to the hand's velocity when interacting
{
rightController.SendHapticImpulse(amplitude, action.durationSeconds);
}
}
// --------------------------------------------------------------------------------------------------------------------------------------------------------------- END OF SCRIPT //