1using System.Collections;
2using System.Collections.Generic;
11 private GameObject objectToGrab;
12 private Transform attachPoint, attachPointLeft;
13 private Transform originalParent;
14 List<GameObject> cachedGameObjects =
new List<GameObject>();
15 List<ActionObject> cachedActionObjects =
new List<ActionObject>();
17 private bool editingLeft =
false;
20 private Vector3 handPosition;
21 private GameObject rightHand, leftHand;
22 Vector3 targetPosition, targetRotation, originalPosition, originalRotation;
30 rightHand = GameObject.Find(
"SteamVR_Tracker");
31 leftHand = GameObject.Find(
"SteamVR_Tracker");
35 if (rightHand !=
null)
37 handPosition = rightHand.transform.position;
42 if (objectToGrab ==
null)
return;
43 cachedGameObjects.Add(objectToGrab);
44 cachedActionObjects.Add(action);
45 actionObject = action;
46 targetPosition = TargetPos;
47 targetRotation = TargetRot;
48 originalPosition = objectToGrab.transform.localPosition;
49 originalRotation = objectToGrab.transform.localEulerAngles;
52 Debug.LogError(
"couldn't find glove, offset editor cannot be entered");
57 if (cachedGameObjects[0] ==
null)
59 Debug.LogError(
"no selected object, offset editor cannot be entered");
63 Vector3 scale = cachedGameObjects[0].transform.localScale;
64 if (scale.x == 0f || scale.y == 0f || scale.z == 0f)
66 Debug.LogError(
"object has at least one scale component set to 0, offset editor cannot be entered");
71 StartCoroutine(moveTheHand());
79 if (attachPoint !=
null)
87 if (attachPoint !=
null)
89 attachPoint.localScale = Vector3.one;
90 attachPointLeft.localScale = Vector3.one;
93 if (cachedGameObjects.Count > 0)
95 var index = cachedGameObjects.IndexOf(objectToGrab);
96 cachedGameObjects.Remove(objectToGrab);
97 cachedActionObjects.RemoveAt(index);
99 if (cachedGameObjects.Count > 0) StartCoroutine(moveTheHand());
102 if (cachedGameObjects.Count == 0)
109 var go = oh.gameObject;
117 var ap = hr.transform.Find(
"AttachPoint");
118 var apl = hl.transform.Find(
"AttachPoint");
120 ap.SetParent(
null,
true);
121 apl.SetParent(
null,
true);
123 hr.transform.SetParent(ap,
true);
124 hl.transform.SetParent(apl,
true);
126 ap.position = go.transform.TransformPoint(oh.
Offset);
127 ap.rotation = go.transform.rotation * Quaternion.Euler(oh.
Rotation);
128 apl.position = go.transform.TransformPoint(oh.
LeftOffset);
129 apl.rotation = go.transform.rotation * (Quaternion.Euler(oh.
LeftRotation));
133 Destroy(ap.gameObject);
134 Destroy(apl.gameObject);
140 IEnumerator moveTheHand()
142 if (cachedGameObjects[0].GetComponent<OffsetHolder>() !=
null)
144 offsetHolder = cachedGameObjects[0].GetComponent<
OffsetHolder>();
146 if (attachPoint ==
null)
148 attachPoint =
handPrefab.transform.Find(
"AttachPoint");
152 if (attachPoint ==
null) yield
break;
154 attachPoint.SetParent(
null,
true);
155 attachPointLeft.SetParent(
null,
true);
158 handPrefab.transform.SetParent(attachPoint,
true);
164 attachPoint.position = cachedGameObjects[0].transform.TransformPoint(o);
165 attachPoint.rotation = cachedGameObjects[0].transform.rotation *
Quaternion.Euler(r);
166 attachPointLeft.position = cachedGameObjects[0].transform.TransformPoint(lo);
167 attachPointLeft.rotation = cachedGameObjects[0].transform.rotation * (
Quaternion.Euler(lr));
171 attachPoint.position = cachedGameObjects[0].transform.TransformPoint(offsetHolder.
Offset);
172 attachPoint.rotation = cachedGameObjects[0].transform.rotation *
Quaternion.Euler(offsetHolder.
Rotation);
173 attachPointLeft.position = cachedGameObjects[0].transform.TransformPoint(offsetHolder.
LeftOffset);
174 attachPointLeft.rotation = cachedGameObjects[0].transform.rotation * (
Quaternion.Euler(offsetHolder.
LeftRotation));
180 originalPosition = attachPoint.position;
181 Vector3 originalLocalPosition = attachPoint.transform.position - cachedGameObjects[0].transform.position;
186 time += Time.deltaTime;
187 if (time >= duration)
190 attachPoint.transform.position = originalPosition;
192 attachPoint.transform.position =
Vector3.Lerp(originalPosition, originalLocalPosition + targetPosition, time / duration);
199 if (cachedGameObjects[0].GetComponent<OffsetHolder>() ==
null)
203 if (attachPoint ==
null)
205 attachPoint =
handPrefab.transform.Find(
"AttachPoint");
209 if (attachPoint ==
null) yield
break;
211 attachPoint.SetParent(
null,
true);
212 attachPointLeft.SetParent(
null,
true);
215 handPrefab.transform.SetParent(attachPoint,
true);
221 attachPoint.position = cachedGameObjects[0].transform.TransformPoint(o);
222 attachPoint.rotation = cachedGameObjects[0].transform.rotation *
Quaternion.Euler(r);
223 attachPointLeft.position = cachedGameObjects[0].transform.TransformPoint(lo);
224 attachPointLeft.rotation = cachedGameObjects[0].transform.rotation * (
Quaternion.Euler(lr));
228 attachPoint.position = cachedGameObjects[0].transform.TransformPoint(offsetTool.attachmentOffset.localPosition);
230 attachPoint.rotation = cachedGameObjects[0].transform.rotation *
Quaternion.Euler(offsetTool.attachmentOffset.localEulerAngles);
231 attachPointLeft.position = cachedGameObjects[0].transform.TransformPoint(offsetTool.attachmentOffsetLeft.localPosition);
233 attachPointLeft.rotation = cachedGameObjects[0].transform.rotation * (
Quaternion.Euler(offsetTool.attachmentOffsetLeft.localEulerAngles));
240 originalPosition = attachPoint.position;
241 Vector3 originalLocalPosition = attachPoint.transform.position - cachedGameObjects[0].transform.position;
244 while (
Vector3.Distance(handPosition, cachedGameObjects[0].transform.position) >= 0.2f)
246 time += Time.deltaTime;
247 if (time >= duration)
250 attachPoint.transform.position = originalPosition;
252 attachPoint.transform.position =
Vector3.Lerp(originalPosition, originalLocalPosition + targetPosition, time / duration);
260 if (cachedGameObjects[0].GetComponent<TwoHandGrab>())
static Transform FindDeepChild(Transform aParent, string aName)
bool GetHandOffsets(out Vector3 offset, out Vector3 rotation, out Vector3 leftoffset, out Vector3 leftrotation)
void TurnOffOnPreviewHand(bool newValue)
void hideHandPreview(GameObject objectToGrab)
void EnterOffsetEditor(GameObject objectToGrab, ActionObject action, Vector3 TargetPos, Vector3 TargetRot)
Vector3 GetHandRootPos(OffsetHolder oh, bool left=false)
GameObject leftHandPrefab2
GameObject leftHandPrefab
GameObject leftTurnableHand
static PreviewHand Instance