Tanoda
TorqueWrench.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using NaughtyAttributes;
4using UnityEngine;
6
7[RequireComponent(typeof(AudioSource), typeof(ThrowableCanDisable))]
8public class TorqueWrench : MonoBehaviour
9{
10 public GameObject helper, helper2;
11 public Vector3 helper2Axis = Vector3.forward;
12 public GameObject FollowObject;
13 public static TorqueWrench instance;
14 public float neededScrewInAngle = 360f;
15 public bool isInUse = false;
16
17 [HideInInspector]
18 public UnityEvent screwedIn = new UnityEvent();
19
20 internal new AudioSource audio;
21 private bool moving = false;
22 private float currentAngle = 0.0f;
23
24 public void Start()
25 {
26 instance = this;
27 audio = GetComponent<AudioSource>();
28 }
29
30 public void AttachSocket(GameObject socket)
31 {
32 socket.transform.SetParent(helper.transform, false);
33 socket.transform.localPosition = new Vector3(0, 0.0074f, 0);
34 socket.transform.localEulerAngles = new Vector3(90, 0, 0);
35 }
36
37 //#if UNITY_EDITOR
38 // public GameObject tesztcucc;
39 //
40 // [Button]
41 // private void Cucc()
42 // {
43 // AttachSocket(tesztcucc);
44 // }
45 //#endif
46
47 [Button]
48 public void StartMoving()
49 {
50 if (moving)
51 return;
52 isInUse = true;
53 currentAngle = 0.0f;
54#if !UNITY_WEBGL
55 FollowObject = GetComponent<ThrowableCanDisable>().savedHand.GetComponent<HackedHand>().objectAttachmentPoint.gameObject;
56 GetComponent<ThrowableCanDisable>().ForceDrop();
57 GetComponent<ThrowableCanDisable>().enabled = false;
58 helper.transform.SetParent(transform.parent);
59 transform.SetParent(helper.transform);
60#endif
61 //moving = true;
62 StartCoroutine(LateStartMoving());
63
64 }
65
66 [Button]
67 internal void DebugStartMoving()
68 {
69 if (moving)
70 return;
71 isInUse = true;
72 currentAngle = 0.0f;
73#if !UNITY_WEBGL
74 helper.transform.SetParent(transform.parent);
75 transform.SetParent(helper.transform);
76#endif
77 //moving = true;
78 StartCoroutine(LateStartMoving());
79
80 }
81
82 IEnumerator LateStartMoving()
83 {
84 yield return null;
85 moving = true;
86 }
87
92 public void StartMoving(GameObject hand)
93 {
94 //FollowObject = hand;
96 }
97
98 [Button]
99 public void StopMoving()
100 {
101 if (!moving)
102 return;
103 currentAngle = 0.0f;
104#if !UNITY_WEBGL
105 var tcd = GetComponent<ThrowableCanDisable>();
106 tcd.enabled = true;
107 transform.SetParent(helper.transform.parent);
108 helper.transform.SetParent(transform);
109 tcd.ResetPosition();
110 tcd.ForceAttach();
111#endif
112 moving = false;
113 FollowObject = null;
114 }
115
116
117 void Update()
118 {
119 if (!moving || !FollowObject) return;
120 helper2.transform.localPosition = helper2Axis * 0.27874f; //(wrench rot pivot)
121 helper2.transform.position = Vector3.MoveTowards(helper2.transform.position, FollowObject.transform.position, 0.2f);
122 var lp = helper2.transform.localPosition;
123 helper2.transform.localPosition = new Vector3(lp.x, 0, lp.z);
124 var lastangle = ConvertQuant2Euler(helper.transform.localRotation);
125 helper.transform.LookAt(helper2.transform, helper.transform.up);
126 var moveDelta = lastangle - ConvertQuant2Euler(helper.transform.localRotation);
127 if (moveDelta.z < -0.01f)
128 {
129 if (!audio.isPlaying)
130 audio.Play();
131 }
132 if (moveDelta.z > 0.01f)
133 {
134 if (moveDelta.z > 180.0f)
135 {
136 Debug.Log("!!!!! moveDelta.z > 180 : " + moveDelta.z);
137 return;
138 }
139 currentAngle += moveDelta.z;
140 if (currentAngle >= neededScrewInAngle)
141 {
142 StopMoving();
143 screwedIn.Invoke();
144 isInUse = false;
145 }
146 }
147 }
148
149 public static Vector3 ConvertQuant2Euler(Quaternion quaternion)
150 {
151 float tempEuler;
152 float[] eulerAngles = new float[3];
153 float[] fixedEulerAngles = new float[3];
154
155 //Convert pitch - X
156 tempEuler = Mathf.Atan2(2 * quaternion.x * quaternion.w + 2 * quaternion.y * quaternion.z,
157 1 - 2 * quaternion.x * quaternion.x - 2 * quaternion.z * quaternion.z);
158 eulerAngles[0] = tempEuler * 180 / Mathf.PI;
159 if (eulerAngles[0] < 0)
160 {
161 fixedEulerAngles[0] = 180 + (180 + eulerAngles[0]);
162 }
163
164 if (eulerAngles[0] > 0)
165 {
166 fixedEulerAngles[0] = eulerAngles[0];
167 }
168
169 //Convert yaw - Y
170 tempEuler = Mathf.Asin(2 * quaternion.x * quaternion.y + 2 * quaternion.z * quaternion.w);
171 eulerAngles[1] = tempEuler * 180 / Mathf.PI;
172 if (eulerAngles[1] < 0)
173 {
174 fixedEulerAngles[1] = 180 + (180 + eulerAngles[1]);
175 }
176
177 if (eulerAngles[1] > 0)
178 {
179 fixedEulerAngles[1] = eulerAngles[1];
180 }
181
182 //Convert roll - Z
183 tempEuler = Mathf.Atan2(2 * quaternion.y * quaternion.w + 2 * quaternion.x * quaternion.z,
184 1 - 2 * quaternion.y * quaternion.y - 2 * quaternion.z * quaternion.z);
185 eulerAngles[2] = tempEuler * 180 / Mathf.PI;
186 if (eulerAngles[2] < 0)
187 {
188 fixedEulerAngles[2] = 180 + (180 + eulerAngles[2]);
189 }
190
191 if (eulerAngles[2] > 0)
192 {
193 fixedEulerAngles[2] = eulerAngles[2];
194 }
195
196 return new Vector3(fixedEulerAngles[0], fixedEulerAngles[1], fixedEulerAngles[2]);
197 }
198}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void ForceDrop()
Definition: HackedHand.cs:824
void StopMoving()
Definition: TorqueWrench.cs:99
GameObject helper2
Definition: TorqueWrench.cs:10
GameObject FollowObject
Definition: TorqueWrench.cs:12
UnityEvent screwedIn
Definition: TorqueWrench.cs:18
void Start()
Definition: TorqueWrench.cs:24
void StartMoving(GameObject hand)
DEPRECATED
Definition: TorqueWrench.cs:92
static Vector3 ConvertQuant2Euler(Quaternion quaternion)
Vector3 helper2Axis
Definition: TorqueWrench.cs:11
static TorqueWrench instance
Definition: TorqueWrench.cs:13
GameObject helper
Definition: TorqueWrench.cs:10
float neededScrewInAngle
Definition: TorqueWrench.cs:14
void StartMoving()
Definition: TorqueWrench.cs:48
void AttachSocket(GameObject socket)
Definition: TorqueWrench.cs:30