CinemachineImpulseSource.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. namespace Cinemachine
  3. {
  4. /// <summary>
  5. /// An event-driven class that broadcasts an impulse signal to listeners.
  6. ///
  7. /// This is the base class for custom impulse sources. It contains an impulse
  8. /// definition, where the characteristics of the impulse signal are defined.
  9. ///
  10. /// API methods are provided for actually broadcasting the impulse. Call these
  11. /// methods from your custom code, or hook them up to game events in the Editor.
  12. ///
  13. /// </summary>
  14. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  15. [SaveDuringPlay]
  16. [HelpURL(Documentation.BaseURL + "manual/CinemachineImpulseSourceOverview.html")]
  17. public class CinemachineImpulseSource : MonoBehaviour
  18. {
  19. /// <summary>
  20. /// This defines the complete impulse signal that will be broadcast.
  21. /// </summary>
  22. public CinemachineImpulseDefinition m_ImpulseDefinition = new CinemachineImpulseDefinition();
  23. /// <summary>
  24. /// The default direction and force of the Impulse Signal in the absense of any
  25. /// specified overrides. Overrides can be specified by calling the appropriate
  26. /// GenerateImpulse method in the API.
  27. /// </summary>
  28. [Header("Default Invocation")]
  29. [Tooltip("The default direction and force of the Impulse Signal in the absense "
  30. + "of any specified overrides. Overrides can be specified by calling the appropriate "
  31. + "GenerateImpulse method in the API.")]
  32. public Vector3 m_DefaultVelocity = Vector3.down;
  33. void OnValidate()
  34. {
  35. m_ImpulseDefinition.OnValidate();
  36. }
  37. void Reset()
  38. {
  39. m_ImpulseDefinition = new CinemachineImpulseDefinition
  40. {
  41. m_ImpulseChannel = 1,
  42. m_ImpulseShape = CinemachineImpulseDefinition.ImpulseShapes.Bump,
  43. m_CustomImpulseShape = new AnimationCurve(),
  44. m_ImpulseDuration = 0.2f,
  45. m_ImpulseType = CinemachineImpulseDefinition.ImpulseTypes.Uniform,
  46. m_DissipationDistance = 100,
  47. m_DissipationRate = 0.25f,
  48. m_PropagationSpeed = 343
  49. };
  50. m_DefaultVelocity = Vector3.down;
  51. }
  52. /// <summary>Broadcast the Impulse Signal onto the appropriate channels,
  53. /// using a custom position and impact velocity</summary>
  54. /// <param name="position">The world-space position from which the impulse will emanate</param>
  55. /// <param name="velocity">The impact magnitude and direction</param>
  56. public void GenerateImpulseAtPositionWithVelocity(Vector3 position, Vector3 velocity)
  57. {
  58. if (m_ImpulseDefinition != null)
  59. m_ImpulseDefinition.CreateEvent(position, velocity);
  60. }
  61. /// <summary>Broadcast the Impulse Signal onto the appropriate channels, using
  62. /// a custom impact velocity, and this transfom's position.</summary>
  63. /// <param name="velocity">The impact magnitude and direction</param>
  64. public void GenerateImpulseWithVelocity(Vector3 velocity)
  65. {
  66. GenerateImpulseAtPositionWithVelocity(transform.position, velocity);
  67. }
  68. /// <summary>Broadcast the Impulse Signal onto the appropriate channels, using
  69. /// a custom impact force, with the standard direction, and this transfom's position.</summary>
  70. /// <param name="force">The impact magnitude. 1 is normal</param>
  71. public void GenerateImpulseWithForce(float force)
  72. {
  73. GenerateImpulseAtPositionWithVelocity(
  74. transform.position, m_DefaultVelocity * force);
  75. }
  76. /// <summary>Broadcast the Impulse Signal onto the appropriate channels,
  77. /// with default velocity = (0, -1, 0), and a default position which is
  78. /// this transform's location.</summary>
  79. public void GenerateImpulse()
  80. {
  81. GenerateImpulseWithVelocity(m_DefaultVelocity);
  82. }
  83. /// <summary>Legacy API: Please use GenerateImpulseAtPositionWithVelocity() instead.
  84. /// Broadcast the Impulse Signal onto the appropriate channels,
  85. /// using a custom position and impact velocity</summary>
  86. /// <param name="position">The world-space position from which the impulse will emanate</param>
  87. /// <param name="velocity">The impact magnitude and direction</param>
  88. public void GenerateImpulseAt(Vector3 position, Vector3 velocity)
  89. => GenerateImpulseAtPositionWithVelocity(position, velocity);
  90. /// <summary>Legacy API: Please use GenerateImpulseWithVelocity() instead.
  91. /// Broadcast the Impulse Signal onto the appropriate channels, using
  92. /// a custom impact velocity, and this transfom's position.</summary>
  93. /// <param name="velocity">The impact magnitude and direction</param>
  94. public void GenerateImpulse(Vector3 velocity) => GenerateImpulseWithVelocity(velocity);
  95. /// <summary>Legacy API: Please use GenerateImpulseWithForce() instead.
  96. /// Broadcast the Impulse Signal onto the appropriate channels, using
  97. /// a custom impact force, with the standard direction, and this transfom's position.</summary>
  98. /// <param name="force">The impact magnitude. 1 is normal</param>
  99. public void GenerateImpulse(float force) => GenerateImpulseWithForce(force);
  100. }
  101. }