Working with Collision Layers in Unity

Simon Leen
2 min readOct 1, 2021

Objective: Set up Collision Layers

Currently when using OnTriggerEnter2D, the sword hits the Player as the Player uses its own collider. To fix this issue, we will use a seperate layer for the sword. With the HitBox, we see the layer being used is Default.

Create a new layer called Sword.

Select the HitBox again and change the Layer to Sword. Do the same process for the Player on its own Layer called Player.

Next, go to Edit, Project Settings, Physics 2D and tell the Player to ignore the Sword Layer.

Now the OnTriggerEnter2D of the Hitbox ignores the Player. If we attack an enemy however we have no response. This is because our enemies have no colliders. Attach a collider to the Skeleton and add set it to Is Trigger. This will prevent the Skeleton pushing the Player when they collide as seen below.

With Is Trigger set on the Skeleton collider the Player and Skeleton can pass through each other and the Sword will also detect a collision with the Skeleton.

In the next post, we will look at adding damage.

--

--