← Pixel Dreamer Studio

Fast projectiles that never fly through enemies

Each physics frame, cast a ray over the distance the projectile is about to move.

Each physics frame, cast a ray from where the projectile is to where it will be, with intersect_ray(), and count a hit on the first thing in between. A projectile that only moves and waits for a collision is checked where it lands each frame, so at high speed it jumps over thin targets. In our Godot 4.7 test an Area3D bullet at 150 m/s, 2.5 m per frame, hit a target 0.6 m thick, as thick as a character, only 7 times out of 20. The script hit 20 out of 20.

The code

Make this the root of the projectile scene and put the mesh, trail or particles under it. It flies along its -Z, Godot's forward, so spawn it with the gun's or hand's rotation. Connect hit to deal damage and spawn the impact effect at point: the projectile stops on the surface it hit.

extends Node3D
## A fast projectile that cannot skip through thin enemies or walls.
## Every physics frame it checks the whole line it is about to cover, not only the point where it lands.

signal hit(target: Node3D, point: Vector3)

@export var speed := 80.0  # metres a second
@export var max_range := 200.0
@export_flags_3d_physics var mask := 1  # the physics layers it can hit
@export var hit_areas := false  # turn on if enemies use Area3D hurtboxes: rays skip areas unless asked

var _travelled := 0.0


func _physics_process(delta: float) -> void:
	var from := global_position
	var to := from - global_basis.z * speed * delta  # flies along its -Z, Godot's forward
	var q := PhysicsRayQueryParameters3D.create(from, to, mask)
	q.collide_with_areas = hit_areas
	var r := get_world_3d().direct_space_state.intersect_ray(q)
	if r:
		global_position = r.position  # stop on the surface it hit, for the impact effect
		hit.emit(r.collider, r.position)
		queue_free()
		return
	global_position = to
	_travelled += speed * delta
	if _travelled >= max_range:
		queue_free()

What we measured

20 projectiles at once, each starting a little further back so they reach the target at every point in a frame. The target is 0.6 m thick, the physics runs at 60 frames a second. The Area3D version is a sphere 0.2 m across that moves position every physics frame and counts body_entered. Two runs gave the same results.

Why fast projectiles fly through

Physics checks shapes where they are at the end of each physics frame, not along the way. When a projectile moves further in one frame than the target is thick plus its own size, some frames end before the target and the next one already past it, and nothing ever overlaps. At 60 frames a second that starts at about 48 m/s for a 0.6 m target and a 0.2 m bullet, and the faster it goes, the more shots slip through. A ray covers every point between the two positions, so the speed no longer matters.

What to change

Aiming the shot: make the shot hit the crosshair. Placing the impact: spawn the hit effect at the impact point.

Effects for the shots: Battle VFX Bundle for Godot 4 has 169 effects from 5 packs: FPS gun effects, spell projectiles with cast, trail and hit, block and parry effects, AoE telegraphs and status effects.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Battle VFX Bundle for Godot 4 has 169 ready-made combat effects from 5 packs: gun effects, spell projectiles with cast, trail and hit, block and parry, AoE telegraphs and status effects.

More how-tos for Battle VFX Bundle for Godot 4: Knockback that the walk code does not cancel · An attack that hits even when a frame is slow

More how-tos: Projectiles and aiming