Among the enemies in range, shoot the one with the smallest curve.get_baked_length() - progress: the least path left before it reaches your base. In our Godot 4.7 test, a tower next to the first leg of a U-shaped path that shot the closest enemy let 17 of 40 enemies through. Shooting the enemy with the least path left let 5 through.
The code
Put it on the tower. Your enemies are PathFollow2D nodes under a Path2D, in the group enemies, moved by changing progress. Call pick_target() when the tower is ready to fire. It returns null when nothing is in range.
extends Node2D
## Tower defense targeting: shoot the enemy that is furthest along its path, not the closest one.
## Enemies are PathFollow2D nodes (children of a Path2D) in the group "enemies". Move them with progress.
@export var attack_range := 160.0
@export var enemy_group := &"enemies"
func pick_target() -> PathFollow2D:
var best: PathFollow2D = null
var best_left := INF
for node in get_tree().get_nodes_in_group(enemy_group):
var f := node as PathFollow2D
if f == null or not is_instance_valid(f):
continue
if global_position.distance_to(f.global_position) > attack_range:
continue
var path := f.get_parent() as Path2D
# distance still to go: works across several paths of different lengths
var left := path.curve.get_baked_length() - f.progress
if left < best_left:
best_left = left
best = f
return best
What we measured
A U-shaped path, 1,000 pixels long. The tower stands 60 pixels from the first leg, and its 160-pixel range also reaches the last leg. 40 enemies with 3 HP, one every 1.2 seconds, moving 80 pixels per second. The tower hits once every 0.5 seconds. Fixed 60 FPS, two runs with the same numbers.
- Shooting the closest enemy: 17 of 40 got through.
- Shooting the first enemy in the group, the one that spawned first: 5.
- The script: 5.
- With every fourth enemy twice as fast: closest 9, first in the group 9, the script 7. Of the fast ones, 9, 9 and 6 got through.
- One enemy every 1.6 seconds instead: 0 got through with all three. Targeting only matters once the tower cannot kill everything.
- Two enemies in range: one 100 pixels along the path and 116.6 pixels from the tower, one 750 pixels along and 148.7 pixels away. Closest picked the first one, the script picked the one at 750.
Why it happens
The enemy nearest the tower is often one that just walked in, while the leaders are at the edge of the range and about to reach your base. On a path that bends back past the tower, closest keeps shooting the new arrivals on the near leg. The first enemy in the group is the one that spawned first, which is also the one furthest along only while every enemy moves at the same speed. Fast enemies pass slow ones, and then spawn order and path order no longer match.
What to change
attack_range: in pixels, measured from the tower to the enemy.enemy_group: the group your enemies are in.
The path left is counted per path, so enemies on two paths of different lengths are compared fairly. In 3D, PathFollow3D and Path3D have the same progress and get_baked_length().
Show the range on the ground: tower attack range ring.
Effects for the whole loop: Tower Defense VFX for Godot 4 has tower place, upgrade and sell, spawn portals, Base Hit and Enemy Leak, slow fields, Mortar Impact and Chain Lightning Hit.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Tower Defense VFX for Godot 4 has Enemy Leak and Base Hit for the enemies that get through, and Mortar Impact and Chain Lightning Hit for the ones your towers catch.
More how-tos for Tower Defense VFX for Godot 4: Chain lightning between enemies · A tower's attack range ring
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
- 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
- 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