OvrGrabObj.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System;
  2. using UnityEngine;
  3. public class OvrGrabObj : MonoBehaviour
  4. {
  5. private void Awake()
  6. {
  7. this.m_nOvrGrabHandLayerNo = LayerMask.NameToLayer("OvrGrabHand");
  8. }
  9. private void Start()
  10. {
  11. if (this.m_trTarget == null)
  12. {
  13. this.m_trTarget = base.transform;
  14. }
  15. this.m_rb = base.GetComponent<Rigidbody>();
  16. if (this.m_rb != null)
  17. {
  18. this.m_rb.maxAngularVelocity = float.PositiveInfinity;
  19. this.m_rb.sleepThreshold = -1f;
  20. }
  21. }
  22. private void OnDisable()
  23. {
  24. this.m_bHitL = (this.m_bHitR = false);
  25. this.m_bGrabSubR = (this.m_bGrabL = (this.m_bGrabSubL = (this.m_bGrabR = false)));
  26. this.m_bEnterCountR = (this.m_bEnterCountL = 0);
  27. if (this.m_goDummyChild != null)
  28. {
  29. UnityEngine.Object.Destroy(this.m_goDummyChild);
  30. }
  31. }
  32. private void OnTriggerEnter(Collider other)
  33. {
  34. if (other.gameObject.layer != this.m_nOvrGrabHandLayerNo)
  35. {
  36. return;
  37. }
  38. OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
  39. OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
  40. if (other.gameObject == left_controller.grip_collider.gameObject)
  41. {
  42. this.m_bHitL = true;
  43. this.m_bGrabSubL = false;
  44. this.m_bEnterCountL++;
  45. }
  46. if (other.gameObject == right_controller.grip_collider.gameObject)
  47. {
  48. this.m_bHitR = true;
  49. this.m_bGrabSubR = false;
  50. this.m_bEnterCountR++;
  51. }
  52. }
  53. private void OnTriggerExit(Collider other)
  54. {
  55. if (other.gameObject.layer != this.m_nOvrGrabHandLayerNo)
  56. {
  57. return;
  58. }
  59. OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
  60. OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
  61. if (other.gameObject == left_controller.grip_collider.gameObject)
  62. {
  63. this.m_bEnterCountL--;
  64. this.m_bHitL = false;
  65. this.m_bGrabSubL = false;
  66. this.m_bEnterCountL = 0;
  67. }
  68. if (other.gameObject == right_controller.grip_collider.gameObject)
  69. {
  70. this.m_bEnterCountR--;
  71. this.m_bHitR = false;
  72. this.m_bGrabSubR = false;
  73. this.m_bEnterCountR = 0;
  74. }
  75. }
  76. private void OnTriggerStay(Collider other)
  77. {
  78. if (other.gameObject.layer != this.m_nOvrGrabHandLayerNo)
  79. {
  80. return;
  81. }
  82. OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
  83. OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
  84. if (other.gameObject == left_controller.grip_collider.gameObject)
  85. {
  86. this.m_bHitL = true;
  87. }
  88. if (other.gameObject == right_controller.grip_collider.gameObject)
  89. {
  90. this.m_bHitR = true;
  91. }
  92. }
  93. private void FixedUpdate()
  94. {
  95. this.m_bHitR = (this.m_bHitL = false);
  96. if (this.m_bGrabToKinematic && this.m_rb != null && this.isThrow)
  97. {
  98. this.isThrow = false;
  99. this.m_rb.isKinematic = false;
  100. Vector3 a = this.nowVelocity / this.fThrowDeltaTime;
  101. Vector3 a2 = this.nowAngularVelocity / this.fThrowDeltaTime;
  102. this.m_rb.AddForce(a * this.m_fGrabThrowRate, ForceMode.VelocityChange);
  103. this.m_rb.AddTorque(a2 * this.m_fGrabThrowRate, ForceMode.VelocityChange);
  104. }
  105. }
  106. private void Update()
  107. {
  108. OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
  109. OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
  110. if (this.m_bHitL)
  111. {
  112. if (!this.m_bGrabR)
  113. {
  114. if (left_controller.controller_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  115. {
  116. this.m_bGrabL = true;
  117. Transform transform = left_controller.grip_collider.gameObject.transform;
  118. if (this.m_goDummyChild != null)
  119. {
  120. UnityEngine.Object.Destroy(this.m_goDummyChild);
  121. }
  122. this.m_goDummyChild = new GameObject("DummyGrabChild");
  123. this.m_goDummyChild.transform.position = this.m_trTarget.position;
  124. this.m_goDummyChild.transform.rotation = this.m_trTarget.rotation;
  125. this.m_goDummyChild.transform.SetParent(transform, true);
  126. }
  127. }
  128. else if (this.m_bGrabR && this.m_bScaleEnable)
  129. {
  130. if (left_controller.controller_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  131. {
  132. this.m_bGrabSubL = true;
  133. this.m_fDistanceHandBack = Vector3.Distance(left_controller.grip_collider.transform.position, right_controller.grip_collider.transform.position);
  134. this.m_vBackScale = this.m_trTarget.localScale;
  135. }
  136. if (this.m_bGrabSubL)
  137. {
  138. if (left_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  139. {
  140. float num = Vector3.Distance(left_controller.grip_collider.transform.position, right_controller.grip_collider.transform.position);
  141. float d = num / this.m_fDistanceHandBack;
  142. this.m_trTarget.localScale = this.m_vBackScale * d;
  143. }
  144. else
  145. {
  146. this.m_bGrabSubL = false;
  147. }
  148. }
  149. }
  150. else
  151. {
  152. this.m_bGrabSubL = false;
  153. }
  154. }
  155. if (this.m_bGrabL)
  156. {
  157. if (left_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  158. {
  159. if (this.m_goDummyChild != null)
  160. {
  161. this.m_trTarget.position = Vector3.Slerp(this.m_trTarget.position, this.m_goDummyChild.transform.position, Time.deltaTime * this.m_fEaseRate);
  162. this.m_trTarget.rotation = Quaternion.Slerp(this.m_trTarget.rotation, this.m_goDummyChild.transform.rotation, Time.deltaTime * this.m_fEaseRate);
  163. }
  164. }
  165. else
  166. {
  167. if (this.m_goDummyChild != null)
  168. {
  169. UnityEngine.Object.Destroy(this.m_goDummyChild);
  170. }
  171. this.m_bGrabL = false;
  172. if (this.m_bHitR && right_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  173. {
  174. this.m_bGrabR = true;
  175. Transform transform2 = right_controller.grip_collider.gameObject.transform;
  176. if (this.m_goDummyChild != null)
  177. {
  178. UnityEngine.Object.Destroy(this.m_goDummyChild);
  179. }
  180. this.m_goDummyChild = new GameObject("DummyGrabChild");
  181. this.m_goDummyChild.transform.position = this.m_trTarget.position;
  182. this.m_goDummyChild.transform.rotation = this.m_trTarget.rotation;
  183. this.m_goDummyChild.transform.SetParent(transform2, true);
  184. }
  185. }
  186. }
  187. if (this.m_bHitR)
  188. {
  189. if (!this.m_bGrabL)
  190. {
  191. if (right_controller.controller_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  192. {
  193. this.m_bGrabR = true;
  194. Transform transform3 = right_controller.grip_collider.gameObject.transform;
  195. if (this.m_goDummyChild != null)
  196. {
  197. UnityEngine.Object.Destroy(this.m_goDummyChild);
  198. }
  199. this.m_goDummyChild = new GameObject("DummyGrabChild");
  200. this.m_goDummyChild.transform.position = this.m_trTarget.position;
  201. this.m_goDummyChild.transform.rotation = this.m_trTarget.rotation;
  202. this.m_goDummyChild.transform.SetParent(transform3, true);
  203. }
  204. }
  205. else if (this.m_bGrabL && this.m_bScaleEnable)
  206. {
  207. if (right_controller.controller_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  208. {
  209. this.m_bGrabSubR = true;
  210. this.m_fDistanceHandBack = Vector3.Distance(left_controller.grip_collider.transform.position, right_controller.grip_collider.transform.position);
  211. this.m_vBackScale = this.m_trTarget.localScale;
  212. }
  213. if (this.m_bGrabSubR)
  214. {
  215. if (right_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  216. {
  217. float num2 = Vector3.Distance(left_controller.grip_collider.transform.position, right_controller.grip_collider.transform.position);
  218. float d2 = num2 / this.m_fDistanceHandBack;
  219. this.m_trTarget.localScale = this.m_vBackScale * d2;
  220. }
  221. else
  222. {
  223. this.m_bGrabSubR = false;
  224. }
  225. }
  226. }
  227. else
  228. {
  229. this.m_bGrabSubR = false;
  230. }
  231. }
  232. if (this.m_bGrabR)
  233. {
  234. if (right_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  235. {
  236. if (this.m_goDummyChild != null)
  237. {
  238. this.m_trTarget.position = Vector3.Slerp(this.m_trTarget.position, this.m_goDummyChild.transform.position, Time.deltaTime * this.m_fEaseRate);
  239. this.m_trTarget.rotation = Quaternion.Slerp(this.m_trTarget.rotation, this.m_goDummyChild.transform.rotation, Time.deltaTime * this.m_fEaseRate);
  240. }
  241. }
  242. else
  243. {
  244. if (this.m_goDummyChild != null)
  245. {
  246. UnityEngine.Object.Destroy(this.m_goDummyChild);
  247. }
  248. this.m_bGrabR = false;
  249. if (this.m_bHitL && left_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
  250. {
  251. this.m_bGrabL = true;
  252. Transform transform4 = left_controller.grip_collider.gameObject.transform;
  253. if (this.m_goDummyChild != null)
  254. {
  255. UnityEngine.Object.Destroy(this.m_goDummyChild);
  256. }
  257. this.m_goDummyChild = new GameObject("DummyGrabChild");
  258. this.m_goDummyChild.transform.position = this.m_trTarget.position;
  259. this.m_goDummyChild.transform.rotation = this.m_trTarget.rotation;
  260. this.m_goDummyChild.transform.SetParent(transform4, true);
  261. }
  262. }
  263. }
  264. Vector3 vector = Vector3.zero;
  265. Quaternion nowRotation = Quaternion.identity;
  266. if (this.m_bGrabToKinematic)
  267. {
  268. if (this.m_rb != null)
  269. {
  270. if (this.m_bGrabL || this.m_bGrabR)
  271. {
  272. OvrMgr.OvrObject ovr_obj = GameMain.Instance.OvrMgr.ovr_obj;
  273. vector = ((!this.m_bGrabL) ? ovr_obj.right_controller.hand_trans.position : ovr_obj.left_controller.hand_trans.position);
  274. nowRotation = ((!this.m_bGrabL) ? ovr_obj.right_controller.hand_trans.rotation : ovr_obj.left_controller.hand_trans.rotation);
  275. this.beforeRotation = this.nowRotation;
  276. this.nowVelocity = vector - this.m_vBackPos;
  277. this.nowRotation = nowRotation;
  278. if (!this.isBeforeGrabL && !this.isBeforeGrabR)
  279. {
  280. this.m_rb.isKinematic = true;
  281. this.beforeRotation = this.nowRotation;
  282. }
  283. this.fThrowDeltaTime = Time.deltaTime;
  284. }
  285. else if (this.isBeforeGrabL || this.isBeforeGrabR)
  286. {
  287. this.isThrow = true;
  288. }
  289. }
  290. this.isBeforeGrabL = this.m_bGrabL;
  291. this.isBeforeGrabR = this.m_bGrabR;
  292. }
  293. this.m_vBackPos = vector;
  294. }
  295. public Vector3 nowVelocity { get; private set; }
  296. public Quaternion nowRotation { get; private set; }
  297. public Quaternion beforeRotation { get; private set; }
  298. public Vector3 nowAngularVelocity
  299. {
  300. get
  301. {
  302. float num;
  303. Vector3 a;
  304. (this.nowRotation * Quaternion.Inverse(this.beforeRotation)).ToAngleAxis(out num, out a);
  305. num *= 0.0174532924f;
  306. return a * num;
  307. }
  308. }
  309. public bool isBeforeGrabL { get; private set; }
  310. public bool isBeforeGrabR { get; private set; }
  311. private bool isThrow { get; set; }
  312. private float fThrowDeltaTime { get; set; }
  313. public Transform m_trTarget;
  314. public float m_fEaseRate = 10f;
  315. public bool m_bScaleEnable = true;
  316. public bool m_bGrabToKinematic;
  317. public float m_fGrabThrowRate = 10f;
  318. private int m_nOvrGrabHandLayerNo = 18;
  319. private bool m_bHitL;
  320. private bool m_bGrabL;
  321. private bool m_bGrabSubL;
  322. private bool m_bHitR;
  323. private bool m_bGrabR;
  324. private bool m_bGrabSubR;
  325. private int m_bEnterCountL;
  326. private int m_bEnterCountR;
  327. private Rigidbody m_rb;
  328. private GameObject m_goDummyChild;
  329. private float m_fDistanceHandBack;
  330. private Vector3 m_vBackScale;
  331. private Vector3 m_vBackPos;
  332. }