← Pixel Dreamer Studio

FPS shots that land on the crosshair

Aim with a ray from the camera. Draw the tracer from the muzzle to where it hit.

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.

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

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