← Pixel Dreamer Studio

An expanding shockwave that hits each enemy once

Each frame, hit the targets between last frame's radius and this frame's, and remember who was hit.

Keep last frame's radius. A target was reached this frame if its distance is more than the old radius and no more than the new one, and a list of who was already hit stops a second hit. Checking whether a target is inside the ring's band depends on how far the ring moves in one frame. In our Godot 4.7 test a ring growing 12 m/s with a 0.5 m band hit 14 of 60 targets twice at 30 fps and every target 2 to 6 times at 60 and 144 fps. At 25 m/s and 30 fps it moved 0.83 m a frame and jumped past 25 of the 60, even with a hit-once list. The script hit all 60 exactly once at every frame rate and speed.

The code

Save it as shockwave_hits.gd. Make one when the shockwave goes off, var wave = Shockwave.new(global_position, 12.0, 10.0) after const Shockwave := preload("res://shockwave_hits.gd"), then in _physics_process() call wave.step(delta, enemies) and damage what it returns, until wave.finished(). Distance is measured on the ground, so a target above the ring is still reached.

extends RefCounted
## An expanding shockwave that hits each target once, on the frame the ring reaches it,
## however far the ring moves in one frame.

var center := Vector3.ZERO
var speed := 12.0  # meters per second
var max_radius := 10.0
var radius := 0.0
var _hit := {}


func _init(at: Vector3, meters_per_second: float = 12.0, reach: float = 10.0) -> void:
	center = at
	speed = meters_per_second
	max_radius = reach


## Call every physics frame with the targets (Node3D). Returns the ones the ring reached this frame.
func step(delta: float, targets: Array) -> Array:
	var before := radius
	radius = minf(radius + speed * delta, max_radius)
	var reached := []
	for t in targets:
		if _hit.has(t):
			continue
		var d := Vector2(t.global_position.x - center.x, t.global_position.z - center.z).length()  # distance on the ground
		if d <= radius and (d > before or before == 0.0):  # the ring passed it this frame
			_hit[t] = true
			reached.append(t)
	return reached


func finished() -> bool:
	return radius >= max_radius

What we measured

60 targets from 0.15 m to 9.9 m from the center, a ring growing to 10 m, at 30, 60 and 144 fps with a fixed delta. The band checks hit a target while it was within 0.25 m of the ring. Two runs gave the same numbers.

Why it happens

A band check asks where the ring is now, and the ring only exists at the frames you see. When it moves less than the band's width in one frame, a target sits inside the band for several frames in a row and is hit each time. When it moves more than the band's width, the band can land on one side of a target and then the other, and the target is never inside it. Checking the range the ring covered since the last frame catches every target exactly once, whatever the frame rate.

What to change

The ring you see: Arcane VFX Vol 1 has Mana_Shockwave in four versions, plain, Crimson, Gold and Small, as one-shots you start with play(), and PDVFX.set_scale() to resize them. Match the script's speed and reach to the size you pick, then start both on the same frame.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Arcane VFX Vol 1 has Mana_Shockwave in plain, Crimson, Gold and Small versions to play() where the script deals the damage.

More how-tos for Arcane VFX Vol 1: Glowing particles in Godot 4 · A spell that keeps flying straight after it leaves the hand

More how-tos: Hazards, traps and attacks