Level Design in Unity 3D Part 6: Colliders
Colliders are working behind the scenes in most games that you play. Unless you want your player to be able to walk through walls, or fall thorough the ground unintended, adding colliders to your game scene is the way to go.
Lets start adding colliders to this science fiction game environment that I have been assembling with assets from Filebase. Create a new empty object and call it colliders. This will act as the main folder for all colliders in the game scene that are not attached to an object.
It’s important to make sure the position is set to zero.
A cube primitive 3D object enters the game scene with a box collider on it, which is exactly what I want here. The box collider is what will be used to block out anything I don’t want the player walking or falling through.
The overhead orthographic view is perfect for quickly resizing the cube to span the entire hallway floor.
The surface is then raised into place just barely above the base floor mesh.
This cube turned floor collider is renamed in the hierarchy to reflect it’s location in the scene.
The top-down orthographic view is once again very useful for duplicating the hallway cube collider, and then repositioning and resizing it into the large room.
This new floor collider is also appropriately named in the hierarchy for organizational purposes.
Here are both floor cube colliders in the scene view.
Both are selected and the Mesh Renderer is turned off for invisibility.
The big floor collider is now duplicated and rotated 90 degrees to become a wall collider. This first one will sit at the main rear wall of the big room.
Here is the rear wall cube collider after making position and scale edits.
The two opposing walls are made in the same fashion, by relocating duplicates of the rear wall.
I keep moving around the scene working towards the main hallway.
Hallway walls are added into the scene.
The door wall is composed of three cube collider objects: left, right and center.
All wall colliders have descriptive names for easy troubleshooting.
As for the columns, a single column is selected and a box collider component is added in the inspector. The edit collider button is pressed to let me really get the collider in the shape of the column.
The player won’t be walking on the roof or shooting projectiles so the little portion at the top of the column here doesn’t matter.
This column is a prefab, so the Overrides drop-down can be toggled to showcase the changes that have been made to the prefab, like adding a box collider. After the changes have been accepted, the original prefab will now have a this box collider added to it, which also puts a box collider on all of the other column prefabs in the scene!
You can see here in the inspector as I toggle through the different columns in the scene, that they all now have a box collider component attached.
The same method is applied when adding box colliders to the smaller columns near the front entrance.
The overrides button is once again used to save the colliders on the prefab, and update the other ones in the scene.
Here I select the different archways and the colliders can be seen when selected.
The door, being the only object in the scene marked for player interaction, gets it’s own box collider. This way, when the door is opened by the player, the collider will move with the door and the player will be able to pass.
Each of the main consoles in the laboratory room are selected, and box colliders are added to all three with one component addition in the inspector.
Box colliders are also added to the reactors and test tubes. A mesh collider or capsule collider might be more useful here depending on what you want to do in your game concerning player interaction. For my purposes here, a low overhead cost box collider will do just fine.
The display platform gets a large cube collider, protecting the entire lower area from accidental access.
The stairs basically get a ramp (rotated cube), that conforms to the height and depth of the stairs. This will create a smooth transition for the player rather than some potentially very jittery looking climbing.
The stair and platform objects are named appropriately in the inspector.
Here is the laboratory room currently, with the mesh renderers off on the cubes, but the all colliders showing via selecting the objects in the scene.
It’s time to test out the game scene by using Unity’s first person controller setup in the Package Manager.
If you can’t find it in the asset manager, you can go online and search for it. You should see this screen on the Unity Asset store. Press the Add to My Assets button. I first downloaded the third person controller here, but I will end up using the first person controller for this demo.
Now the Package Manager should show the Starter Assets for the controller when viewing My Assets, under Packages.
All that is needed now is to download the assets and then press the import button.
With the first person controller installed, I can go to Tools > Starter Assets > Reset First Person Controller.
A 3D capsule primitive object to represent the player, as well as a cinemachine camera to follow the player, are automatically added into the game scene.
Here you can see the player capsule inside the front door.
After pressing play, the player can move about the room, not fall through the floor and not go through walls thanks to the colliders!
Here the player goes up the stairs that were made with the ramp collider for a smoother movement.
This scene is a little dark, so I hope you join me in my next article where I start adding lights to the scene. Thanks for reading!