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.
- A ship 60 m away crossing at 8 m/s, shot at 30 m/s: 15.46 m, 0.56 m, 0.00 m (2.08 s in the air).
- The same ship 120 m away: 30.92 m, 1.12 m, 0.01 m.
- Sailing away at an angle: 14.55 m, 2.71 m, 0.04 m. Coming closer at an angle: 9.86 m, 2.18 m, 0.02 m.
- Our own ship moving at 5 m/s, with the shot keeping that speed: 13.37 m, 2.04 m, and 0.06 m with the target velocity minus our own passed to the script.
- A ship sailing away at 12 m/s and a 10 m/s shot: every aim missed, and the script gave null.
- 2D, an enemy 200 px from a tower walking at 80 px/s, bullets at 300 px/s: 51.53 px, 1.93 px, 0.01 px.
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
- Shots that fall in an arc need more than this, because their speed toward the target changes as they fly: launch speed for a target at a height.
- Turning the cannon toward the aim point the short way: a turret that turns the long way around. Shots so fast they pass through: a raycast each frame.
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
- A ball that hits the net however hard it is kicked
- 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
- 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