VortexParticleForceField.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. // =================================
  7. // Define namespace.
  8. // =================================
  9. namespace MirzaBeig
  10. {
  11. namespace Scripting
  12. {
  13. namespace Effects
  14. {
  15. // =================================
  16. // Classes.
  17. // =================================
  18. [AddComponentMenu("Effects/Particle Force Fields/Vortex Particle Force Field")]
  19. public class VortexParticleForceField : ParticleForceField
  20. {
  21. // =================================
  22. // Nested classes and structures.
  23. // =================================
  24. // ...
  25. // =================================
  26. // Variables.
  27. // =================================
  28. // ...
  29. Vector3 axisOfRotation;
  30. [Header("ForceField Controls")]
  31. [Tooltip(
  32. "Internal offset for the axis of rotation.\n\n" +
  33. "Useful if the force field and particle system are on the same game object, and you need a seperate rotation for the system, and the affector, but don't want to make the two different game objects.")]
  34. public Vector3 axisOfRotationOffset = Vector3.zero;
  35. // =================================
  36. // Functions.
  37. // =================================
  38. // ...
  39. protected override void Awake()
  40. {
  41. base.Awake();
  42. }
  43. // ...
  44. protected override void Start()
  45. {
  46. base.Start();
  47. }
  48. // ...
  49. protected override void Update()
  50. {
  51. base.Update();
  52. }
  53. // ...
  54. protected override void LateUpdate()
  55. {
  56. base.LateUpdate();
  57. }
  58. // ...
  59. void UpdateAxisOfRotation()
  60. {
  61. axisOfRotation = Quaternion.Euler(axisOfRotationOffset) * transform.up;
  62. }
  63. // ...
  64. protected override void PerParticleSystemSetup()
  65. {
  66. UpdateAxisOfRotation();
  67. }
  68. // ...
  69. protected override Vector3 GetForce()
  70. {
  71. // With no rotation, looking at the PS with the vortex down the Z axis, you may
  72. // think it's spinning the wrong way because it's counter-clockwise. But it's actually correct...
  73. // Because if you were to look up aligned with the up-axis of the vortex, you'd see it spinning
  74. // clockwise. And if that up was inverted (you looked down at the vortex from above), then it would
  75. // be spinning counter-clockwise since now the vector of rotation is point at you, not away from you.
  76. // I can't believe I almost mixed that up by adding a negative (-) in front of the return.
  77. return Vector3.Normalize(Vector3.Cross(axisOfRotation, parameters.scaledDirectionToForceFieldCenter));
  78. }
  79. // ...
  80. protected override void OnDrawGizmosSelected()
  81. {
  82. if (enabled)
  83. {
  84. base.OnDrawGizmosSelected();
  85. // ...
  86. Gizmos.color = Color.red;
  87. // When not playing, I don't have a reference to the specific particle system,
  88. // so just use the default method of showing the axis of rotation (which may be wrong).
  89. // There's no easy way around this since I may have several particle systems being updated
  90. // with a single vortex. It's just a visual guide anyways, so no big deal, I suppose.
  91. Vector3 axisOfRotation;
  92. if (Application.isPlaying && enabled)
  93. {
  94. UpdateAxisOfRotation();
  95. axisOfRotation = this.axisOfRotation;
  96. }
  97. else
  98. {
  99. axisOfRotation = Quaternion.Euler(axisOfRotationOffset) * transform.up;
  100. }
  101. Vector3 offsetCenter = transform.position + center;
  102. Gizmos.DrawLine(offsetCenter, offsetCenter + (axisOfRotation * scaledRadius));
  103. }
  104. }
  105. // =================================
  106. // End functions.
  107. // =================================
  108. }
  109. // =================================
  110. // End namespace.
  111. // =================================
  112. }
  113. }
  114. }
  115. // =================================
  116. // --END-- //
  117. // =================================