Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR;
  5.  
  6. public class Behaviour : MonoBehaviour {
  7.  
  8. public SteamVR_Input_Sources handType;
  9. public SteamVR_Action_Boolean teleportAction;
  10. public SteamVR_Behaviour_Pose controllerPose;
  11. public SteamVR_Action_Boolean grabAction;
  12. public LineRenderer RayPrefab;
  13. private LineRenderer Ray;
  14. private Vector3 hitPoint;
  15.  
  16. public Transform cameraRigTransform;
  17. public GameObject teleportReticlePrefab;
  18. private GameObject reticle;
  19. private Transform teleportReticleTransform;
  20. public Transform headTransform;
  21. public Vector3 teleportReticleOffset;
  22. public LayerMask teleportMask;
  23. private bool shouldTeleport;
  24.  
  25. private float fadeDuration = 2f;
  26.  
  27. private GameObject collidingObject;
  28. private GameObject objectInHand;
  29.  
  30. // Update is called once per frame
  31. void Update () {
  32. if (GetTeleport())
  33. {
  34. print("Teleport " + handType);
  35. if (Ray == null) {
  36. Ray = Instantiate(RayPrefab, this.transform.position, Quaternion.identity);
  37. reticle = Instantiate(teleportReticlePrefab);
  38. teleportReticleTransform = reticle.transform;
  39. }
  40.  
  41. if (Ray != null) {
  42. RaycastHit hit;
  43. Ray.transform.position = gameObject.transform.position;
  44. Ray.transform.rotation = gameObject.transform.rotation;
  45. Debug.Log("Raycast");
  46. if (Physics.Raycast(new Ray (Ray.transform.position, Ray.transform.rotation*Vector3.forward), out hit, 100)) {
  47. hitPoint = hit.point;
  48. reticle.SetActive(true);
  49. teleportReticleTransform.position = hitPoint + teleportReticleOffset;shouldTeleport = true;
  50. Debug.Log(hitPoint);
  51. }
  52. else {
  53. reticle.SetActive(false);
  54. }
  55.  
  56. }
  57. }
  58. if(GetTeleportUp()) {
  59. Destroy(Ray);
  60. Destroy(reticle);
  61. if(shouldTeleport) {
  62. Teleport();
  63. }
  64.  
  65. }
  66.  
  67. if (GetGrabDown()) {
  68. Debug.Log("grabbed");
  69. if(collidingObject) {
  70.  
  71. GrabObject();
  72. }
  73. }
  74.  
  75. if(GetGrabUp()) {
  76. Debug.Log("released");
  77. if(objectInHand) {
  78.  
  79. ReleaseObject();
  80. }
  81. }
  82.  
  83. }
  84.  
  85. private void FadeToBlack() {
  86. SteamVR_Fade.Start(Color.black, 0f);
  87. }
  88.  
  89. private void FadeFromBlack() {
  90. SteamVR_Fade.Start(Color.black, 0f);
  91. SteamVR_Fade.Start(Color.clear, fadeDuration);
  92. }
  93.  
  94.  
  95. public bool GetTeleport() {
  96. return teleportAction.GetState(handType);
  97. }
  98.  
  99. public bool GetTeleportUp() {
  100. return teleportAction.GetStateUp(handType);
  101. }
  102.  
  103. public bool GetGrabDown() {
  104. return grabAction.GetStateDown(handType);
  105. }
  106.  
  107. public bool GetGrabUp() {
  108. return grabAction.GetStateUp(handType);
  109. }
  110.  
  111. private void Teleport()
  112. {
  113. FadeToBlack();
  114. shouldTeleport = false;
  115. reticle.SetActive(false);
  116. Vector3 difference = cameraRigTransform.position - headTransform.position;
  117. difference.y = 0;
  118. cameraRigTransform.position = hitPoint + difference;
  119. Invoke("FadeFromBlack", fadeDuration);
  120.  
  121. }
  122.  
  123. private void SetCollidingObject(Collider colObj) {
  124. if (collidingObject || !colObj.GetComponent<Rigidbody>()) {
  125. return;
  126. }
  127. collidingObject = colObj.gameObject;
  128. }
  129.  
  130. public void OnTriggerEnter(Collider other) {
  131. SetCollidingObject(other);
  132. }
  133.  
  134. public void OnTriggerStay(Collider other) {
  135. SetCollidingObject(other);
  136. }
  137. public void OnTriggerExit(Collider other) {
  138. if (!collidingObject) {
  139. return;
  140. }
  141. collidingObject = null;
  142. }
  143.  
  144. private void GrabObject() {
  145. objectInHand = collidingObject;
  146. collidingObject = null;
  147. var joint = AddFixedJoint();
  148. joint.connectedBody = objectInHand.GetComponent<Rigidbody>();
  149. }
  150.  
  151. private FixedJoint AddFixedJoint() {
  152. FixedJoint fx = gameObject.AddComponent<FixedJoint>();
  153. fx.breakForce = 20000;
  154. fx.breakTorque = 20000;
  155. return fx;
  156. }
  157.  
  158. private void ReleaseObject() {
  159. if (GetComponent<FixedJoint>())
  160. {
  161. GetComponent<FixedJoint>().connectedBody = null;
  162. Destroy(GetComponent<FixedJoint>());
  163. objectInHand.GetComponent<Rigidbody>().velocity = controllerPose.GetVelocity();
  164. objectInHand.GetComponent<Rigidbody>().angularVelocity = controllerPose.GetAngularVelocity();
  165. }
  166. objectInHand = null;
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement