0 votes

I'm trying to create an enemy with a Area2D node. The idea is that when the player, which is a KinematicBody2D node, intersects the Area2D, the Area2D will send a signal to its parent, the enemy. However, whenever the player and enemy intersect, nothing happens. Does it have to do with the fact that both bodies are moving? I have tried using the body_enter signal, and even gave the player its own Area2D and used the area_enter signal. Neither method appears to work.

The Area2D node is connected to the enemy node by the following line of code in the _ready() method:

$Area2D.connect("area_entered", self, "_on_body_entered")

And the signal connects to the following method:

func _on_body_entered():
    print("stab")
    emit_signal("stab")

However, as evidenced by no output being printed to the console, this method is never called.

I tried placing the following code directly into the enemy's Area2D, but still no results:

extends Area2D

func _ready():
    connect("area_entered", self, "_on_area_entered")

func _on_area_entered():
    print("Test")

I also tried the above with body_entered.

Here are the collisions:
Collisions

And here are the node trees:
Nodes

in Engine by (38 points)
edited by

You need to share more details of what you've tried and how you have things set up. By your description, that is the way it should work, so it's likely you're doing something wrong. body_entered is the correct signal. A couple of things to try:

  • Have you attached a CollisionShape2D to the Area2D? It won't work unless it has a shape defined.
  • Did you connect the signal in the Inspector, or via code? If the latter, please share.
  • How are you checking to see if it works?

1 Answer

0 votes
Best answer
  1. try to have the same collision layer.
  2. if the area2d is a child of the enemy there is no need to have that connect method in ready function. Just send body_entered signal to the enemy. your signal function returns nothing. when you do it the way I mentioned it should have (body) parameter, that you should use to do your actions.
by (54 points)
selected by

As it turns out, the problem was caused because the connected method did not accept any arguments. I assumed I could ignore this, as I didn't necessarily need to know what was intersecting the Area2D, but I now realize that the method won't work unless I provide arguments.

Thank you for your help.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.