Tanoda
HandHintSystem.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using NaughtyAttributes;
4using UnityEngine;
5
6public class HandHintSystem : MonoBehaviour
7{
8 public static HandHintSystem instance;
9 public GameObject leftHand, rightHand;
10 public GameObject[] leftNodes, rightNodes;
11
12 private Quaternion[] leftRots, rightRots;
13 [SerializeField]
14 private List<ActionObject> activeActions = new List<ActionObject>();
15
16 private float t = 0.0f;
17
18 public bool autoPilot = false;
19#if DANA
20
21 private void Awake()
22 {
23 if (!instance)
24 instance = this;
25 else
26 Destroy(gameObject);
27 }
28
29 public void ActionStarted(ActionObject ao)
30 {
31 activeActions.Add(ao);
32 t = 0.0f;
33 }
34
35 public void ActionFinished(ActionObject ao)
36 {
37 activeActions.Remove(ao);
38 t = 0.0f;
39 }
40
41 // Start is called before the first frame update
42 void Start()
43 {
44 var lr = new List<Quaternion>();
45 foreach (var o in leftNodes)
46 {
47 lr.Add(o.transform.localRotation);
48 }
49
50 leftRots = lr.ToArray();
51
52 var rr = new List<Quaternion>();
53 foreach (var o in rightNodes)
54 {
55 rr.Add(o.transform.localRotation);
56 }
57
58 rightRots = rr.ToArray();
59 }
60
61 // Update is called once per frame
62 void Update()
63 {
64 if (!autoPilot)
65 return;
66
67 var hand = (SavedUser.instance.handTracking == 2 ? leftHand : rightHand).GetComponent<HackedHand>();
68
69 t += 0.01f;
70 hand.transform.position = Vector3.Slerp(hand.transform.position, GetTargetPosition(hand), t);
71 }
72
73 private Vector3 GetTargetPosition(HackedHand hand)
74 {
75 var retval = Vector3.zero;
76
77 if (activeActions.Count == 0)
78 return retval;
79
80 foreach (var action in activeActions)
81 {
82 if (action is PositionAction pa)
83 {
84 if (pa.GetInputGO() == hand.currentAttachedObject)
85 {
86 retval = pa.GetInputGO().transform.TransformPoint(pa.targetPosition);
87 }
88 else
89 {
90 retval = pa.GetInputGO().transform.TransformPoint(pa.originalPosition);
91 }
92 return retval;
93 }
94 }
95
96 return retval;
97 }
98
99
100 [Button]
101 private void RestoreLeft()
102 {
103 StartCoroutine(restoreFingers(true));
104 }
105
106 [Button]
107 private void RestoreRight()
108 {
109 StartCoroutine(restoreFingers(false));
110 }
111
112 internal void StopMovingFinger()
113 {
114 StopAllCoroutines();
115 }
116
117 internal void RestoreFingers(bool left)
118 {
119 StartCoroutine(restoreFingers(left));
120 }
121
122 IEnumerator restoreFingers(bool left)
123 {
124 for (float i = 0; i < 1; i += 0.01f)
125 {
126 if (left)
127 {
128 for (int j = 0; j < leftNodes.Length; j++)
129 {
130 leftNodes[j].transform.localRotation = Quaternion.Slerp(leftNodes[j].transform.localRotation, leftRots[j], i);
131 }
132 }
133 else
134 {
135 for (int j = 0; j < rightNodes.Length; j++)
136 {
137 rightNodes[j].transform.localRotation = Quaternion.Slerp(rightNodes[j].transform.localRotation, rightRots[j], i);
138 }
139 }
140 yield return new WaitForEndOfFrame();
141 }
142 yield return null;
143 }
144#endif
145}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
GameObject[] rightNodes
static HandHintSystem instance
GameObject leftHand
GameObject rightHand
GameObject[] leftNodes