← Pixel Dreamer Studio

Make a tower shoot the enemy closest to your base

Pick the enemy with the least path left, from PathFollow2D progress, not the one nearest the tower.

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.

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

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