CinemachineCollisionImpulseSource.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #if UNITY_2023_3_OR_NEWER
  83. vel = -mRigidBody.linearVelocity;
  84. #else
  85. vel = -mRigidBody.velocity;
  86. #endif
  87. }
  88. var rb = other != null ? other.attachedRigidbody : null;
  89. if (rb != null)
  90. {
  91. if (m_ScaleImpactWithMass)
  92. mass *= rb.mass;
  93. if (getVelocity)
  94. #if UNITY_2023_3_OR_NEWER
  95. vel += rb.linearVelocity;
  96. #else
  97. vel += rb.velocity;
  98. #endif
  99. }
  100. }
  101. return mass;
  102. }
  103. private void GenerateImpactEvent(Collider other, Vector3 vel)
  104. {
  105. // Check the filters
  106. if (!enabled)
  107. return;
  108. if (other != null)
  109. {
  110. int layer = other.gameObject.layer;
  111. if (((1 << layer) & m_LayerMask) == 0)
  112. return;
  113. if (m_IgnoreTag.Length != 0 && other.CompareTag(m_IgnoreTag))
  114. return;
  115. }
  116. // Calculate the signal direction and magnitude
  117. float mass = GetMassAndVelocity(other, ref vel);
  118. if (m_ScaleImpactWithSpeed)
  119. mass *= Mathf.Sqrt(vel.magnitude);
  120. Vector3 dir = m_DefaultVelocity;
  121. if (m_UseImpactDirection && !vel.AlmostZero())
  122. dir = -vel.normalized * dir.magnitude;
  123. // Fire it off!
  124. GenerateImpulseWithVelocity(dir * mass);
  125. }
  126. #endif
  127. #if CINEMACHINE_PHYSICS_2D
  128. private void OnCollisionEnter2D(Collision2D c)
  129. {
  130. GenerateImpactEvent2D(c.collider, c.relativeVelocity);
  131. }
  132. private void OnTriggerEnter2D(Collider2D c)
  133. {
  134. GenerateImpactEvent2D(c, Vector3.zero);
  135. }
  136. private float GetMassAndVelocity2D(Collider2D other2d, ref Vector3 vel)
  137. {
  138. bool getVelocity = vel == Vector3.zero;
  139. float mass = 1;
  140. if (m_ScaleImpactWithMass || m_ScaleImpactWithSpeed || m_UseImpactDirection)
  141. {
  142. if (mRigidBody2D != null)
  143. {
  144. if (m_ScaleImpactWithMass)
  145. mass *= mRigidBody2D.mass;
  146. if (getVelocity)
  147. vel = -mRigidBody2D.velocity;
  148. }
  149. var rb2d = other2d != null ? other2d.attachedRigidbody : null;
  150. if (rb2d != null)
  151. {
  152. if (m_ScaleImpactWithMass)
  153. mass *= rb2d.mass;
  154. if (getVelocity)
  155. {
  156. Vector3 v = rb2d.velocity;
  157. vel += v;
  158. }
  159. }
  160. }
  161. return mass;
  162. }
  163. private void GenerateImpactEvent2D(Collider2D other2d, Vector3 vel)
  164. {
  165. // Check the filters
  166. if (!enabled)
  167. return;
  168. if (other2d != null)
  169. {
  170. int layer = other2d.gameObject.layer;
  171. if (((1 << layer) & m_LayerMask) == 0)
  172. return;
  173. if (m_IgnoreTag.Length != 0 && other2d.CompareTag(m_IgnoreTag))
  174. return;
  175. }
  176. // Calculate the signal direction and magnitude
  177. float mass = GetMassAndVelocity2D(other2d, ref vel);
  178. if (m_ScaleImpactWithSpeed)
  179. mass *= Mathf.Sqrt(vel.magnitude);
  180. Vector3 dir = m_DefaultVelocity;
  181. if (m_UseImpactDirection && !vel.AlmostZero())
  182. dir = -vel.normalized * dir.magnitude;
  183. // Fire it off!
  184. GenerateImpulseWithVelocity(dir * mass);
  185. }
  186. #endif
  187. }
  188. #endif
  189. }