3D Level Design: Adding Colliders
Intro
In this article, we are going to cover how to add simple colliders to your level to allow a character controller to navigate the level without walking or falling through it.
Colliders
The two most common colliders used in level design are Box Colliders and Mesh Colliders.
Mesh Colliders
Mesh Colliders are will create a collider that best matches the geometric shape. This will give you the most accurate collider, but adds more triangles which will cost more processing power.
Box Colliders
Box Colliders are more cost efficient, yet effective way to add colliders to your level. A simple, six-sided box will be placed around the object. I will be using box colliders for my level.
Adding Colliders
When add colliders to big areas, such as the floor and walls, you can keep the triangle count down by not adding box colliders to each floor or wall tile, but create one object that covers the entire area.
For the floor, we will create a cube object.
Then, size it to fit the entire floor area and add a box collider. Now, there is one box collider for the floor instead of a box collider for each floor piece.
We do the same thing for the walls.
Once we have all the floor and wall colliders set up, we can get rid of the image of those cube game objects.
The only thing we want is the Box Colliders.
You can easily do this by unselecting the Mesh Renderer, which will remove the game object image, but leave the Box Collider.
Door Colliders
When creating a doorway, you want to make sure the door walls have colliders, but leaving the door way clear.
On the door object, you can add a box collider so the player cannot walk through it. Now, when the door is moved there is no collider there and the player can walk through.
Character Controllers
We are building levels and needs to be tested by placing players in it. We can use Character Controllers to quickly place either a first person or third person type player and cameras into the level.
Unity offers both First Person and Third Person Character Controllers, but you will need Unity 2020.3 or higher.
When using Unity 2020.3, you will need to go to the Unity Asset Store website (no longer going through the Unity Editor). You will add the Character Controller Asset to your Assets.
In the Unity Editor, open up the Package Manager.
You will want to sort through assets by filtering it to My Assets and download and import it into the project.
You may need to Reinstall Dependencies and Reset First Person Controller to place it into the scene. This can be done through Tools > Starter Assets.
Stair Colliders
Now that we have a character controller in the level, we can build out the stair colliders for the player to walk up.
You cannot simply add a Box Collider to the stairs because the collider will be placed around the entire stairs. This will make it impossible for the character controller to walk up.
A Mesh Collider could possibly be used, but again this could have an impact on the performance of the game.
We can add simple cube objects, just like the walls and floors, and rotate it to create a ramp that matches the slope of the stairs. At the top of the stairs, add a landing. Once the ramp is created, test it with the player controller to make sure it works, then remove the Mesh Renderer.
Add collider to other level objects you don’t want your player to move through. Until next time…cheers!