|
| 1 | +using UnityEngine; |
| 2 | + |
| 3 | +namespace Zigurous.Physics |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Adds an explosion force to all rigidbodies within a set radius upon |
| 7 | + /// being triggered. |
| 8 | + /// </summary> |
| 9 | + public class Explosion : MonoBehaviour |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// A type of trigger that causes an explosion. |
| 13 | + /// </summary> |
| 14 | + public enum Trigger |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// The explosion is triggered manually. |
| 18 | + /// </summary> |
| 19 | + Manual, |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Triggers the explosion on the start of the script. |
| 23 | + /// </summary> |
| 24 | + OnStart, |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Triggers the explosion when a collision occurs on the object. |
| 28 | + /// </summary> |
| 29 | + OnCollisionEnter, |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Triggers the explosion after a collision exits on the object. |
| 33 | + /// </summary> |
| 34 | + OnCollisionExit, |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Triggers the explosion when entering a trigger collider. |
| 38 | + /// </summary> |
| 39 | + OnTriggerEnter, |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Triggers the explosion after exiting a trigger collider. |
| 43 | + /// </summary> |
| 44 | + OnTriggerExit, |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// The type of trigger that causes the explosion. |
| 49 | + /// </summary> |
| 50 | + [Tooltip("The type of trigger that causes the explosion.")] |
| 51 | + public Trigger trigger = Trigger.Manual; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// The layers that can be affected by the explosion. |
| 55 | + /// </summary> |
| 56 | + [Tooltip("The layers that can be affected by the explosion.")] |
| 57 | + public LayerMask layerMask = 1; |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// The type of force applied to affected rigidbodies. |
| 61 | + /// </summary> |
| 62 | + [Tooltip("The type of force applied to affected rigidbodies.")] |
| 63 | + public ForceMode forceMode = ForceMode.Force; |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// The radius of the sphere within which the explosion has its effect. |
| 67 | + /// </summary> |
| 68 | + [Tooltip("The radius of the sphere within which the explosion has its effect.")] |
| 69 | + public float radius; |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// The force strength of the explosion. |
| 73 | + /// </summary> |
| 74 | + [Tooltip("The force strength of the explosion.")] |
| 75 | + public float strength; |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// An adjustment to the apparent position of the explosion to make it |
| 79 | + /// seem to lift objects. |
| 80 | + /// </summary> |
| 81 | + [Tooltip("An adjustment to the apparent position of the explosion to make it seem to lift objects.")] |
| 82 | + public float upwardsModifier; |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// The amount of seconds before the explosion takes effect after being |
| 86 | + /// triggered. |
| 87 | + /// </summary> |
| 88 | + [Tooltip("The amount of seconds before the explosion takes effect after being triggered.")] |
| 89 | + public float fuseTime; |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Destroys the game object of the script after exploding. |
| 93 | + /// </summary> |
| 94 | + [Tooltip("Destroys the game object of the script after exploding.")] |
| 95 | + public bool destroyOnExplode; |
| 96 | + |
| 97 | + public virtual void Explode() |
| 98 | + { |
| 99 | + Collider[] colliders = UnityEngine.Physics.OverlapSphere(this.transform.position, this.radius, this.layerMask); |
| 100 | + |
| 101 | + for (int i = 0; i < colliders.Length; i++) |
| 102 | + { |
| 103 | + Rigidbody rigidbody = colliders[i].attachedRigidbody; |
| 104 | + |
| 105 | + if (rigidbody != null) { |
| 106 | + rigidbody.AddExplosionForce(this.strength, this.transform.position, this.radius, this.upwardsModifier); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (this.destroyOnExplode) { |
| 111 | + Destroy(this.gameObject); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void Explode(float fuseTime) |
| 116 | + { |
| 117 | + if (fuseTime > 0.0f) { |
| 118 | + Invoke(nameof(Explode), fuseTime); |
| 119 | + } else { |
| 120 | + Explode(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + protected virtual void Start() |
| 125 | + { |
| 126 | + if (this.trigger == Trigger.OnStart) { |
| 127 | + Explode(this.fuseTime); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + protected virtual void OnCollisionEnter(Collision collision) |
| 132 | + { |
| 133 | + if (this.trigger == Trigger.OnCollisionEnter) { |
| 134 | + Explode(this.fuseTime); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + protected virtual void OnCollisionExit(Collision collision) |
| 139 | + { |
| 140 | + if (this.trigger == Trigger.OnCollisionExit) { |
| 141 | + Explode(this.fuseTime); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + protected virtual void OnTriggerEnter(Collider collider) |
| 146 | + { |
| 147 | + if (this.trigger == Trigger.OnTriggerEnter) { |
| 148 | + Explode(this.fuseTime); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + protected virtual void OnTriggerExit(Collider collider) |
| 153 | + { |
| 154 | + if (this.trigger == Trigger.OnTriggerExit) { |
| 155 | + Explode(this.fuseTime); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments