CinemachineCollisionImpulseSource.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #if !UNITY_2019_3_OR_NEWER
  2. #define CINEMACHINE_PHYSICS
  3. #define CINEMACHINE_PHYSICS_2D
  4. #endif
  5. using Cinemachine.Utility;
  6. using UnityEngine;
  7. namespace Cinemachine
  8. {
  9. #if !(CINEMACHINE_PHYSICS || CINEMACHINE_PHYSICS_2D)
  10. // Workaround for Unity scripting bug
  11. [AddComponentMenu("")] // Hide in menu
  12. public class CinemachineCollisionImpulseSource : CinemachineImpulseSource {}
  13. #else
  14. /// <summary>
  15. /// Generate an Impulse Event this object's Collider collides with something
  16. /// or its trigger zone is entered.
  17. ///
  18. /// This component should be attached to a GameObject with a Collider or a Collider2D.
  19. /// Objects colliding with this (or entering its trigger zone if it's a trigger) will be
  20. /// filtered according to the layer and tag settings defined here, and if they
  21. /// pass the filter, they will cause an impulse event to be generated.
  22. /// </summary>
  23. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  24. [SaveDuringPlay]
  25. [HelpURL(Documentation.BaseURL + "manual/CinemachineCollisionImpulseSource.html")]
  26. public class CinemachineCollisionImpulseSource : CinemachineImpulseSource
  27. {
  28. /// <summary>Only collisions with objects on these layers will generate Impulse events.</summary>
  29. [Header("Trigger Object Filter")]
  30. [Tooltip("Only collisions with objects on these layers will generate Impulse events")]
  31. public LayerMask m_LayerMask = 1;
  32. /// <summary>No Impulse evemts will be generated for collisions with objects having these tags</summary>
  33. [TagField]
  34. [Tooltip("No Impulse evemts will be generated for collisions with objects having these tags")]
  35. public string m_IgnoreTag = string.Empty;
  36. /// <summary>If checked, signal direction will be affected by the direction of impact</summary>
  37. [Header("How To Generate The Impulse")]
  38. [Tooltip("If checked, signal direction will be affected by the direction of impact")]
  39. public bool m_UseImpactDirection = false;
  40. /// <summary>If checked, signal amplitude will be multiplied by the mass of the impacting object</summary>
  41. [Tooltip("If checked, signal amplitude will be multiplied by the mass of the impacting object")]
  42. public bool m_ScaleImpactWithMass = false;
  43. /// <summary>If checked, signal amplitude will be multiplied by the speed of the impacting object</summary>
  44. [Tooltip("If checked, signal amplitude will be multiplied by the speed of the impacting object")]
  45. public bool m_ScaleImpactWithSpeed = false;
  46. #if CINEMACHINE_PHYSICS
  47. Rigidbody mRigidBody;
  48. #endif
  49. #if CINEMACHINE_PHYSICS_2D
  50. Rigidbody2D mRigidBody2D;
  51. #endif
  52. private void Start()
  53. {
  54. #if CINEMACHINE_PHYSICS
  55. mRigidBody = GetComponent<Rigidbody>();
  56. #endif
  57. #if CINEMACHINE_PHYSICS_2D
  58. mRigidBody2D = GetComponent<Rigidbody2D>();
  59. #endif
  60. }
  61. private void OnEnable() {} // For the Enabled checkbox
  62. #if CINEMACHINE_PHYSICS
  63. private void OnCollisionEnter(Collision c)
  64. {
  65. GenerateImpactEvent(c.collider, c.relativeVelocity);
  66. }
  67. private void OnTriggerEnter(Collider c)
  68. {
  69. GenerateImpactEvent(c, Vector3.zero);
  70. }
  71. private float GetMassAndVelocity(Collider other, ref Vector3 vel)
  72. {
  73. bool getVelocity = vel == Vector3.zero;
  74. float mass = 1;
  75. if (m_ScaleImpactWithMass || m_ScaleImpactWithSpeed || m_UseImpactDirection)
  76. {
  77. if (mRigidBody != null)
  78. {
  79. if (m_ScaleImpactWithMass)
  80. mass *= mRigidBody.mass;
  81. if (getVelocity)
  82. vel = -mRigidBody.velocity;
  83. }
  84. var rb = other != null ? other.attachedRigidbody : null;
  85. if (rb != null)
  86. {
  87. if (m_ScaleImpactWithMass)
  88. mass *= rb.mass;
  89. if (getVelocity)
  90. vel += rb.velocity;
  91. }
  92. }
  93. return mass;
  94. }
  95. private void GenerateImpactEvent(Collider other, Vector3 vel)
  96. {
  97. // Check the filters
  98. if (!enabled)
  99. return;
  100. if (other != null)
  101. {
  102. int layer = other.gameObject.layer;
  103. if (((1 << layer) & m_LayerMask) == 0)
  104. return;
  105. if (m_IgnoreTag.Length != 0 && other.CompareTag(m_IgnoreTag))
  106. return;
  107. }
  108. // Calculate the signal direction and magnitude
  109. float mass = GetMassAndVelocity(other, ref vel);
  110. if (m_ScaleImpactWithSpeed)
  111. mass *= vel.magnitude;
  112. Vector3 dir = Vector3.down;
  113. if (m_UseImpactDirection && !vel.AlmostZero())
  114. dir = -vel.normalized;
  115. // Fire it off!
  116. GenerateImpulse(dir * mass);
  117. }
  118. #endif
  119. #if CINEMACHINE_PHYSICS_2D
  120. private void OnCollisionEnter2D(Collision2D c)
  121. {
  122. GenerateImpactEvent2D(c.collider, c.relativeVelocity);
  123. }
  124. private void OnTriggerEnter2D(Collider2D c)
  125. {
  126. GenerateImpactEvent2D(c, Vector3.zero);
  127. }
  128. private float GetMassAndVelocity2D(Collider2D other2d, ref Vector3 vel)
  129. {
  130. bool getVelocity = vel == Vector3.zero;
  131. float mass = 1;
  132. if (m_ScaleImpactWithMass || m_ScaleImpactWithSpeed || m_UseImpactDirection)
  133. {
  134. if (mRigidBody2D != null)
  135. {
  136. if (m_ScaleImpactWithMass)
  137. mass *= mRigidBody2D.mass;
  138. if (getVelocity)
  139. vel = -mRigidBody2D.velocity;
  140. }
  141. var rb2d = other2d != null ? other2d.attachedRigidbody : null;
  142. if (rb2d != null)
  143. {
  144. if (m_ScaleImpactWithMass)
  145. mass *= rb2d.mass;
  146. if (getVelocity)
  147. {
  148. Vector3 v = rb2d.velocity;
  149. vel += v;
  150. }
  151. }
  152. }
  153. return mass;
  154. }
  155. private void GenerateImpactEvent2D(Collider2D other2d, Vector3 vel)
  156. {
  157. // Check the filters
  158. if (!enabled)
  159. return;
  160. if (other2d != null)
  161. {
  162. int layer = other2d.gameObject.layer;
  163. if (((1 << layer) & m_LayerMask) == 0)
  164. return;
  165. if (m_IgnoreTag.Length != 0 && other2d.CompareTag(m_IgnoreTag))
  166. return;
  167. }
  168. // Calculate the signal direction and magnitude
  169. float mass = GetMassAndVelocity2D(other2d, ref vel);
  170. if (m_ScaleImpactWithSpeed)
  171. mass *= vel.magnitude;
  172. Vector3 dir = Vector3.down;
  173. if (m_UseImpactDirection && !vel.AlmostZero())
  174. dir = -vel.normalized;
  175. // Fire it off!
  176. GenerateImpulse(dir * mass);
  177. }
  178. #endif
  179. }
  180. #endif
  181. }