Enemy not being detected by radius for Tower Defense Game

Godot Version 4.4.1

Hello, I ran into an issue with my detection radius for programing a tower defense game. Here is the following code I used.

# Detection for Enemy in radius
func _on_area_2d_body_entered(body: Node2D) -> void:
	print("Enemy is on unit radius")

# Called when enenmy leaves radius
func _on_detection_radius_body_exited(body: Node2D) -> void:
	print("Enenmy left unit radius")

The following is supposed to print when the enemy interacts with the radius, the top half is supposed to print when the enemy enters the unit’s detection radius while the bottom half is supposed to print when the enemy leaves the unit’s detection radius, this is to make sure the units know when to shoot the enemies.

However nothing is being detected, is this a hierarchy issue? My current hierarchy for the level is:

  • Detection Radius (Area 2D)
    • Unit Sprite (Sprite 2D)
    • Radial Shape (Collision Shape 2D)
  • Path 2D
    • Enemy (Path 2D Follow)

Is your enemy a PathFollow2D?
If so, that’s why your code is not working.

A Path2D or PathFollow2D is not a body. Your current code only detects bodies, such as CharacterBody2D or RigidBody2D.

Whenever this happens to me. I double (eventually) double check the collision_layer and collision_mask of the detector and the thing you want to detect.

To go a bit further with what @tibaverus said, you are using the “body_entered” signal for your tower when you should use the “area_entered” signal. Also, you’ll need an area on your enemies for the detection area to “see”.