Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.4 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. public class RealGun : VRTK_InteractableObject
  5. {
  6. public float bulletSpeed = 200f;
  7. public float bulletLife = 5f;
  8. private GameObject bullet;
  9. private GameObject trigger;
  10. private RealGun_Slide slide;
  11. private RealGun_SafetySwitch safetySwitch;
  12. private Rigidbody slideRigidbody;
  13. private Collider slideCollider;
  14. private Rigidbody safetySwitchRigidbody;
  15. private Collider safetySwitchCollider;
  16. private VRTK_ControllerEvents controllerEvents;
  17. private float minTriggerRotation = -10f;
  18. private float maxTriggerRotation = 45f;
  19. private void ToggleCollision(Rigidbody objRB, Collider objCol, bool state)
  20. {
  21. objRB.isKinematic = state;
  22. objCol.isTrigger = state;
  23. }
  24. private void ToggleSlide(bool state)
  25. {
  26. if (!state)
  27. {
  28. slide.ForceStopInteracting();
  29. }
  30. slide.enabled = state;
  31. slide.isGrabbable = state;
  32. ToggleCollision(slideRigidbody, slideCollider, state);
  33. }
  34. private void ToggleSafetySwitch(bool state)
  35. {
  36. if (!state)
  37. {
  38. safetySwitch.ForceStopInteracting();
  39. }
  40. ToggleCollision(safetySwitchRigidbody, safetySwitchCollider, state);
  41. }
  42. public override void Grabbed(VRTK_InteractGrab currentGrabbingObject)
  43. {
  44. base.Grabbed(currentGrabbingObject);
  45. controllerEvents = currentGrabbingObject.GetComponent<VRTK_ControllerEvents>();
  46. ToggleSlide(true);
  47. ToggleSafetySwitch(true);
  48. //Limit hands grabbing when picked up
  49. if (VRTK_DeviceFinder.GetControllerHand(currentGrabbingObject.controllerEvents.gameObject) == SDK_BaseController.ControllerHand.Left)
  50. {
  51. allowedTouchControllers = AllowedController.LeftOnly;
  52. allowedUseControllers = AllowedController.LeftOnly;
  53. slide.allowedGrabControllers = AllowedController.RightOnly;
  54. safetySwitch.allowedGrabControllers = AllowedController.RightOnly;
  55. }
  56. else if (VRTK_DeviceFinder.GetControllerHand(currentGrabbingObject.controllerEvents.gameObject) == SDK_BaseController.ControllerHand.Right)
  57. {
  58. allowedTouchControllers = AllowedController.RightOnly;
  59. allowedUseControllers = AllowedController.RightOnly;
  60. slide.allowedGrabControllers = AllowedController.LeftOnly;
  61. safetySwitch.allowedGrabControllers = AllowedController.LeftOnly;
  62. }
  63. }
  64. public override void Ungrabbed(VRTK_InteractGrab previousGrabbingObject)
  65. {
  66. base.Ungrabbed(previousGrabbingObject);
  67. ToggleSlide(false);
  68. ToggleSafetySwitch(false);
  69. //Unlimit hands
  70. allowedTouchControllers = AllowedController.Both;
  71. allowedUseControllers = AllowedController.Both;
  72. slide.allowedGrabControllers = AllowedController.Both;
  73. safetySwitch.allowedGrabControllers = AllowedController.Both;
  74. controllerEvents = null;
  75. }
  76. public override void StartUsing(VRTK_InteractUse currentUsingObject)
  77. {
  78. base.StartUsing(currentUsingObject);
  79. if (safetySwitch.safetyOff)
  80. {
  81. slide.Fire();
  82. FireBullet();
  83. VRTK_ControllerHaptics.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject), 0.63f, 0.2f, 0.01f);
  84. }
  85. else
  86. {
  87. VRTK_ControllerHaptics.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject), 0.08f, 0.1f, 0.01f);
  88. }
  89. }
  90. protected override void Awake()
  91. {
  92. base.Awake();
  93. bullet = transform.Find("Bullet").gameObject;
  94. bullet.SetActive(false);
  95. trigger = transform.Find("TriggerHolder").gameObject;
  96. slide = transform.Find("Slide").GetComponent<RealGun_Slide>();
  97. slideRigidbody = slide.GetComponent<Rigidbody>();
  98. slideCollider = slide.GetComponent<Collider>();
  99. safetySwitch = transform.Find("SafetySwitch").GetComponent<RealGun_SafetySwitch>();
  100. safetySwitchRigidbody = safetySwitch.GetComponent<Rigidbody>();
  101. safetySwitchCollider = safetySwitch.GetComponent<Collider>();
  102. }
  103. protected override void Update()
  104. {
  105. base.Update();
  106. if (controllerEvents)
  107. {
  108. var pressure = (maxTriggerRotation * controllerEvents.GetTriggerAxis()) - minTriggerRotation;
  109. trigger.transform.localEulerAngles = new Vector3(0f, pressure, 0f);
  110. }
  111. else
  112. {
  113. trigger.transform.localEulerAngles = new Vector3(0f, minTriggerRotation, 0f);
  114. }
  115. }
  116. private void FireBullet()
  117. {
  118. GameObject bulletClone = Instantiate(bullet, bullet.transform.position, bullet.transform.rotation) as GameObject;
  119. bulletClone.SetActive(true);
  120. Rigidbody rb = bulletClone.GetComponent<Rigidbody>();
  121. rb.AddForce(bullet.transform.forward * bulletSpeed);
  122. Destroy(bulletClone, bulletLife);
  123. }
  124. }
  125. }