# 6. Explosions and Area Damage Part of [[Weapons, Hitscan and Projectiles]]. Previous: [[5. Projectiles and Ballistics]]. Next: [[7. WeaponUser Input ADS Switching Tests and Pitfalls]]. ## 6.1 What a good explosion needs 1. **Falloff.** Full damage inside an inner radius, then a ramp down to an edge value at the outer radius, then nothing. A sharp edge reads better than a long tail: players learn "outside the smoke ring is safe". 2. **One hit per target.** A character has several hurtboxes; an overlap finds all of them. Damage the *owner* once. 3. **Cover works.** If a wall is between the blast and you, you take nothing. Without this, players die through walls and blame the game. 4. **Knockback with lift.** Push away from the centre with some upward bias, so bodies pop up instead of sliding along the floor. ## 6.2 Blast falloff `Blast.Multiplier` is in `Ballistics.cs` (part 5), so it's tested with the rest of the maths: 1 inside `innerRadius`, a linear ramp from 1 to `edgeMultiplier` out to `radius`, 0 beyond. ## 6.3 ExplosionDamage.cs ```csharp using System.Collections.Generic; using UnityEngine; /// <summary> /// Area damage: OverlapSphere for hurtboxes, distance falloff, and a line-of-sight check so walls /// block blasts. One attack id per detonation, and one hit per IDamageable even if it has several hurtboxes. /// </summary> public sealed class ExplosionDamage : MonoBehaviour { [SerializeField] int damage = 80; [SerializeField] float innerRadius = 1.5f; [SerializeField] float radius = 5f; [SerializeField, Range(0f, 1f)] float edgeMultiplier = 0.2f; [SerializeField] LayerMask hurtboxMask = ~0; [SerializeField] LayerMask blockingMask = 1; // Default: level geometry [SerializeField] float upwardKnock = 0.4f; readonly Collider[] _overlaps = new Collider[64]; readonly HashSet<IDamageable> _done = new HashSet<IDamageable>(); public event System.Action<Vector3, float> Detonated; // position, radius (VFX, camera shake) public int Detonate(Vector3 center, AttackDefinition attack, TeamId team, GameObject source) { var id = AttackIds.Next(); _done.Clear(); var count = Physics.OverlapSphereNonAlloc(center, radius, _overlaps, hurtboxMask, QueryTriggerInteraction.Collide); var hits = 0; for (var i = 0; i < count; i++) { var col = _overlaps[i]; if (!col.TryGetComponent<Hurtbox>(out var hb) || hb.Damageable == null || !_done.Add(hb.Damageable)) continue; var target = col.bounds.center; var toTarget = target - center; var dist = toTarget.magnitude; // Walls block the blast: if level geometry is between the centre and the hurtbox, skip it. if (dist > 0.01f && Physics.Raycast(center, toTarget / dist, dist, blockingMask, QueryTriggerInteraction.Ignore)) continue; var amount = Mathf.RoundToInt(damage * Blast.Multiplier(dist, innerRadius, radius, edgeMultiplier)); if (amount <= 0) continue; var dir = (dist > 0.01f ? toTarget / dist : Vector3.up) + Vector3.up * upwardKnock; if (HitResolver.TryDamage(col, col.ClosestPoint(center), -toTarget.normalized, dir, amount, attack, id, team, source)) hits++; } Detonated?.Invoke(center, radius); return hits; } void OnDrawGizmosSelected() { Gizmos.color = new Color(1f, 0.5f, 0f, 0.5f); Gizmos.DrawWireSphere(transform.position, innerRadius); Gizmos.DrawWireSphere(transform.position, radius); } } ``` - `OverlapSphereNonAlloc` with a fixed 64-entry buffer: no garbage per blast. If a blast can touch more than 64 hurtboxes, make the buffer bigger. Colliders past the buffer size are silently dropped. - The `HashSet<IDamageable>` makes the "one hit per owner" rule explicit. `HitResolver` would also block the second hurtbox through `TryRegisterHit` (same attack id), but the set saves the line-of-sight raycast. - The line-of-sight ray goes to the hurtbox's **bounds centre** against `blockingMask` only (level geometry), ignoring triggers. A target half behind cover whose centre is hidden takes nothing. For fairer partial cover, test 2–3 points (head, centre, feet) and scale damage by how many are visible. - The rocket sits 5 cm off the surface (`hit.point + hit.normal * 0.05f` in `Projectile`), so the line-of-sight rays don't start inside the wall that was hit. **Self damage.** `HitResolver` skips the shooter's *team*, so with the defaults the player's own rocket doesn't hurt them. For rocket-jump games or self-damage as a counter (part 1's design sheet), give the player's projectiles `TeamId.Neutral`, or add a `selfDamageMultiplier` and compare `source` with the victim. ## 6.4 Feedback hooks `Detonated(position, radius)` is for everything that isn't damage: - a VFX Graph or particle burst at the position; - camera shake scaled by distance to the camera (Cinemachine Impulse is a good fit, see [[Cinemachine 3]]); - a scorch decal: raycast down from the centre and call the decal spawner's `Spawn` with the `"scorch"` kind, as in [[4. Runtime Bullet Holes and Scorch Marks]]. ## Exercises 1. Add the three-point partial-cover test from 6.3 and compare damage behind a waist-high wall. 2. Hook `Detonated` to a scorch decal and a camera shake.