Help with Navigation Agents

Godot Version

4.6

Question

Is there a way to have many objects follow a single navigation agent’s path? I am making a tower defense game and I want to be able to place towers and change the Navigation Region when a route gets interrupted, but rather than having every individual enemy have their own navigation agent I want there to be one that all of them then just follow like its a PathFollow node. I hope that makes sense.

If you want a frame of reference I want the enemies to function like the Tower Defense game Tower Madness.

Just have the first enemy navigate and have the next one follow it, and the next one follow that one. Then you get the snake-like following you’re looking for. Each enemy has a reference to the enemy in front of it. If an enemy dies, you have the enemy behind it link up to the one it was following. If the first one dies, the second one will be updated to follow no one and it’ll start figuring out the path.

Prototype:

class_name Enemy extends CharacterBody2D

signal died(new_leader: Enemy)

@export var leader: Enemy # doesn't need to be exported, but might make testing easier.

func _ready() -> void:
	leader.died.connect(_on_leader_died)


func _on_leader_died(new_leader: Enemy) -> void:
	 leader = new_leader

func _physics_process(delta: float) -> void:
	if leader:
		#set the navigation agent to follow the leader
	else:
		# set the navigation agent to target the base.

	#Have the navigation agent follow the next segment of its path.
1 Like