← Pixel Dreamer Studio

A shot that meets a moving target

Work out when the shot and the target reach the same point, then aim there.

Aiming at a moving target's current position misses, because it has moved on by the time the shot arrives. In our Godot 4.7 test a 30 m/s shot fired at a ship 60 m away sailing across at 8 m/s missed by 15.46 m. The common quick fix, aiming ahead by the distance divided by the shot speed, came within 0.56 m on a straight crossing but missed by 2.71 m when the ship sailed away at an angle. Solving for the point where the shot and the ship meet hit within 0.06 m in every case, in 3D and in 2D.

The code

Save it as lead_aim.gd, load it with const LeadAim := preload("res://lead_aim.gd") and aim with var aim = LeadAim.point(muzzle.global_position, ship.global_position, ship_velocity, shot_speed). If it gives null, the target is getting away faster than the shot can fly, so hold fire. ship_velocity is how fast the target moves per second, for example velocity on a CharacterBody3D or linear_velocity on a RigidBody3D. The same call works with Vector2 positions for 2D towers. It is for shots that fly in a straight line at a constant speed.

extends RefCounted
## Aims a straight shot at where a moving target will be when the shot gets
## there, instead of where it is now. Works with Vector3 and Vector2.
##   var aim = LeadAim.point(muzzle_pos, ship_pos, ship_velocity, 30.0)
##   if aim != null:
##       fire(muzzle_pos.direction_to(aim))
##   # null = the target is too fast, a shot at this speed can never reach it
## If the shot also carries the shooter's own speed (a cannon on a moving ship),
## pass target_velocity - shooter_velocity.


static func time_to_hit(from, target, target_velocity, shot_speed: float) -> float:
	var d = target - from
	var a: float = target_velocity.dot(target_velocity) - shot_speed * shot_speed
	var b: float = 2.0 * d.dot(target_velocity)
	var c: float = d.dot(d)
	if absf(a) < 0.0001:  # the target is exactly as fast as the shot
		return -c / b if b < 0.0 else -1.0
	var disc := b * b - 4.0 * a * c
	if disc < 0.0:
		return -1.0
	var r := sqrt(disc)
	var t1 := (-b - r) / (2.0 * a)
	var t2 := (-b + r) / (2.0 * a)
	var t := minf(t1, t2)
	if t < 0.0:
		t = maxf(t1, t2)
	return t if t >= 0.0 else -1.0


static func point(from, target, target_velocity, shot_speed: float):
	var t := time_to_hit(from, target, target_velocity, shot_speed)
	if t < 0.0:
		return null
	return target + target_velocity * t

What we measured

Each shot and its target flew forward together in steps of 1/240 of a second, and we kept the closest they got. Three aims each time: at the target, ahead by distance over speed, and the script. Two runs gave the same numbers.

Why it happens

Aiming ahead by distance over speed uses the time the shot needs to reach where the target is now. By then the target is somewhere else, at a different distance, so the time is wrong too, and more so when the target moves toward you or away from you. The script asks a different question: at what time t is a shot, fired now at shot_speed, at the same place as the target moving at its velocity? That is a quadratic in t. The smallest answer that is not in the past is the hit, and no such answer means the target can never be caught.

What to change

What the shot looks like: Naval & Pirate VFX for Godot 4 has 24 ready-made ship battle effects, including Cannon Fire, Broadside Volley, Cannonball Splash, Deck Fire and Ship Sinking.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Naval & Pirate VFX for Godot 4 has 24 ready-made ship battle effects, including Cannon Fire, Broadside Volley, Cannonball Splash, Deck Fire and Ship Sinking.

More how-tos for Naval & Pirate VFX for Godot 4: Boat wake particles that follow the speed

More how-tos: Projectiles and aiming