0 votes

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 onbodyentered 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"):
    emit_signal("Hit_Enemy")
    print('dead')
    get_tree().reload_current_scene()
in Engine by (31 points)

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

1 Answer

+1 vote

I clarify, in case you didn't know that kinematicbody has its own collision detector, and returns the object / s collided with moveandcollide (). 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"):
        print(body)

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

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

by (2,058 points)

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

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.

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.

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,

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.