Tanoda
ThrowableCanDisable.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using JetBrains.Annotations;
5using NaughtyAttributes;
6using UnityEngine;
8#if !UNITY_WEBGL
9using Valve.VR;
10using Valve.VR.InteractionSystem;
11using static Valve.VR.InteractionSystem.Hand;
12#endif
14#if UNITY_WEBGL
15 : MonoBehaviour
16#else
17 : Throwable
18#endif
19{
20 public Transform attachmentOffsetLeft;
21 private Transform backupAttachmentOffset;
22 [ReadOnly]
23 [SerializeField]
24 private Vector3 startPos;
25 private Quaternion startRot;
26#if !UNITY_WEBGL
27 [SerializeField]
28 internal Hand savedHand;
29 public float delayEventSeconds = 1.0f;
30 public UnityEvent DelayedOnPickUp;
31 public bool autoDisableEnableOnDrop = false;
32 public SteamVR_Action_Boolean forceDropWithController;
33 [HideInInspector]
34 public float pickedUpTime = 0.0f;
35
36 public Vector3 handRotate;
37
38 internal bool hoveringRN = false;
39 bool isInDelayedAction = false;
40 private bool savedIsSmall = false;
41 private bool savedIsThin = false;
42 private bool isSmall = false;
43 private bool isThin = false;
44 internal Transform originalParent;
45 internal bool actionOver = false;
46 internal bool wrong = false;
47 internal Action onItemReseted;
48 protected override void Awake()
49 {
50 base.Awake();
51 startPos = transform.position;
52 startRot = transform.rotation;
53 }
54
55 protected void Start()
56 {
57 backupAttachmentOffset = attachmentOffset;
58 startPos = transform.position;
59 startRot = transform.rotation;
60 if (onPickUp == null) onPickUp = new UnityEvent();
61 onPickUp.AddListener(DelayedPickup);
62 float volume = 0.0f;
63 if (GetComponentInChildren<MeshRenderer>())
64 {
65 var size = GetComponentInChildren<MeshRenderer>().bounds.size;
66 volume = size.x * size.y * size.z;
67 }
68 if (volume < 0.0004f)
69 {
70 attachmentFlags |= AttachmentFlags.SnapOnAttach;
71 }
72 originalParent = transform.parent;
73 }
74 public bool IsAttachedToHand()
75 {
76 return interactable.attachedToHand;
77 }
78
79 void Update()
80 {
81 if (transform.position.y < -10)
82 {
84 }
85#if DANA
86 if (!IsAttachedToHand() && IsSmall())
87 SmallPickupHelper();
88#endif
89 }
90
91 private void SmallPickupHelper()
92 {
93#if !UNITY_WEBGL
94 var playerHands = Player.instance.hands;
95 foreach (var hand in playerHands)
96 {
97 if (hand is HackedHand hh)
98 {
99 if (!hh.objectAttachmentPoint) continue;
100
101 if (hh.currentAttachedObject) continue;
102
103 if (Vector3.Distance(hh.objectAttachmentPoint.position, transform.position) < 0.05f)
104 {
105 //attachmentOffset = hh.handType is SteamVR_Input_Sources.LeftHand ? attachmentOffsetLeft : backupAttachmentOffset;
106 hh.AttachObject(gameObject, GrabTypes.Grip);
107 Debug.Log($"SmallPickupHelper: Helped picking up: {name}");
108 }
109 }
110 }
111#endif
112 }
113
114 public bool IsSmall()
115 {
116 if (savedIsSmall)
117 return isSmall;
118
119 var bounds = Macro.GetBounds(gameObject);
120
121 if (Macro.LargestAxis(bounds.size) < 0.05f) // kisebb mint 5cm
122 isSmall = true;
123
124 savedIsSmall = true;
125 return isSmall;
126 }
127
128 public bool IsThin()
129 {
130 if (savedIsThin)
131 return isThin;
132
133 var bounds = Macro.GetBounds(gameObject);
134
135 if (Macro.SmallestAxis(bounds.size) < 0.06f) // kisebb mint 3cm
136 isThin = true;
137
138 savedIsThin = true;
139 return isThin;
140 }
141 [Button]
142 public void ResetPosition(bool silent = false)
143 {
144 var rb = GetComponent<Rigidbody>();
145 var wasSleeping = rb.isKinematic;
146 rb.isKinematic = true;
147 rb.isKinematic = false;
148 transform.position = startPos;
149 transform.rotation = startRot;
150//#if UNITY_EDITOR
151 var cti = FindObjectOfType<ControllerToIris>();
152 rb.isKinematic = wasSleeping;
153 transform.position -= cti.lateRepositionOffset;
154 if (rb.useGravity)
155 {
156 rb.useGravity = false;
157 rb.isKinematic = true;
158 }
159 //#endif
160 if (!silent)
161 onItemReseted?.Invoke();
162
163 onItemReseted = null;
164 }
165
166 private void DelayedPickup()
167 {
168 StartCoroutine(DelayedPickupEvent());
169 }
170
171 private IEnumerator DelayedPickupEvent()
172 {
173 isInDelayedAction = true;
174 yield return new WaitForSeconds(delayEventSeconds);
175 DelayedOnPickUp?.Invoke();
176 isInDelayedAction = false;
177 }
178
179 private void OnDisable()
180 {
181 if (isInDelayedAction)
182 {
183 DelayedOnPickUp?.Invoke();
184 }
185 }
186
187 protected override void OnAttachedToHand(Hand hand)
188 {
189#if DANA
191 if (enabled && name.Contains("front") && (GameObject.Find("fake_csavarozas (2)") || GameObject.Find("fake_csavarozas (3)")))
192 {
193 return;
194 }
196#endif
197 savedHand = hand;
198 if(enabled) base.OnAttachedToHand(hand);
199 }
200
201 [Button]
202 public void ForceHoverBegin()
203 {
204 OnHandHoverBegin(default);
205 }
206
207 [Button]
208 public void ForceHoverEnd()
209 {
210 OnHandHoverEnd(default);
211 }
212
213 protected override void OnHandHoverBegin(Hand hand)
214 {
215#if DANA
217 //if (!enabled)
218 //{
219 // hand.GetComponentInChildren<HandColorer>().ChangeColor(Color.red);
220 //}
221 //if (name.Contains("front") && (GameObject.Find("fake_csavarozas (2)") || GameObject.Find("fake_csavarozas (3)")))
222 //{
223 // return;
224 //}
226#endif
227 if (enabled)
228 {
229 if (hand == default)
230 {
231 interactable.SendMessage("OnHandHoverBegin", GazePickupHelper.instance.htRight, SendMessageOptions.DontRequireReceiver);
232 hoveringRN = true;
233 }
234 else
235 {
236 base.OnHandHoverBegin(hand);
237 }
238 }
239 }
240
241 protected override void OnHandHoverEnd(Hand hand)
242 {
243 if (hand == default)
244 {
245 interactable.SendMessage("OnHandHoverEnd", GazePickupHelper.instance.htRight, SendMessageOptions.DontRequireReceiver);
246 hoveringRN = false;
247 }
248 else
249 {
250 base.OnHandHoverEnd(hand);
251 }
252 }
253
254 public Vector3 localOffset = Vector3.zero;
255
256 private bool IsInPos(Hand hand, OffsetHolder oh)
257 {
258 var go = oh.gameObject;
259 if (hand.handType == SteamVR_Input_Sources.RightHand)
260 {
261 //var wp = go.transform.TransformPoint(oh.Offset);
262 var rootPos = PreviewHand.Instance.GetHandRootPos(oh);
263 return Vector3.Distance(rootPos, Macro.FindDeepChild(hand.transform, "Root").position) <= 0.05f;
264 }
265 if (hand.handType == SteamVR_Input_Sources.LeftHand)
266 {
267 //var wp = go.transform.TransformPoint(oh.LeftOffset);
269 //return Vector3.Distance(wp, hand.transform.position) <= 0.05f;
270 var rootPos = PreviewHand.Instance.GetHandRootPos(oh, true);
271 return Vector3.Distance(rootPos, Macro.FindDeepChild(hand.transform, "Root").position) <= 0.05f;
272 }
273 return false;
274 }
275
276 protected override void HandHoverUpdate(Hand hand)
277 {
278 if (enabled)
279 {
280#if !DANA
281 var thg = GetComponent<TwoHandGrab>();
282 if (thg != null)
283 {
284 var oh = GetComponent<OffsetHolder>();
285 if (!IsInPos(hand, oh) || !IsInPos(hand.otherHand, oh))
286 {
287 if (forceDropWithController != null && forceDropWithController.GetState(hand.handType))
288 {
289 ForceDrop(false);
290 }
291 return;
292 }
293 }
294#endif
295 attachmentOffset = hand.name.Contains("Left") ? attachmentOffsetLeft : backupAttachmentOffset;
296 pickedUpTime = Time.time;
297 base.HandHoverUpdate(hand);
298 if (forceDropWithController != null && forceDropWithController.GetState(hand.handType))
299 {
300 ForceDrop(false);
301 }
302 }
303 else
304 {
305#if DANA
306 hand.GetComponentInChildren<HandColorer>()?.ChangeColor(Color.red);
307#endif
308 }
309 }
310
311 [CanBeNull]
312 public Hand GetHand()
313 {
314 return savedHand;
315 }
316 [Button]
317 public void ForceDrop()
318 {
319 ForceDrop(false);
320 }
321
322 public void ForceDrop(bool returnToStart = false)
323 {
324 if (savedHand)
325 {
326 try
327 {
328 savedHand.DetachObject(gameObject);
330 {
331 enabled = false;
332 StartCoroutine(DelayedEnable());
333 }
334 if (returnToStart)
335 {
336 StartCoroutine(Lerp());
337 }
338 }
339 catch (Exception)
340 {
341 // ignored
342 }
343 }
344 }
345
346 public void ForceAttach()
347 {
348 if (savedHand)
349 {
350 try
351 {
352 attachmentOffset = savedHand.name.Contains("Left") ? attachmentOffsetLeft : backupAttachmentOffset;
353 savedHand.AttachObject(gameObject, GrabTypes.Grip, attachmentOffset: attachmentOffset);
354 }
355 catch (Exception)
356 {
357 // ignored
358 }
359 }
360 }
361
362#if UNITY_EDITOR
363 [Button]
364 private void ForcePickupEditor()
365 {
366 FindObjectOfType<HackedHand>().AttachObject(gameObject, GrabTypes.Grip, attachmentOffset: attachmentOffset);
367 }
368#endif
369
370 public void ForcePickup()
371 {
372 (savedHand ? savedHand : FindObjectOfType<HackedHand>()).AttachObject(gameObject, GrabTypes.Grip, attachmentOffset: attachmentOffset);
373 }
374 public void ForcePickupLeft()
375 {
376 foreach (var hand in FindObjectsOfType<HackedHand>())
377 {
378 if (hand.handType == SteamVR_Input_Sources.LeftHand)
379 {
380 hand.AttachObject(gameObject, GrabTypes.Grip, attachmentOffset: attachmentOffset);
381 return;
382 }
383 }
384 }
385
386 private IEnumerator Lerp()
387 {
388 float time = 0;
389 var devOffset = Vector3.zero;
390
391//#if UNITY_EDITOR
392 var cti = FindObjectOfType<ControllerToIris>();
393 devOffset -= cti.lateRepositionOffset;
394//#endif
395 while (time <= 0.5f)
396 {
397 time += Time.deltaTime;
398 transform.position = Vector3.Lerp(transform.position, startPos + devOffset, time / 0.5f);
399 transform.rotation = Quaternion.Lerp(transform.rotation, startRot, time / 0.5f);
400 yield return new WaitForEndOfFrame();
401 }
402 }
403
404 private IEnumerator DelayedEnable()
405 {
406 yield return new WaitForSeconds(0.6f);
407 wrong = false;
408 if (!actionOver)
409 {
410 enabled = true;
411 }
412 yield return null;
413 }
414#endif
415 }
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
static GazePickupHelper instance
Definition: Macro.cs:12
static Bounds GetBounds(GameObject go)
Definition: Macro.cs:482
static float SmallestAxis(Vector3 v)
Definition: Macro.cs:185
static float LargestAxis(Vector3 v)
Definition: Macro.cs:198
static Transform FindDeepChild(Transform aParent, string aName)
Definition: Macro.cs:149
Vector3 GetHandRootPos(OffsetHolder oh, bool left=false)
Definition: PreviewHand.cs:107
static PreviewHand Instance
Definition: PreviewHand.cs:16
void ForceDrop(bool returnToStart=false)
override void OnAttachedToHand(Hand hand)
override void HandHoverUpdate(Hand hand)
SteamVR_Action_Boolean forceDropWithController
override void OnHandHoverBegin(Hand hand)
void ResetPosition(bool silent=false)
override void OnHandHoverEnd(Hand hand)