CinemachineImpulseSource.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. [CinemachineImpulseDefinitionProperty]
  23. public CinemachineImpulseDefinition m_ImpulseDefinition = new CinemachineImpulseDefinition();
  24. private void OnValidate()
  25. {
  26. m_ImpulseDefinition.OnValidate();
  27. }
  28. /// <summary>Broadcast the Impulse Signal onto the appropriate channels,
  29. /// using a custom position and impact velocity</summary>
  30. /// <param name="position">The world-space position from which the impulse will emanate</param>
  31. /// <param name="velocity">The impact magnitude and direction</param>
  32. public void GenerateImpulseAt(Vector3 position, Vector3 velocity)
  33. {
  34. if (m_ImpulseDefinition != null)
  35. m_ImpulseDefinition.CreateEvent(position, velocity);
  36. }
  37. /// <summary>Broadcast the Impulse Signal onto the appropriate channels, using
  38. /// a custom impact velocity, and this transfom's position.</summary>
  39. /// <param name="velocity">The impact magnitude and direction</param>
  40. public void GenerateImpulse(Vector3 velocity)
  41. {
  42. GenerateImpulseAt(transform.position, velocity);
  43. }
  44. /// <summary>Broadcast the Impulse Signal onto the appropriate channels, using
  45. /// a custom impact force, with the standard direction, and this transfom's position.</summary>
  46. /// <param name="force">The impact magnitude. 1 is normal</param>
  47. public void GenerateImpulse(float force)
  48. {
  49. GenerateImpulseAt(transform.position, new Vector3(0, -force, 0));
  50. }
  51. /// <summary>Broadcast the Impulse Signal onto the appropriate channels,
  52. /// with default velocity = (0, -1, 0), and a default position which is
  53. /// this transform's location.</summary>
  54. public void GenerateImpulse()
  55. {
  56. GenerateImpulse(Vector3.down);
  57. }
  58. }
  59. }