Games feature numerous collisions. For many, it is the entire point of a game such as shooting spaceships or punching combatants. Yet, even in the most aggressive bullet-hell games, you’ll want to manage collisions. After all, you don’t want your enemies blowing themselves up before reaching the player.
Unity provides many ways to manage collision. One convenient method is using collision layers. The word ‘layers’ is somewhat misleading. In the age of Photoshop, it’s easy to think of image layers where you move parts of an image from forward to back. In Unity, layers act more like categories or zones.
By placing GameObjects in these physics zones, you can determine whether they interact with other layers.
Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.
Adding layers
To see layers in action, select the SpaceMarine in the Hierarchy. In the Inspector, find the Character Controller. Change the radius to 7.50. This surrounds the Space Marine with a large collider. You’ll even gain levitation powers.

Play your game. Now, fire your gun. Believe it or not, the gun is firing, but the bullets are hitting the marine’s collider, and then being instantly destroyed. This collision makes it look as though nothing is happening. You can solve this problem with selective collisions that you set up with layers.
Select the SpaceMarine, and in the Inspector, click the Layer dropdown. This will show all the current defined layers in the game. Click the Add Layer option

These are built in layers in the game. The first six layers are controlled by Unity but the rest are wide open. Starting with layer 6 and onwards, add the following: Alien, Alien Head, Bullet, Column, Floor, Head, Player, Wall and Upgrade.

Yes, you’ll use all those physics layers in your game. Notice how they correspond with the GameObjects.
Assigning layers to GameObjects
At this point, it’s time to assign your layers. You’ll start with a few but throughout this tutorial series, you’ll be adding layers in every chapter.
In the Hierarchy, select the SpaceMarine. In the Inspector, set the Layer drop-down to Player.

You’ll see a prompt that asks if you want to set the layer for all the child objects. Click No, this object only.

Note: GameObjects can only belong to one layer, but their children can belong to different layers, as you’ll see later in this book.
Next, in the Prefabs folder, select the Projectile prefab, and in the layer drop-down, select Bullet.

You’ve now added two GameObjects to your layers. Your final task is to configure the layers.
Configuring collisions layers
At this point, when a bullet collides with the space marine, these two GameObjects are on different layers. You can configure these layers to ignore each other by way of the collision matrix. To access this matrix, click Edit ▸ Project Settings. This is a central hub for all your settings.
You’ll notice there is a Tags and Layers category. This is another place for you to add additional collision layers.
To configure your layers, expand the Physics category and select Settings. You’ll see the collision matrix.

The y-axis shows the subject layers, and the x-axis shows the collision target layers. The checkmark indicates whether the subject layer will interact with the target layer.
In the Bullet row, uncheck the Player column. Now, the Player layer will ignore all interactions with the Bullet layer.

Play your game. Shoot your gun. Now, you’ll get bullets galore! Well done!
Before you save, select the SpaceMarine in the Hierarchy. In the Inspector, set the Character Controller radius back to 2.5. This brings your space marine back to the ground.
Where to go from here
Layers are critical for managing your collisions and you’ll be using them all the time in your game. You’ll also be using tags in the 2D games as well but that’s beyond the scope of this tutorial series.
At this point, you have a game called Bobblehead Wars, but your space marine doesn’t exactly bobble. Unity provides lots of physics options to make the marine’s head bobble with each movement. You’ll add this in the next tutorial.
Discover more from Jezner Blog
Subscribe to get the latest posts sent to your email.