Unity Developer — Collision Layers

When dealing with triggers and collisions, we used the technique of checking tags before performing the required task. This works especially well when there is only one thing that you want to have a trigger for, but there is another method when accounting for multiple objects. That is setting up your collision layers.

Collision Layers are a simple and easy way to make sure that objects only interact if desire. We have a player with a hitbox and an enemy with a hitbox. We can set up their layers in their Inspectors. In the top right of any Inspector, go to Layers and Add Layer.

Here you can add as many layers as you like. We are going to add a layer for the Player and Enemies. We are also going to add a layer for their attacks, called ‘Sword’ and ‘EnemyAttack.’ You could name these anything that makes sense for you and your project.

After the new layers are set up, assign them on the proper game objects.

For the final step, go to the Project Settings and Physics2D.

Here you will see a cross section of all the layers. All you have to do is untick any collisions you don’t want to happen. In our case Player/Sword shouldn’t interact, because we don’t want the Player to be hit with their own hitbox. Same goes for the Enemy and EnemyAttack.

And that’s it! With the power of layers, you can fully control what you want to interact and what will pass each other by. You can use this to prevent Players from hitting each other in multiplayer, enemies from hitting each other, or you can even do some interesting layering in the environment. Pass through water, but not ground. Let your creativity run wile.

--

--

--

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

Recommended from Medium

Advantages of Migrating to NCache from AppFabric

Day 9

Abusing Azure Container Registry Tasks

All About Swiftly

Configuration of GitLab runner on AWS Linux EC2 instances (Ubuntu)

Interactive OpenGL development with Clojure and LWJGL

Useful ethtool commands.

Object-Oriented Programming

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
Christopher Graf

Christopher Graf

More from Medium

Unity Physics

Creating objects in Unity

Different Lights in Unity

Unity Performance Optimization Ⅶ: Physics