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.
- Checking only where the beam points, like a raycast, at 180 degrees a second: 181 of 200 at 30 fps, where it turns 6 degrees a frame, and all 200 at 60 and 144.
- The same at 720 degrees a second: 79 of 200 at 30 fps (24 degrees a frame), 139 at 60 and 197 at 144.
- The script: 200 of 200 in every one of those six runs, and 0 of the 50 beyond the beam. Turning the other way: 200 of 200.
- A quarter turn at 720 degrees a second: 60 targets inside the 90 degree arc. The script hit all 60 and none outside it at 30 and 144 fps. Only checking where it points missed 26 at 30 fps and 1 at 144.
- A real
RayCast3Don a node turned withrotate_y()andforce_raycast_update()every physics tick: 6 of the 24 spheres. The script on the same ticks: 24 of 24.
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
radius: the target's size. 0 counts only the target's centre, a bigger number counts a graze.- If the beam can stay on a big target for several frames, give each target a short cooldown for hits, or use damage times delta for a steady burn.
- It works for up to half a turn per frame. Faster than that, split the frame's turn into steps.
- Walls: the script only checks the angle and reach. Cast a ray to a target it says was crossed, to see that nothing stands in between.
- The beam itself stopping at what it hits: laser beam stops at the hit. A sword or a fast swing: hit each enemy once per swing. Bullets that skip through thin enemies: raycast each frame.
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
- 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
- Explosion damage that reaches big enemies