Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
XRControllerHapticSource.cs
Go to the documentation of this file.
1/* ​
2* Copyright (c) 2023 Go Touch VR SAS. All rights reserved. ​
3* ​
4*/
5
6using System.Collections;
7using System.Collections.Generic;
8using UnityEngine;
9using Interhaptics;
10
12{
14 {
15 [SerializeField]
16 private bool playOnBothControllers;
17 [SerializeField]
18 private bool playLeftFirst;
19 [SerializeField]
20 private bool stopHapticsExitTriggerCollision;
21 [SerializeField]
22 private GameObject controllerLeft;
23 [SerializeField]
24 private GameObject controllerRight;
25
26 private GameObject controller => playLeftFirst ? controllerLeft : controllerRight;
27
28 protected override void Start()
29 {
30 base.Start();
31 }
32
33 public IEnumerator XRControlVibration(GameObject hbp)
34 {
35 DebugMode(string.Format("Started playing haptics! + {0}", Time.time));
36 Play();
37 yield return new WaitForSeconds((float)hapticEffectDuration);
38 RemoveTarget(hbp);
39 DebugMode(string.Format("Finished playing haptics at timestamp : + {0} at {1}", hapticEffectDuration, Time.time));
40 }
41
42 protected override void OnCollisionEnter(Collision other)
43 {
44 DebugMode("Collision:" + hapticEffectDuration + other.gameObject);
45 ActivateHaptics(other.gameObject);
46 }
47
48 protected override void OnTriggerEnter(Collider other)
49 {
50 DebugMode("Trigger:" + hapticEffectDuration + other.gameObject);
51 ActivateHaptics(other.gameObject);
52 }
53
54 private void ActivateHaptics(GameObject other)
55 {
56 if ((playOnBothControllers) || (other == controller))
57 {
58 AddTarget(other);
59 }
61 {
62 StartCoroutine(XRControlVibration(other));
63 }
64 else
65 {
66 Play();
67 }
68 }
69
70 protected override void OnTriggerExit(Collider other)
71 {
72 if (stopHapticsExitTriggerCollision)
73 {
74 RemoveTarget(other.gameObject);
75 }
76 }
77
78 protected override void OnCollisionExit(Collision other)
79 {
80 if (stopHapticsExitTriggerCollision)
81 {
82 RemoveTarget(other.gameObject);
83 }
84 }
85 }
86}
87
virtual void Play()
Call this method to play the haptic effect.
void DebugMode(string debugMessage)
Debug method to print messages in the console only when debugMode is enabled.
override void Start()
Initialize the haptic effect settings at the start of the game.
void RemoveTarget(GameObject target)