Why won't my enemy collision detection work?

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

So I am working on a 2d platformer in Godot3 and have come across an issue. I have an enemy that just bounces around as a kinematicbody2d and the player is also a kinematicbody2d. I attached an area2d to the player (and a collision-shape as a child of the area) and put the enemy in a group called “Enemy”. For some reason though when I test the game nothing happens when the enemy and player “collide”. I attached the on_body_entered signal to the player script and have defined it with the following code (The enemy node and all its children are in the “Enemy” group). I also tested it without the if statement and it worked, but it ran the code even when the player was only touching the ground since it was colliding with the ground. For some reason the if statement seems to be false when it should be true (when the enemy which is in a group call “Enemy” is making contact with the player containing the area2d node).

func _on_Area2D_body_entered(body):
if body.is_in_group("Enemy"):

I will attempt to respond to any questions and/or solutions as soon as I can.

Dragon11705 | 2020-01-24 02:12

:bust_in_silhouette: Reply From: estebanmolca

I clarify, in case you didn’t know that kinematicbody has its own collision detector, and returns the object / s collided with move_and_collide (). But even so the area would have to work the same, I am not an expert in this but looking at the signals of the area2d it has different signals to detect bodies and areas. You should use this signal from area 2d: area entered (area) and / or area exited (area)

func _on_Area2D_body_entered(body):
	if body.is_in_group("Enemy"):

func _on_Area2D_area_entered(area):
	if area.is_in_group("Enemy"):

I also make it clear that I am using version 3.2 beta 6

Thank you I will look into it more later and try it out.

Dragon11705 | 2020-01-24 13:38

I just ran my game again without chageing anything and it started working, but it only detects the enemy collision sometimes for some reason. I will keep looking into it though.

Dragon11705 | 2020-01-25 00:43

Also I am pretty sure that on area2d enter triggers when another area enters the current area, and onbodyenter is for when a physics body(which is inherited by rigid and kinematic bodies) enters the area.

Dragon11705 | 2020-01-25 00:45

Ok, it’s weird then, something is slipping out there. Try a new simple scene and see if the same thing happens to you. If it is a learning project and you want to share it so we can see how it is configured or upload an image of the node tree and the script where it gives problems,

estebanmolca | 2020-01-25 14:15