Damage that falls off with the distance from the blast to the target's centre treats a big enemy as if it were a point in its middle, so a boss whose side is inside the blast can get nothing. In our Godot 4.7 test a 5 m blast found a 2.5 m radius boss, a tank and a tower with its sphere query, but their centres were 6.50, 6.58 and 6.28 m away, so damage from the centre distance was 0 for all three. Measuring to the nearest point of each collision shape, which matched the physics engine to the millimetre, gave them 20.0, 4.3 and 19.7 damage out of 100.
The code
Save it as blast_falloff.gd and load it with const Blast := preload("res://blast_falloff.gd"). For each body the blast found, call Blast.damage(body, blast_position, 5.0, 100.0) instead of using the distance to body.global_position. It reads the body's CollisionShape3D children and supports sphere, box, capsule and cylinder shapes that are not scaled; other shapes count from their centre. Keep finding the bodies with a sphere query and a wall check, as in explosion damage through walls.
extends RefCounted
## Explosion damage that falls off with the distance from the blast to the
## nearest point of each target's collision shapes, not to the target's centre,
## so a big enemy with its side inside the blast still gets hurt.
## const Blast := preload("res://blast_falloff.gd")
## var dmg := Blast.damage(body, blast_position, 5.0, 100.0)
## Works with Sphere, Box, Capsule and Cylinder shapes that are not scaled.
## Other shapes count from their centre.
static func damage(body: Node3D, blast: Vector3, radius: float, full: float) -> float:
var d := surface_distance(body, blast)
if d >= radius:
return 0.0
return full * (1.0 - d / radius)
static func surface_distance(body: Node3D, point: Vector3) -> float:
var best := INF
for child in body.get_children():
var cs := child as CollisionShape3D
if cs == null or cs.disabled or cs.shape == null:
continue
var p := cs.global_transform.affine_inverse() * point # the blast in the shape's own space
var s = cs.shape
var d := p.length()
if s is SphereShape3D:
d = p.length() - s.radius
elif s is BoxShape3D:
var q: Vector3 = p.abs() - s.size * 0.5
d = Vector3(maxf(q.x, 0.0), maxf(q.y, 0.0), maxf(q.z, 0.0)).length() + minf(maxf(q.x, maxf(q.y, q.z)), 0.0)
elif s is CapsuleShape3D:
var half: float = maxf(s.height * 0.5 - s.radius, 0.0)
d = Vector3(p.x, p.y - clampf(p.y, -half, half), p.z).length() - s.radius
elif s is CylinderShape3D:
var side: float = Vector2(p.x, p.z).length() - s.radius
var top: float = absf(p.y) - s.height * 0.5
d = Vector2(maxf(side, 0.0), maxf(top, 0.0)).length() + minf(maxf(side, top), 0.0)
best = minf(best, maxf(d, 0.0))
return best
What we measured
A blast at the origin with a 5 m radius and 100 damage falling off in a straight line to 0 at the edge, and five StaticBody3D targets. For each one we also asked the physics engine for the smallest sphere around the blast that touches it, by halving the radius 30 times. Two runs gave the same numbers.
- A soldier capsule (radius 0.4 m, height 1.8 m) 3.13 m from the blast: 37.4 damage from the centre, 47.5 from its nearest point, 2.627 m away.
- The same soldier 4.88 m away: 2.3 from the centre, 11.7 from its nearest point, 4.417 m away.
- A boss sphere of radius 2.5 m, centre 6.50 m away: 0 from the centre, 20.0 from its nearest point, 4.000 m away.
- A tank box of 3 by 2 by 6 m turned 30°, centre 6.58 m away: 0 from the centre, 4.3 from its nearest point, 4.784 m away.
- A tower cylinder of radius 1.5 m and height 6 m, centre 6.28 m away: 0 from the centre, 19.7 from its nearest point, 4.015 m away.
- The 5 m sphere query hit all five, and the script's nearest points were the same as the physics engine's for all five, to three decimals.
Why it happens
A sphere query finds every body whose collision shape overlaps the blast, but global_position is only the body's origin. For a soldier the difference is the width of a capsule; for a boss, a vehicle or a building it can be several metres, enough to put the centre outside the blast while the side is well inside. The script moves the blast into each shape's own space and works out the distance to its surface, the same way for a turned box as for a straight one. For a body made of several shapes, the script uses the closest one.
What to change
- Walls that should block the blast: a sphere query and a ray that ignores the target.
- A shockwave that spreads out over time and should hit each enemy once: a ring that hits once at any frame rate.
- The blast itself as one burst of particles: explosion particles in one burst.
What the blasts look like: Modern Battlefield VFX for Godot 4 has mortar, landmine and fuel depot blasts to play where explode() deals the damage.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Modern Battlefield VFX for Godot 4 has mortar, landmine and fuel depot blasts to play where explode() deals the damage.
More how-tos for Modern Battlefield VFX for Godot 4: Explosion effect in one burst · 3D debris particles not rotating · 3D camera shake that lands back on center
More how-tos: Hazards, traps and attacks
- Turn a 2D character left without the flicker
- A line or rectangle attack that hits what the player sees
- A scare that plays once, and only for the player
- An attack that hits even when a frame is slow
- Growing one cloud or aura zone grows all of them
- A parry window that is the same length on every screen
- A pair of portals that sends you through once
- An expanding shockwave that hits each enemy once
- Explosion damage that walls block, on the frame it goes off
- A fire or poison zone that keeps hurting while you stand in it
- A spike trap that hits whoever is already standing on it
- Who is inside an AoE attack circle
- Block only attacks from the front
- A sword hitbox that hits each enemy once per swing
- Slows and roots that overlap
- Blink forward without going into or through walls
- Knockback that the walk code does not cancel
- Random spots spread evenly inside a circle
- Keep a pressure plate down while anything is still on it
- Refill the guard meter only after the hits stop
- Heal from the damage that actually landed
- Let the shield take what it can and pass the rest of the hit to health
- A saw blade that slides along its path at a steady speed
- A spinning laser that hits everything it sweeps over