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.

115 lines
4.5 KiB

  1. // Controller Rigidbody Activator|Prefabs|0050
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// Event Payload
  7. /// </summary>
  8. /// <param name="interactingObject">The object that touching the activator.</param>
  9. public struct ControllerRigidbodyActivatorEventArgs
  10. {
  11. public VRTK_InteractTouch touchingObject;
  12. }
  13. /// <summary>
  14. /// Event Payload
  15. /// </summary>
  16. /// <param name="sender">this object</param>
  17. /// <param name="e"><see cref="ControllerRigidbodyActivatorEventArgs"/></param>
  18. public delegate void ControllerRigidbodyActivatorEventHandler(object sender, ControllerRigidbodyActivatorEventArgs e);
  19. /// <summary>
  20. /// Provides a simple trigger collider volume that when a controller enters will enable the rigidbody on the controller.
  21. /// </summary>
  22. /// <remarks>
  23. /// **Prefab Usage:**
  24. /// * Place the `VRTK/Prefabs/ControllerRigidbodyActivator/ControllerRigidbodyActivator` prefab in the scene at the location where the controller rigidbody should be automatically activated.
  25. /// * The prefab contains a default sphere collider to determine ths collision, this collider component can be customised in the inspector or can be replaced with another collider component (set to `Is Trigger`).
  26. ///
  27. /// > If the prefab is placed as a child of the target Interactable Object then the collider volume on the prefab will trigger collisions on the Interactable Object.
  28. /// </remarks>
  29. public class VRTK_ControllerRigidbodyActivator : MonoBehaviour
  30. {
  31. [Tooltip("If this is checked then the Collider will have it's Rigidbody toggled on and off during a collision.")]
  32. public bool isEnabled = true;
  33. [Tooltip("If this is checked then the Rigidbody Activator will activate the rigidbody and colliders on the Interact Touch script.")]
  34. public bool activateInteractTouch = true;
  35. [Tooltip("If this is checked then the Rigidbody Activator will activate the rigidbody and colliders on the Controller Tracked Collider script.")]
  36. public bool activateTrackedCollider = false;
  37. /// <summary>
  38. /// Emitted when the controller rigidbody is turned on.
  39. /// </summary>
  40. public event ControllerRigidbodyActivatorEventHandler ControllerRigidbodyOn;
  41. /// <summary>
  42. /// Emitted when the controller rigidbody is turned off.
  43. /// </summary>
  44. public event ControllerRigidbodyActivatorEventHandler ControllerRigidbodyOff;
  45. public virtual void OnControllerRigidbodyOn(ControllerRigidbodyActivatorEventArgs e)
  46. {
  47. if (ControllerRigidbodyOn != null)
  48. {
  49. ControllerRigidbodyOn(this, e);
  50. }
  51. }
  52. public virtual void OnControllerRigidbodyOff(ControllerRigidbodyActivatorEventArgs e)
  53. {
  54. if (ControllerRigidbodyOff != null)
  55. {
  56. ControllerRigidbodyOff(this, e);
  57. }
  58. }
  59. protected virtual void OnTriggerEnter(Collider collider)
  60. {
  61. ToggleRigidbody(collider, true);
  62. }
  63. protected virtual void OnTriggerExit(Collider collider)
  64. {
  65. ToggleRigidbody(collider, false);
  66. }
  67. protected virtual void ToggleRigidbody(Collider collider, bool state)
  68. {
  69. if (isEnabled || !state)
  70. {
  71. if (activateTrackedCollider)
  72. {
  73. VRTK_ControllerTrackedCollider trackedCollider = collider.GetComponentInParent<VRTK_ControllerTrackedCollider>();
  74. if (trackedCollider != null)
  75. {
  76. trackedCollider.ToggleColliders(state);
  77. EmitEvent(state, trackedCollider.interactTouch);
  78. }
  79. }
  80. if (activateInteractTouch)
  81. {
  82. VRTK_InteractTouch touch = collider.GetComponentInParent<VRTK_InteractTouch>();
  83. if (touch != null)
  84. {
  85. touch.ToggleControllerRigidBody(state, state);
  86. EmitEvent(state, touch);
  87. }
  88. }
  89. }
  90. }
  91. protected virtual void EmitEvent(bool state, VRTK_InteractTouch touch)
  92. {
  93. ControllerRigidbodyActivatorEventArgs e;
  94. e.touchingObject = touch;
  95. if (state)
  96. {
  97. OnControllerRigidbodyOn(e);
  98. }
  99. else
  100. {
  101. OnControllerRigidbodyOff(e);
  102. }
  103. }
  104. }
  105. }