Working with Collision Layers in Unity

Working with colliders you get into the habit of thinking that you are only checking that the object you are interested in has collided. Very wrong.
When working with 2D colliders all objects are on the same plane, so the hitbox I mentioned yesterday hits the player first.

But obviously, Unity Technologies has thought of this situation. Layers can be assigned and created exactly as I do for the tags under the name of each game object.

Let’s take our player and create a new layer, I’ll call it Player, that we will assign to him and consequently to all the elements inside.

Now let’s select the game object that interests us, in my case, it is the sword object that I have called hitbox. Let’s create a layer for him too that I will call Sword and I will assign it exclusively to him.

The last step is necessary to open the Project Settings. And under the heading Physics 2D, we will find at the bottom the field Layer Collision Matrix

In this matrix, we only need to deselect the voice sword under the column of the player to make our sword ignore him.

project manager by day, unity developer by night