CustomInteractible.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using Valve.VR;
  6. public class CustomInteractible : MonoBehaviour {
  7. public bool isInteractible = true;
  8. public List<SteamVR_Skeleton_Poser> grabPoints,secondPoses; //not influenting on rotation posers
  9. public CustomHand leftHand, rightHand;//hand which currently holding an object
  10. public SteamVR_Skeleton_Poser leftMyGrabPoser, rightMyGrabPoser;//current holding posers
  11. public bool TwoHanded, useSecondPose, HideController;//two handed interaction, use posers which influent on rotation, hide controllers
  12. public CustomHand.GrabType grabType=CustomHand.GrabType.Grip;//how object should be grabbed
  13. [Header("SoundEvents")]
  14. public bool pickReleaseOnce; //sound if all hands are released or picked both hands
  15. public UnityEvent Grab;
  16. public UnityEvent ReleaseHand;
  17. //
  18. public Transform GetMyGrabPoserTransform() {
  19. if (leftMyGrabPoser)
  20. return leftMyGrabPoser.transform;
  21. if (rightMyGrabPoser)
  22. return rightMyGrabPoser.transform;
  23. return null;
  24. }
  25. public Transform GetMyGrabPoserTransform(CustomHand hand) {
  26. if (hand.handType == SteamVR_Input_Sources.LeftHand && leftMyGrabPoser)
  27. return leftMyGrabPoser.transform;
  28. if (hand.handType == SteamVR_Input_Sources.RightHand && rightMyGrabPoser)
  29. return rightMyGrabPoser.transform;
  30. return null;
  31. }
  32. public SteamVR_Skeleton_Poser GetMyGrabPoser(CustomHand hand) {
  33. if (hand.handType == SteamVR_Input_Sources.LeftHand && leftMyGrabPoser)
  34. return leftMyGrabPoser;
  35. if (hand.handType == SteamVR_Input_Sources.RightHand && rightMyGrabPoser)
  36. return rightMyGrabPoser;
  37. return null;
  38. }
  39. public Transform CloseObject(Vector3 tempPoint) {
  40. Transform TempClose = null;
  41. if (grabPoints != null) {
  42. float MinDistance = float.MaxValue;
  43. for (int i = 0; i < grabPoints.Count; i++) {
  44. if (Vector3.Distance(tempPoint, grabPoints[i].transform.position) < MinDistance) {
  45. MinDistance = Vector3.Distance(tempPoint, grabPoints[i].transform.position);
  46. TempClose = grabPoints[i].transform;
  47. }
  48. }
  49. if (useSecondPose) {
  50. for (int i = 0; i < secondPoses.Count; i++) {
  51. if (Vector3.Distance(tempPoint, secondPoses[i].transform.position) < MinDistance) {
  52. MinDistance = Vector3.Distance(tempPoint, secondPoses[i].transform.position);
  53. TempClose = secondPoses[i].transform;
  54. }
  55. }
  56. }
  57. }
  58. return TempClose;
  59. }
  60. public SteamVR_Skeleton_Poser ClosePoser(Vector3 tempPoint) {
  61. SteamVR_Skeleton_Poser TempClose = null;
  62. if (grabPoints != null) {
  63. float MinDistance = float.MaxValue;
  64. for (int i = 0; i < grabPoints.Count; i++) {
  65. if (grabPoints[i] != leftMyGrabPoser && grabPoints[i] != rightMyGrabPoser) {
  66. if (Vector3.Distance(tempPoint, grabPoints[i].transform.position) < MinDistance) {
  67. MinDistance = Vector3.Distance(tempPoint, grabPoints[i].transform.position);
  68. TempClose = grabPoints[i];
  69. }
  70. }
  71. }
  72. if (useSecondPose&&ifOtherHandUseMainPoseOnThisObject()) {
  73. for (int i = 0; i < secondPoses.Count; i++) {
  74. if (secondPoses [i] != leftMyGrabPoser && secondPoses [i] != rightMyGrabPoser) {
  75. if (Vector3.Distance (tempPoint, secondPoses [i].transform.position) < MinDistance) {
  76. MinDistance = Vector3.Distance (tempPoint, secondPoses [i].transform.position);
  77. TempClose = secondPoses [i];
  78. }
  79. }
  80. }
  81. }
  82. }
  83. return TempClose;
  84. }
  85. public void SetInteractibleVariable(CustomHand hand) {
  86. if (hand.handType == SteamVR_Input_Sources.LeftHand) {
  87. if (leftHand)
  88. DettachHand(leftHand);
  89. if (!TwoHanded && rightHand)
  90. DettachHand(rightHand);
  91. leftMyGrabPoser = ClosePoser(hand.GrabPoint());
  92. if (leftMyGrabPoser) {
  93. hand.grabPoser = leftMyGrabPoser;
  94. leftHand = hand;
  95. leftHand.SkeletonUpdate();
  96. }
  97. //haptic
  98. }
  99. if (hand.handType == SteamVR_Input_Sources.RightHand) {
  100. if (rightHand)
  101. DettachHand(rightHand);
  102. if (!TwoHanded && leftHand)
  103. DettachHand(leftHand);
  104. rightMyGrabPoser = ClosePoser(hand.GrabPoint());
  105. if (rightMyGrabPoser) {
  106. hand.grabPoser = rightMyGrabPoser;
  107. rightHand = hand;
  108. rightHand.SkeletonUpdate();
  109. }
  110. //haptic
  111. }
  112. }
  113. public void SetInteractibleVariable(CustomHand hand,SteamVR_Skeleton_Poser poser){
  114. if (hand.handType == SteamVR_Input_Sources.LeftHand) {
  115. if (leftHand)
  116. DettachHand (leftHand);
  117. if (!TwoHanded && rightHand)
  118. DettachHand (rightHand);
  119. leftMyGrabPoser = poser;
  120. if (leftMyGrabPoser) {
  121. hand.grabPoser = leftMyGrabPoser;
  122. leftHand = hand;
  123. leftHand.SkeletonUpdate ();
  124. }
  125. //haptic
  126. }
  127. if (hand.handType == SteamVR_Input_Sources.RightHand) {
  128. if (rightHand)
  129. DettachHand (rightHand);
  130. if (!TwoHanded && leftHand)
  131. DettachHand (leftHand);
  132. rightMyGrabPoser = poser;
  133. if (rightMyGrabPoser) {
  134. hand.grabPoser = rightMyGrabPoser;
  135. rightHand = hand;
  136. rightHand.SkeletonUpdate ();
  137. }
  138. //haptic
  139. }
  140. }
  141. public bool ifOtherHandUseMainPoseOnThisObject(){
  142. bool tempBool=false;
  143. if (rightHand) {
  144. if (grabPoints.Contains (rightHand.grabPoser)) {
  145. tempBool = true;
  146. }
  147. }
  148. if (leftHand) {
  149. if (grabPoints.Contains (leftHand.grabPoser)) {
  150. tempBool = true;
  151. }
  152. }
  153. return tempBool;
  154. }
  155. public bool ifUseSecondPose(){
  156. bool tempBool=false;
  157. if (leftHand && rightHand) {
  158. if (secondPoses.Contains (leftHand.grabPoser)||secondPoses.Contains(rightHand.grabPoser)) {
  159. tempBool = true;
  160. }
  161. }
  162. return tempBool;
  163. }
  164. public bool CanSelected(CustomHand hand) {
  165. if (!leftHand && !rightHand)
  166. {
  167. return true;
  168. } else {
  169. if ((leftHand && leftHand == hand) || (rightHand && rightHand == hand))
  170. {
  171. return true;
  172. }
  173. else
  174. return false;
  175. }
  176. }
  177. public void DettachHand(CustomHand hand){
  178. hand.DetachHand ();
  179. if (hand.handType == SteamVR_Input_Sources.LeftHand) {
  180. leftMyGrabPoser = null;
  181. leftHand = null;
  182. }
  183. if (hand.handType == SteamVR_Input_Sources.RightHand) {
  184. rightMyGrabPoser = null;
  185. rightHand = null;
  186. }
  187. }
  188. public void DettachHands(){
  189. if (leftHand) {
  190. leftHand.DetachHand ();
  191. leftMyGrabPoser = null;
  192. leftHand = null;
  193. }
  194. if (rightHand) {
  195. rightHand.DetachHand ();
  196. rightMyGrabPoser = null;
  197. rightHand = null;
  198. }
  199. }
  200. }