Area 2D how to prevent collision with the same body?

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

So I have done this

In my project the same kinematic body can be used by player and enemy. When the punch_hit area 2d collides with Hurt_box area 2d a hurt animation plays. I want the Hurt_box to ignore this when it’s own punch_hit is colliding.

Any ideas aside from adding new groups to the Area_2Ds?

:bust_in_silhouette: Reply From: Microices

Set up the physics layer, and assign it in the editor it should automatically work.

The Docs has a set by step for a the basic set-up it should work.

Thanks. I will check it out. I was trying to make it so that any character can hurt any character like in Grand Theft Auto or Skyrim.
I’m using a script to assign ID to every generated NPC and that ID gets added as a group to the character’s collision so when the punch collides with the character’s own collision shape it checks if the ID is the same as it’s own and returns the function.

Skydome | 2020-12-27 06:28

:bust_in_silhouette: Reply From: Lopy

You could check it manually. In the code run when the two areas collide, verify if it is the sibling Node.
Edit, if you have a script in Hurt_Box :
onready var own_punch = $"../Punch Hit" #“…” represents the parent Node.
func on_Hurt_Box_area_entered(area):
. if area != own_punch: #don’t punch yourself
. . pass #all good, you can punch

Alternatively, if enemies can only hurt players, and vice versa, you can use collision masks. punch_player collides with hurt_enemy, and punch_enemy collides with hurt_player.

:bust_in_silhouette: Reply From: vitavit

I would recommend reading this:

Layers and masks are not I tried before my own projects but I think this is your solution.

Thank you I will look at this. I’m sure it will help.

Skydome | 2020-12-26 16:21

:bust_in_silhouette: Reply From: Skydome

I’m answering the solution that works for me.

I wanted to make it like GTA or Skyrim where anyone can attack anyone. I just wanted the collision shape to not get triggered by their own punches.

Giving every character unique generated IDs works. After the ID is generated. Use var2str and add the area2d to the group var2str(ID). When area enter is being checked make sure your area doesn’t belong in var2str(This character’s ID).