+2 votes

Hi guys !

After some time of research, I don't find how to do what I want to do.
My main player (2D) has a sword in her hand and I want to detect the collision of the sword with ennemies of course (which are of RigidBody2D type).

However, concerning the physic engine, I don't want the sword to interact with anything, even not with the ennemies. So for me making it a rigidbody2d or staticbody is out of question. My first attempt was to make it as an Area2D, but in that case, the collision are not catched by the ennemies (collision triggering ennemies damage).

Is that clear ? Do you have any idea ?

in Engine by (101 points)

1 Answer

+1 vote

All "collisions" with an Area trigger on the area itself, not on the other body.
So you could connect to "body_enter" and test if the body is an enemy, then call the damage function.

an example (off the top of my mind, not tested):

func on_area_body_enter(body):
    if body in get_tree().get_nodes_in_group("enemy"):
        body.damage()
by (912 points)
Yeah, I thought about that also. It should work, but I would have prefer to delegate it to the ennemy class itself, after all it's the ennemy which is hurt...
Thank you !
Alright, then how about adding an area also for the enemy? So it would have both a rigidbody, with collision shapes that allow it to move around the world, and an area that checks for damage collisions.

Area/Area collisions will be triggered on both objects.
I thought about it too!!
It will work for sure, but the inconvenient is to have too different collision shape for each ennemy: one for the rigid body behavior and one for the Area2D.
That was the reason why I discarded this idea.

 However, I think it's not so bad after all. And it makes sense also, ennemies might not be vulnerable on all their body ;-)

Thank you very much for your quick responses !
It works fine!

Except when my ennemy is also an Area2D....
It seems to me that Area2D don't report collision with Area2D (such has in the case StaticBody2D vs Area2D). Is this a bug or is this only me?
I found the issue! I was using body_enter on the sword which doesn't monitors area. Using additionally area_enter when sword enter a Area2D made it work !
pfiouuuu !
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.

Categories