AttractionParticleForceField.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/Attraction Particle Force Field")]
  19. public class AttractionParticleForceField : ParticleForceField
  20. {
  21. // =================================
  22. // Nested classes and structures.
  23. // =================================
  24. // ...
  25. // =================================
  26. // Variables.
  27. // =================================
  28. // ...
  29. [Header("ForceField Controls")]
  30. [Tooltip("Tether force based on linear inverse particle distance to force field center.")]
  31. public float arrivalRadius = 1.0f;
  32. [Tooltip("Dead zone from force field center in which no additional force is applied.")]
  33. public float arrivedRadius = 0.5f;
  34. float arrivalRadiusSqr;
  35. float arrivedRadiusSqr;
  36. // =================================
  37. // Functions.
  38. // =================================
  39. // ...
  40. protected override void Awake()
  41. {
  42. base.Awake();
  43. }
  44. // ...
  45. protected override void Start()
  46. {
  47. base.Start();
  48. }
  49. // ...
  50. protected override void Update()
  51. {
  52. base.Update();
  53. }
  54. // ...
  55. protected override void LateUpdate()
  56. {
  57. float uniformTransformScale = transform.lossyScale.x;
  58. arrivalRadiusSqr = (arrivalRadius * arrivalRadius) * uniformTransformScale;
  59. arrivedRadiusSqr = (arrivedRadius * arrivedRadius) * uniformTransformScale;
  60. // ...
  61. base.LateUpdate();
  62. }
  63. // ...
  64. protected override Vector3 GetForce()
  65. {
  66. Vector3 force;
  67. if (parameters.distanceToForceFieldCenterSqr < arrivedRadiusSqr)
  68. {
  69. force.x = 0.0f;
  70. force.y = 0.0f;
  71. force.z = 0.0f;
  72. }
  73. else if (parameters.distanceToForceFieldCenterSqr < arrivalRadiusSqr)
  74. {
  75. float inverseArrivalScaleNormalized = 1.0f - (parameters.distanceToForceFieldCenterSqr / arrivalRadiusSqr);
  76. force = Vector3.Normalize(parameters.scaledDirectionToForceFieldCenter) * inverseArrivalScaleNormalized;
  77. }
  78. else
  79. {
  80. force = Vector3.Normalize(parameters.scaledDirectionToForceFieldCenter);
  81. }
  82. return force;
  83. }
  84. // ...
  85. protected override void OnDrawGizmosSelected()
  86. {
  87. if (enabled)
  88. {
  89. base.OnDrawGizmosSelected();
  90. // ...
  91. float uniformTransformScale = transform.lossyScale.x;
  92. float arrivalRadius = this.arrivalRadius * uniformTransformScale;
  93. float arrivedRadius = this.arrivedRadius * uniformTransformScale;
  94. Vector3 offsetCenter = transform.position + center;
  95. Gizmos.color = Color.yellow;
  96. Gizmos.DrawWireSphere(offsetCenter, arrivalRadius);
  97. Gizmos.color = Color.red;
  98. Gizmos.DrawWireSphere(offsetCenter, arrivedRadius);
  99. //Gizmos.color = Color.white;
  100. //Gizmos.DrawLine(currentParticleSystem.transform.position, offsetCenter);
  101. }
  102. }
  103. // =================================
  104. // End functions.
  105. // =================================
  106. }
  107. // =================================
  108. // End namespace.
  109. // =================================
  110. }
  111. }
  112. }
  113. // =================================
  114. // --END-- //
  115. // =================================