Problems with collision detection using component

Godot Version

v4.3 stable

Question

Hi guys!

I’m developing a game and I’m having trouble setting up collision detection between a tower and an enemy.

Game structure (https://github.com/kliver985/endless-override):

  • enemy.gd: script responsible for the enemy.
  • tower.gd: script responsible for the tower.
  • range_area.gd: script for the tower’s range (using Area2D).
  • range_component.gd: script for collision detection and range logic.

The idea is that when an enemy enters the turret’s range (range_area), the system detects the collision and the turret can attack him. However, the collisions are not being detected correctly. The enemy enters the turret’s radius, but nothing happens - the signals (body_entered, area_entered) are not being fired as expected.

What I’ve already checked:

  1. The turret and the enemy have CollisionShape2D properly configured.
  2. The CollisionLayers and CollisionMasks seem to be correct (turret on layer 1, enemies on 2, and vice versa in the masks).
  3. The turret’s range (range_area) is active and has a connected signal.
  4. I’ve tested with monitoring = true and monitorable = true.
  5. No errors are displayed on the console.

Question:

What else can I check to ensure that the collision between the enemy and the tower works correctly? Any tips on how to debug this kind of problem? Is it possible that some component is getting in the way of detection without me realizing it?

If anyone has experienced this or can help me identify what I’m missing

Is the distance what you think it is? You could always manually compute the distance for debugging:

tower.gd:

func _process(delta: float):
    [...]
    var player_range = global_position.distance_to(player.global_position)
    if player_range <= AGGRO_RANGE:
        print("ATTACK!")
    [...]

If that behaves as expected, maybe turn on all the bits in the masks to see if there’s a mismatch there. I’d probably also suggest throwing logging in at every step and looking at what doesn’t appear in the logs.

The distance is working fine.

As you can see at the image, the range is being calculated but not the collision.

My idea here is to have one object to control the range and the collision, and to be loaded in the enemy or the tower if the component is available for it.

After a lot of testing, debugging, and cracking head, I just realized that the collision shape inside my range area component, inside the range component was disabled :dizzy_face:

1 Like