← Pixel Dreamer Studio

Explosion damage that walls block, on the frame it goes off

Find who is in range with a sphere query, cast one ray to each target that leaves the target out, and scale the damage by distance.

Ask the physics space with intersect_shape() instead of spawning an Area3D, then cast one ray from the blast to each target with the target in the ray's exclude list: anything the ray still hits is a wall. In our Godot 4.7 test a 6 m blast that used distance alone hurt all 6 enemies in range, including 2 behind walls. A ray that only checked is_empty() hurt none of them, because the first thing it hit was the enemy's own capsule. A new Area3D found nobody until its second physics frame. The script hit the 4 in the open on the same frame, from 61.1 damage at 2.3 m down to 14.3 at 5.1 m, and left the 2 behind walls at full health.

The code

Save it as blast_damage.gd and call it where the blast lands: const Blast := preload("res://blast_damage.gd"), then Blast.explode(get_world_3d(), global_position, 6.0, 100.0). Targets need a take_damage(amount) method, and the call returns a Dictionary of who was hit and for how much. Put walls and floors on collision layer 1 and enemies on another layer, so wall_mask = 1 only sees walls.

extends RefCounted
## Explosion damage on the frame it goes off: a sphere query finds who is in range,
## one ray per target checks for walls, and damage falls off with distance.
## Keep enemies off wall_mask (for example walls on layer 1, enemies on layer 2) so they do not shield each other.


static func explode(world: World3D, center: Vector3, radius: float, max_damage: float, wall_mask: int = 1, target_mask: int = 0xFFFFFFFF) -> Dictionary:
	var space := world.direct_space_state
	var sphere := SphereShape3D.new()
	sphere.radius = radius
	var q := PhysicsShapeQueryParameters3D.new()
	q.shape = sphere
	q.transform = Transform3D(Basis(), center)
	q.collision_mask = target_mask
	var hits := {}
	for r in space.intersect_shape(q, 256):  # the default is 32 results, and floors and walls count
		var body = r.collider
		if body in hits or not body.has_method("take_damage"):
			continue
		var point: Vector3 = body.global_position + Vector3.UP * 0.5  # aim at the chest, not the feet
		var d := center.distance_to(point)
		if d >= radius or not _clear(space, center, point, body, wall_mask):
			continue
		hits[body] = max_damage * (1.0 - d / radius)
	for body in hits:
		body.take_damage(hits[body])
	return hits


static func _clear(space: PhysicsDirectSpaceState3D, from: Vector3, to: Vector3, body: CollisionObject3D, mask: int) -> bool:
	var ray := PhysicsRayQueryParameters3D.create(from, to, mask, [body.get_rid()])
	return space.intersect_ray(ray).is_empty()  # the target is left out, so only walls block

What we measured

A blast 0.3 m above the floor with a 6 m radius and 100 damage, and 7 enemies: capsules 2 m tall on collision layer 2, with the floor and two walls on layer 1. Distances are to each enemy's chest, 0.5 m above its center. Two runs gave the same numbers.

Why it happens

An Area3D's list of overlapping bodies is filled in by the physics step, so an explosion that reads get_overlapping_bodies() right after it is added gets an empty list, and in our test it stayed empty until the second physics frame. A shape query asks the physics space directly and answers on the spot. A ray aimed at an enemy has to cross the enemy's own collision shape to reach that point, so unless the ray is told to skip the enemy, it always hits something.

What to change

The blast you see: Modern Battlefield VFX for Godot 4 has 29 presets, among them Mortar_Impact, Landmine_Blast and Fuel_Depot_Explosion, in Stylized and PSX looks. The one-shots play with play() and flash a real OmniLight3D. A mortar drops its shell first and hits after a short built-in delay, so call explode() when it hits, not when you call play().

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