← Pixel Dreamer Studio

A laser beam that stops where it hits, the moment you aim

Call force_raycast_update() after you turn the ray.

Call force_raycast_update() on the RayCast3D after you aim it and before you read get_collision_point(). A RayCast3D casts once per physics step, so reading it right after turning it gives you the hit from where it pointed before. In our Godot 4.7 test a ray turned from a wall 4.90 m away to a wall 7.90 m away still said 4.90 m until the next physics step. The script below said 7.90 m straight away.

The code

Add this as a child of your gun with its -Z axis pointing out of the barrel, keep the node unscaled, and call fire() every frame the trigger is held, after you aim. It draws the beam as a thin box from the gun to the hit and puts a light at the hit point. length is how long the beam is now, for damage or a hit sound.

extends RayCast3D
## A laser beam that stops at the first thing it hits, in the same frame you aim it.
## Add it to your gun with its -Z axis pointing out of the barrel, and keep the node unscaled.
## Call fire() after you aim, every frame while the trigger is held.

@export var max_length := 30.0
@export var width := 0.05
@export var color := Color(1.0, 0.25, 0.15)

var length := 0.0  ## how long the beam is now
var _beam := MeshInstance3D.new()
var _glow := OmniLight3D.new()


func _ready() -> void:
	target_position = Vector3(0, 0, -max_length)
	var box := BoxMesh.new()
	box.size = Vector3(width, width, 1.0)
	_beam.mesh = box
	var m := StandardMaterial3D.new()
	m.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
	m.albedo_color = color
	_beam.material_override = m
	_beam.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
	add_child(_beam)
	_glow.light_color = color
	_glow.omni_range = 1.5
	add_child(_glow)
	fire()


func fire() -> void:
	force_raycast_update()  # otherwise the result is from the last physics step, before you aimed
	length = max_length
	if is_colliding():
		length = global_position.distance_to(get_collision_point())
	_beam.scale.z = maxf(length, 0.001)
	_beam.position = Vector3(0, 0, -length * 0.5)
	_glow.position = Vector3(0, 0, -length)
	_glow.visible = is_colliding()

What we measured

A ray 1 m up and 30 m long, a wall 4.90 m in front of it and another 7.90 m to its right.

Two runs gave the same numbers.

Why the beam lags

The RayCast3D node casts its ray during the physics step and keeps that result until the next one. Turning the node or moving a wall changes nothing until then. force_raycast_update() casts again right now, from where the node is now and against the bodies as they are now, so the beam and the hit agree in the frame you aimed.

What to change

Ready-made beams: Sci-Fi & Energy VFX for Godot 4 has 33 presets in Stylized and Retro PSX looks, including Laser, Plasma, Mining and Repair beams that stretch until their RayCast3D hits something and put an impact glow there, an Energy Shield Bubble that ripples where it is hit, and a Black Hole.

For aiming at the mouse in 3D: aim a spell at the mouse.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Sci-Fi & Energy VFX for Godot 4 has 33 ready-made sci-fi effects in Stylized and Retro PSX looks, including Laser, Plasma, Mining and Repair beams that stop where they hit, an Energy Shield Bubble and a Black Hole.

More how-tos for Sci-Fi & Energy VFX for Godot 4: Let the shield take what it can and pass the rest of the hit to health · A spinning laser that hits everything it sweeps over

More how-tos: Projectiles and aiming