← Pixel Dreamer Studio

A spinning laser that hits everything it sweeps over

Each frame, check the whole slice of circle the beam turned through since the last frame, widened by the target's size.

A raycast only checks the line the beam points along on this frame. When the beam turns further in one frame than a target is wide, the target sits in the gap between two frames and is never hit. In our Godot 4.7 test, a RayCast3D laser spinning 720 degrees a second at the default 60 physics ticks turned 12 degrees a tick and hit 6 of 24 bodies in a full turn. The script below checks the arc between last frame's direction and this one and hit all 24, and 200 of 200 targets at every spin speed and frame rate we tried, with none beyond the beam's reach.

The code

Save it as beam_sweep.gd and load it with const BeamSweep := preload("res://beam_sweep.gd"). Make one per beam. Each physics frame, after turning the beam, call crossed() for every target with the beam's origin and direction, then end_frame() once. Keep the RayCast3D for drawing: it still tells the beam where to stop and where to put the impact glow.

extends RefCounted
## A spinning or sweeping beam that hits everything it passes over, even when it turns
## further than a target's width in one frame (a fast spin, or a low frame rate).
## Angles are measured flat, on the ground (x and z), from the beam's origin.
##   var sweep := BeamSweep.new()
##   func _physics_process(delta: float) -> void:
##       rotate_y(spin_speed * delta)
##       var dir := -global_basis.z
##       for e in get_tree().get_nodes_in_group("enemies"):
##           if sweep.crossed(global_position, dir, e.global_position, beam_length, 0.5):
##               e.take_damage(10)  # give each enemy a short cooldown if the beam can stay on it
##       sweep.end_frame(dir)

var _last := Vector2.ZERO  # the beam's direction last frame, flat


## True if the beam passed over the target since the last end_frame(), or points at it now.
## radius = the target's size, so a body the beam only grazes still counts. Up to half a turn per frame.
func crossed(origin: Vector3, dir: Vector3, target: Vector3, length: float, radius := 0.0) -> bool:
	var to := Vector2(target.x - origin.x, target.z - origin.z)
	var dist := to.length()
	if dist > length + radius:
		return false
	if dist <= radius:
		return true
	var now := Vector2(dir.x, dir.z).normalized()
	var before := now if _last == Vector2.ZERO else _last
	var half := asin(radius / dist)    # how wide the target looks from the origin
	var turned := before.angle_to(now)  # this frame's turn, + one way, - the other
	var at := before.angle_to(to)       # where the target is, seen from last frame's beam
	if turned >= 0.0:
		return at >= -half and at <= turned + half
	return at <= half and at >= turned - half


## Call once per frame after checking every target.
func end_frame(dir: Vector3) -> void:
	_last = Vector2(dir.x, dir.z).normalized()

What we measured

A 12 m beam spinning one full turn on flat ground, with 200 targets inside its reach, 0.3 to 0.6 m in radius at 2 to 11.5 m, and 50 more just beyond it, at 30, 60 and 144 frames a second and two spin speeds. Then a quarter turn, a turn the other way, and a real RayCast3D against 24 StaticBody3D spheres 0.3 m wide, 10 m out. Two runs gave the same numbers.

Why it happens

A frame is a snapshot. Between two snapshots the beam jumps, it does not slide, so anything narrower than the jump can fall between them. At 10 m a body 0.3 m in radius looks about 3.4 degrees wide, and at 720 degrees a second the beam jumps 12 degrees a tick, so most bodies are skipped. The arc from last frame's direction to this one is everything the beam passed over. A target is hit if its direction falls inside that arc, widened on both sides by how wide the target looks from the beam's origin, and it is within reach.

What to change

Ready-made beams: Sci-Fi & Energy VFX for Godot 4 has Laser, Plasma, Mining and Repair beams that stretch until their RayCast3D hits something and put the impact glow there, plus a Scanner Sweep, an Energy Shield Bubble and 27 more presets in Stylized and Retro PSX looks.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Sci-Fi & Energy VFX for Godot 4 has Laser, Plasma, Mining and Repair beams that stretch until their RayCast3D hits something, plus a Scanner Sweep and an Energy Shield Bubble, in Stylized and Retro PSX looks.

More how-tos for Sci-Fi & Energy VFX for Godot 4: A laser beam that stops where it hits, the moment you aim

More how-tos: Hazards, traps and attacks