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.

109 lines
3.3 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. public struct CollisionTrackerEventArgs
  5. {
  6. public bool isTrigger;
  7. public Collision collision;
  8. public Collider collider;
  9. }
  10. public delegate void CollisionTrackerEventHandler(object sender, CollisionTrackerEventArgs e);
  11. public class VRTK_CollisionTracker : MonoBehaviour
  12. {
  13. public event CollisionTrackerEventHandler CollisionEnter;
  14. public event CollisionTrackerEventHandler CollisionStay;
  15. public event CollisionTrackerEventHandler CollisionExit;
  16. public event CollisionTrackerEventHandler TriggerEnter;
  17. public event CollisionTrackerEventHandler TriggerStay;
  18. public event CollisionTrackerEventHandler TriggerExit;
  19. protected void OnCollisionEnterEvent(CollisionTrackerEventArgs e)
  20. {
  21. if (CollisionEnter != null)
  22. {
  23. CollisionEnter(this, e);
  24. }
  25. }
  26. protected void OnCollisionStayEvent(CollisionTrackerEventArgs e)
  27. {
  28. if (CollisionStay != null)
  29. {
  30. CollisionStay(this, e);
  31. }
  32. }
  33. protected void OnCollisionExitEvent(CollisionTrackerEventArgs e)
  34. {
  35. if (CollisionExit != null)
  36. {
  37. CollisionExit(this, e);
  38. }
  39. }
  40. protected void OnTriggerEnterEvent(CollisionTrackerEventArgs e)
  41. {
  42. if (TriggerEnter != null)
  43. {
  44. TriggerEnter(this, e);
  45. }
  46. }
  47. protected void OnTriggerStayEvent(CollisionTrackerEventArgs e)
  48. {
  49. if (TriggerStay != null)
  50. {
  51. TriggerStay(this, e);
  52. }
  53. }
  54. protected void OnTriggerExitEvent(CollisionTrackerEventArgs e)
  55. {
  56. if (TriggerExit != null)
  57. {
  58. TriggerExit(this, e);
  59. }
  60. }
  61. protected virtual void OnCollisionEnter(Collision collision)
  62. {
  63. OnCollisionEnterEvent(SetCollisionTrackerEvent(false, collision, collision.collider));
  64. }
  65. protected virtual void OnCollisionStay(Collision collision)
  66. {
  67. OnCollisionStayEvent(SetCollisionTrackerEvent(false, collision, collision.collider));
  68. }
  69. protected virtual void OnCollisionExit(Collision collision)
  70. {
  71. OnCollisionExitEvent(SetCollisionTrackerEvent(false, collision, collision.collider));
  72. }
  73. protected virtual void OnTriggerEnter(Collider collider)
  74. {
  75. OnTriggerEnterEvent(SetCollisionTrackerEvent(true, null, collider));
  76. }
  77. protected virtual void OnTriggerStay(Collider collider)
  78. {
  79. OnTriggerStayEvent(SetCollisionTrackerEvent(true, null, collider));
  80. }
  81. protected virtual void OnTriggerExit(Collider collider)
  82. {
  83. OnTriggerExitEvent(SetCollisionTrackerEvent(true, null, collider));
  84. }
  85. protected virtual CollisionTrackerEventArgs SetCollisionTrackerEvent(bool isTrigger, Collision givenCollision, Collider givenCollider)
  86. {
  87. CollisionTrackerEventArgs e;
  88. e.isTrigger = isTrigger;
  89. e.collision = givenCollision;
  90. e.collider = givenCollider;
  91. return e;
  92. }
  93. }
  94. }