Area2D not detecting anything properly.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SharkRabbit

I have a TileMap node with it’s collision box all set up and a bullet with an Area2D. The bullet sends a body_entered signal to itself and i’ve specified “TileMap.” When the bullet hits the tiles, nothing happens. I feel like I’ve done everything correctly? It also worked before and now it doesn’t.

func _on_BlueBullet_body_entered(TileMap):
print(“SIGNAL”)

If you want screenshots or more information let me know.

:bust_in_silhouette: Reply From: automatrio

Okay, there’s a more conventional approach that might just get it working.

First of all, I suppose it makes more sense if the Area2D is a child of the TileMap, instead. Connect its signal to its parent and write something like this:

func _on_Area2D_body_entered() -> void:
    if body.is_in_group("bullets"):
        #do whatever you want now

Now we should add the bullet to that “bullets” group, and we can do it in its constructor, i.e, the _init() method:

func _init() -> void:
    self.add_to_group("bullets")

The bullet should be a either a KinematicBody 2D or a RigidBody2D with its CollisionShape2D properly set up.

Do tell me if it worked for you.

I really appreciate your response and it really sounds like you know what your doing, which obviously, I don’t.

I have a couple comments:

  1. If I were to make a bullet that only needed to go up/down and left/right at a constant speed then should I be using Area2D or like you said here, Kinematic or RigidBody2D. I’ve used KinematicBody2D as a bullet before and I didn’t like it because it messed up a lot of physics in my game.

  2. Your code did not work, it seems that how it is set up is not the problem because it used to work in my less conventional code but all of a sudden stopped working.

Again, thank you for your response.

SharkRabbit | 2021-02-06 01:43

Anytime!

I’m sorry it didn’t work, yet there are still a couple more things we could try:

  • in the editor, make sure neither the area nor the collision shape is disabled;
  • make sure the Area2D’s collision layer and mask correspond to the Tile map’s (the Area2D’s active mask should be the same as the Tilemap’s active collision layer);
  • did you create a collision shape for the tiles? Is it working?

I’m all out of ideias now, so if it still doesn’t work, try posting some of your code here for us to investigate in greater depth.

automatrio | 2021-02-06 09:15