Tanoda
GazePickupHelper.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4#if !UNITY_WEBGL
5using ViveHandTracking;
6using Valve.VR.InteractionSystem;
7#endif
8
9public class GazePickupHelper : MonoBehaviour
10{
11
12#if !UNITY_WEBGL
13 private ThrowableCanDisable lastThrowable;
15 [SerializeField]
16 private float magnetDistance = 0.01f;
17 internal HackedHand htLeft, htRight;
18 public Transform Crosshair;
19 public GameObject target;
20 private ModelRenderer[] htRenderers;
21 public bool logHit = false;
22
23 void Awake()
24 {
25 if (!instance)
26 instance = this;
27 else
28 Destroy(this);
29 }
30
31 void Start()
32 {
33 htLeft = GameObject.Find("LeftHandModelMask").GetComponent<HackedHand>();
34 htRight = GameObject.Find("RightHandModelMask").GetComponent<HackedHand>();
35 htRenderers = FindObjectsOfType<ModelRenderer>();
36 }
37
38 void FixedUpdate()
39 {
40 return;
41#if !UNITY_WEBGL && DANA
42
43 int layerMask = /*(1 << 8) +*/ (1 << 9) + (1 << 5);
44 layerMask = ~layerMask;
45
46 if (Physics.Raycast(transform.position, transform.forward + (-transform.up * 0.1f), out var hit, Mathf.Infinity, layerMask))
47 {
48#if UNITY_EDITOR
49 if (logHit)
50 {
51 Debug.Log(hit.transform.gameObject);
52 }
53#endif
54 Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.magenta);
55 Crosshair.position = hit.point;
56 target = hit.transform.gameObject;
57 UpdateTargets(target);
58 Crosshair.localPosition -= Vector3.forward * 0.01f;
59 if (hit.transform.gameObject && hit.transform.gameObject.GetComponent<ThrowableCanDisable>() is ThrowableCanDisable tcd && tcd.enabled)
60 {
61 if (!tcd.hoveringRN)
62 {
63 if (lastThrowable && lastThrowable != tcd)
64 {
65 lastThrowable.ForceHoverEnd();
66 }
67 tcd.ForceHoverBegin();
68 lastThrowable = tcd;
69 }
70
71 if (!tcd.IsAttachedToHand() && tcd.pickedUpTime + 2.0f < Time.time)
72 {
73 var left = Vector3.Distance(tcd.transform.position, htLeft.transform.position) < magnetDistance;
74 var right = Vector3.Distance(tcd.transform.position, htRight.transform.position) < magnetDistance;
75
76 if (left)
77 {
78 htLeft.AttachObject(tcd.gameObject, GrabTypes.Grip, tcd.attachmentFlags, tcd.attachmentOffsetLeft);
79 }
80 else if (right)
81 {
82 htRight.AttachObject(tcd.gameObject, GrabTypes.Grip, tcd.attachmentFlags, tcd.attachmentOffset);
83 }
84 }
85
86 }
87 else if (lastThrowable != null && lastThrowable.hoveringRN)
88 {
89 lastThrowable.ForceHoverEnd();
90 }
91 }
92 else
93 {
94 UpdateTargets(null);
95 Debug.DrawRay(transform.position, transform.forward * 1000, Color.red);
96 if (lastThrowable != null && lastThrowable.hoveringRN)
97 {
98 lastThrowable.ForceHoverEnd();
99 }
100 }
101#endif
102 }
103
104 private void UpdateTargets(GameObject target)
105 {
106 if (htRenderers != null)
107 foreach (var modelRenderer in htRenderers)
108 {
109 modelRenderer?.GazeTargetUpdate(target);
110 }
111 }
112#else
113 public static GazePickupHelper instance;
114 public GameObject target;
115#endif
116}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
static GazePickupHelper instance
override void AttachObject(GameObject objectToAttach, GrabTypes grabbedWithType, AttachmentFlags flags=defaultAttachmentFlags, Transform attachmentOffset=null)
Definition: HackedHand.cs:474