Collision Layer in Unity- Game Dev Series 105

Objective: use layer collision setting to avoid Player attacking itself.

In the previous article, we created a hitbox to attack. However, once you test the function, you will notice that the sword would hit Player itself.

The Sword hit ourself.

To avoid this situation, we need to use layer.

Create layer to sword and Player

First we need to add a new layer. Select Player and find drop down menu of Layer in Inspector on the top right, then select “Add Layer”.

Inside Layer window, find an empty column and create a new name. In here, we can create a “Sword” and a “Player”.

Then we can assign the Layer to Player and Sword. When assign layer to Player, it would ask if you want to change layer to all children. It is fine to select agree, later we can select sword layer individually to Sword.

Once finished setting layers, we can make the sword to avoid hitting Player.

Layer Collision Matrix

Open Project Setting from the drop down menu. Then select Physics2D, find out Layer Collision Matrix at the bottom. Uncheck between Player and Sword.

Once you uncheck it. These 2 layers would no longer collide with each other.
Now you can freely to swing your sword!




A passionate gamer whose goal is to work in video game development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Optimizing Liquidity Allocation in Balancer Protocol

Naija Coding Series: Episode 1

Installing and Running KanjiTomo for Macs

The “Business card raytracer” in python, part 2

How I Built #1 Product of the Day on Product Hunt

Home Networking: Enable Pi-Hole On Asus Routers

Object Detection via Smartphone Camera using Faster R-CNN

Develop Your Own Operating System (part 0)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
S.J. Jason Liu

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

More from Medium

Building a Custom Cinemachine Virtual Camera Zoom Feature with One Button in Unity

Player Movement in Unity

Adding FPS Functionality to the Player Controller in Unity 2021

Unity : AI [Navmesh implementation (Part II)]