• CommunityForum
  • StaticBody2D will not trigger _on_body_entered() function in Area2D

I have been trying to make a simple shooter game in the past few days. However, whenever a bullet (an Area2D will an on_body_entered() signal/function) collides with an ammo crate (a staticBody2D) it completely ignores the crate, and the body entered function does not trigger. I have already checked the collision masks and layers.

This is the on body entered function of the bullet (which does not get triggered when colliding with the ammo crate):

func _on_body_entered(body):
if "hit" in body:
body.hit()
queue_free()

This is the code in the ammo crate:

extends CrateContainer

func hit():
print("This crate has been shot.")

The ammo crate has a parent class called CrateContainer, with a sprite2D node and a CollisionShape node (both of which have no textures or shapes)

    BobJefferson first, I'd see if your signals are connected. If you haven't connected a signal in code or the inspector, it won't trigger if you code it by hand. So you need to connect that signal in the Area2D somewhere so it detects the body. Don't connect this signal on the bullet as bodies only detect collision and not entering areas. Have the AREA check for the body. (You don't need it on the bullet as the Area will resolve it.)

    BONUS: You may also consider a check that checks for the body name rather than just "body" as any body will trigger it. Something like if "bullet" in body.name: for logic, or else you may only trigger on one projectile. I don't know your project setup so you may not need this.

    You may also want to use Markdown (three ~ symbols at start and end) so the code displays properly here so we can count out stuff like syntax errors.

    BobJefferson if "hit" in body:

    Is that the actual code? I do something similar, but I use this:
    if body.has_method("hit"):

    Add this at the beginning of _on_body_entered:
    print_debug("body name=", body.name)

    What's your Godot version?