Cast the shot from the camera through the middle of the screen, then draw the tracer and spawn the impact from the muzzle to the point it hit. A ray straight out of the barrel runs parallel to the view, offset by where the gun sits: in our Godot 4.7 test, with the gun 0.3 m right of and 0.25 m below the camera, it landed 0.391 m from the crosshair at 2 m, 10 m and 30 m, and flew past a 10 cm pole the crosshair was on.
The code
Put this on the gun model, a child of the camera, with -Z pointing out of the barrel, and call shoot() when the player fires. hit_point, hit_normal and hit_collider tell you where to put the bullet impact and what to damage. The tracer goes from the node's position, the muzzle, to hit_point.
extends Node3D
## Hitscan shooting for a first-person gun that lands exactly on the crosshair.
## The ray comes from the camera, through the middle of the screen. The tracer and the impact
## then go from the muzzle to that point. Put this on the gun with -Z out of the barrel.
@export var max_range := 100.0
var hit_point := Vector3.ZERO ## where the last shot landed
var hit_normal := Vector3.UP ## the surface it landed on, for the impact effect
var hit_collider: Object = null ## what it hit, null if nothing
func shoot() -> void:
var cam := get_viewport().get_camera_3d()
var centre := get_viewport().get_visible_rect().size * 0.5
var from := cam.project_ray_origin(centre)
var to := from + cam.project_ray_normal(centre) * max_range
var q := PhysicsRayQueryParameters3D.create(from, to)
var hit := get_world_3d().direct_space_state.intersect_ray(q)
hit_collider = hit.get("collider")
hit_point = hit.position if hit else to
hit_normal = hit.normal if hit else Vector3.UP
# draw your tracer from global_position (the muzzle) to hit_point,
# and spawn the impact at hit_point facing hit_normal
What we measured
A camera 1.6 m up looking along -Z, with the gun 0.3 m to the right, 0.25 m down and 0.5 m forward.
- Walls at 2 m, 10 m and 30 m: the script's shot landed at (0, 1.6, -2.0), (0, 1.6, -10.0) and (0, 1.6, -30.0), right behind the crosshair. A ray straight out of the barrel landed 0.391 m from those points each time.
- A pole 10 cm thick at 10 m, centred on the crosshair, with a wall behind it at 30 m: the script hit the pole, 9.95 m away. The barrel ray missed it and hit the wall at 30 m.
- Nothing in range: no collider, and
hit_point100 m out along the crosshair, so a tracer still flies the right way.
Two runs gave the same numbers.
Why the barrel ray misses
The barrel and the camera point the same way but start in different places, so their rays are parallel and never meet. The gap is the gun's offset from the camera, the same at any distance, which is enough to miss a head or a thin pole the crosshair is clearly on. The camera ray is what the player aims with. The muzzle is only where the tracer starts.
What to change
max_range: how far a shot can reach.- If something of the player's own, like a shield, sits in front of the camera, add its RID to
q.excludebefore the ray is cast. - For a shotgun, cast several rays through points around the screen centre instead of the centre alone.
- A camera kick that settles back without pulling the aim to the centre: recoil kept apart from the aim.
Ready-made shooter effects: FPS Gun VFX for Godot 4 has 39 presets in Modern and Retro PSX looks: muzzle flashes that light the walls with a real OmniLight3D, tracers, shell ejects, and bullet impacts for concrete, metal, wood, dirt, water, glass and flesh that you aim from a raycast in one line.
For the flash itself: a muzzle flash with a light.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: FPS Gun VFX for Godot 4 has 39 ready-made shooter effects in Modern and Retro PSX looks, including muzzle flashes with a real light, tracers, shell ejects and bullet impacts for seven surfaces.
More how-tos for FPS Gun VFX for Godot 4: Hit sparks from a pool of emitters
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
- A spell that keeps flying straight after it leaves the hand
- Turn towards a target the short way round
- A laser beam that stops where it hits, the moment you aim
- 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