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.
- A plain RayCast3D turned 90 degrees to the right and read straight away: 4.90 m, the old wall. After one physics step: 7.90 m.
- The script, turned and fired: 7.90 m, with the light at (7.9, 1.0, 0.0), on the wall.
- A new wall added across the beam 2.90 m away: the plain ray read straight away still said 7.90 m, the script said 2.90 m.
- Aimed at nothing: the beam was the full 30 m and the light was off.
- Fired from inside the shooter's own capsule, with the ray under a gun node under a CharacterBody3D: it went through the capsule and hit the wall 14.90 m away.
hit_from_insideis off by default, so a ray that starts inside a shape does not hit that shape.
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
max_length: how far the beam reaches when it hits nothing.widthandcolor: a thin red laser, a wide orange mining beam.- If the ray starts outside something it must not hit, like a shield the player carries, add that body with
add_exception(). - A beam that spins or sweeps fast: check the whole arc it turned through.
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
- A ball that hits the net however hard it is kicked
- A shot that meets a moving target
- An Area3D that does not see a bullet or another area
- A fire rate that holds at any frame rate
- Point swords, projectiles and models that face +Z at the target
- FPS shots that land on the crosshair
- A spell that keeps flying straight after it leaves the hand
- Turn towards a target the short way round
- Fast projectiles that never fly through enemies
- A trajectory line that matches where the ball lands
- Aim a spell at the mouse on a 3D floor
- A homing missile that turns, but not instantly
- Particles fall sideways when you rotate the emitter
- Particles that fly into the player
- Gaps in the trail of a fast emitter
- Make a tower shoot the enemy closest to your base
- Hit the target with an arcing shot at any angle and height
- Kick a RigidBody3D ball with the same strength at any tick rate
- Reload without losing or making up rounds
- Fan out any number of projectiles evenly around the aim direction
- Recoil that settles back to where the player aims