OvrGrabObj.cs 11 KB

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