Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
SpatialHapticSource.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
11namespace Interhaptics
12{
13 [AddComponentMenu("Interhaptics/SpatialHapticSource")]
14 [RequireComponent(typeof(Rigidbody))]
15 public class SpatialHapticSource : Internal.HapticSource
16 {
17
18#region Enums
19 public enum PlayMethod
20 {
24 }
25
27#endregion
28
29#region Lifecycle
30 protected override void Start()
31 {
32 isLooping = false;
33 playAtStart = false;
34 base.Start();
35 }
36 #endregion
37
38 public override void PlayEventVibration()
39 {
40 base.PlayEventVibration();
41 }
42
47 public override IEnumerator ControlVibration()
48 {
49 DebugMode(string.Format("Started playing haptics on Spatial HS! + {0}",Time.time));
50 Play();
51 yield return new WaitForSeconds((float)hapticEffectDuration);
52 DebugMode(string.Format("Finished playing haptics at timestamp on SpatialHS : + {0} at {1}", hapticEffectDuration, Time.time));
53 }
54
55#if UNITY_EDITOR
56 [System.Diagnostics.Conditional("UNITY_EDITOR")]
57 public void Reset()
58 {
59 SphereCollider sphere = GetComponent<SphereCollider>();
60 BoxCollider box = GetComponent<BoxCollider>();
61
62 if (sphere == null && box == null)
63 {
64 if (UnityEditor.EditorUtility.DisplayDialog("Choose a Component", "You are missing one of the required componets. Please choose one to add", "SphereCollider", "BoxCollider"))
65 {
66 gameObject.AddComponent<SphereCollider>().isTrigger = true;
67 }
68 else
69 {
70 gameObject.AddComponent<BoxCollider>().isTrigger = true;
71 }
72 }
73 gameObject.GetComponent<Rigidbody>().useGravity = false;
74 }
75#endif
76
77 public void AddTarget(GameObject target)
78 {
79 if (target.TryGetComponent(out HapticBodyPart hapticBodyPart))
80 {
81 hapticBodyPart.HapticMaterialId = this.HapticMaterialId;
82 AddTarget(hapticBodyPart.ToCommandData());
83 hapticBodyPart.UpdateTargetIntensity(hapticBodyPart.TargetIntensity); // Update the target intensity when adding the target
84 }
85 }
86
87 public void RemoveTarget(GameObject target)
88 {
89 if (target.TryGetComponent(out HapticBodyPart hbp))
90 {
91 RemoveTarget(hbp.ToCommandData());
92 }
93 }
94
95 protected virtual void OnCollisionEnter(Collision other)
96 {
97 if ((playMethod == PlayMethod.OnCollision)&&(other.gameObject.GetComponent<HapticBodyPart>() != null))
98 {
99 ActivateHaptics(other.gameObject);
100 }
101 }
102
103 protected virtual void OnTriggerEnter(Collider other)
104 {
105 if ((playMethod == PlayMethod.OnTrigger) && (other.gameObject.GetComponent<HapticBodyPart>()!=null))
106 {
107 ActivateHaptics(other.gameObject);
108 }
109 }
110
111 private void ActivateHaptics(GameObject other)
112 {
113 AddTarget(other);
114 if (hapticEffectDuration > 0)
115 {
117 }
118 else
119 {
120 Play();
121 }
122 }
123
124 protected virtual void OnTriggerExit(Collider other)
125 {
126 RemoveTarget(other.gameObject);
127 }
128
129 protected virtual void OnCollisionExit(Collision other)
130 {
131 RemoveTarget(other.gameObject);
132 }
133
134 }
135}
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.
void RemoveTarget(GameObject target)
override void PlayEventVibration()
Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibratio...
virtual void OnCollisionExit(Collision other)
virtual void OnTriggerExit(Collider other)
override IEnumerator ControlVibration()
Controls the vibration perception based on the full length of the haptic material; stops any residual...
virtual void OnCollisionEnter(Collision other)
override void Start()
Initialize the haptic effect settings at the start of the game.
virtual void OnTriggerEnter(Collider other)