2D avoid hit box with layers in Unity

Daniel Kirwan
2 min readSep 13, 2021

Objective: Make sure that my player cannot hit itself with its weapon

In a previous article I added in a hitbox to my sword when the attack animation is playing. But I ran into an issue that the sword was colliding with the player and then not registering the hit on any enemy. To get around this I created two new layers called Player and Sword.

Add the player layer to the player, this will prompt you to changed the children as well, click yes.

Once the player layer has been added you can then add the sword layer to the hitbox.

Now, there is one more thing I needed to do. I need the player layer to ignore collision on the sword layer. Click edit>project settings > Physics 2D.

This will make it so that the player colliders will ignore anything it collides with on the sword layer. This means that I can now hit my enemies.

--

--