To stop your player from being able to walk through objects in the scene, you
need to use colliders. Colliders are components that allow us to define a shape or area for processing collision events. This includes physically preventing a player from walking through walls and other game objects.
Adding a Collider
Adding a collider is fairly simple. Select the object you wish to add the collider to from your hierarchy, and on the inspector, select Add Component. From here, you can select the type of collider you wish to add, or type "collider" to view all of the collider components presently available to you.
Choosing the right Collider
There are many possible collider types to choose from, so which one should you choose? First and foremost, since you are creating a 3D scene, you should not be using any Collider 2D component. Physics works differently for 2D and 3D, so 2D colliders will only interact with other 2D colliders, and by default will only respond to 2D physics events. The same goes for 3D colliders and 3D physics events.
Of the remaining options, there are five particular colliders that stand out:
- Box Collider: A simple box without further detail. A great option for your average building.
- Sphere Collider: A simple sphere without further detail. A great option for spherical objects, such as balls or character heads.
- Capsule Collider: A simple capsule, with rounded faces. A great option for basic player colliders and poles.
- Mesh Collider: A collider derived from a 3D model. By default, this will automatically link to the model you are currently using on the game object. While this is the way to go for more complex models where basic shapes simply won't cut it, you would normally make a specific collider object from your 3D model, by scaling down its polygon count. Remember, the more polygons, the more complex the collider.
- Terrain Collider: A collider derived from a terrain object. You should not be specifically creating one of these, as they only provide collision for the Unity terrain object, and are loaded in by default. If you made the ground using terrain editing, in Unity, you probably already have one of these in your project.
Choosing between them depends on the object you are placing the collider on, how detailed you want your collision to be, and how efficient you want your collider to be. Consider the detail in your game objects. As an example, consider a brick wall. In the real world, you could feel the detail on the bricks, and in indent of the mortar between each brick. In a 3D game, this detail is unlikely to be noticed, apart from your game potentially stalling to calculate collision due to the far more complex collider. In this case, a simple box would suffice, as the player would still react to the basic shape in practically the same way.
Choosing the objects to place the colliders
For the purpose of simply preventing the player from moving into objects, its important to note that it really does not matter what game object you place the collider on. For organisational sake, it is probably best to simply add the collider to the object the collider represents. In the case where you have a collider spanning multiple game objects, these game objects should likely be grouped under a parent game object, with the parent game object holding the collider.
If you ever want to have any other reaction to collision, such as having the player take damage or triggering a door to open, you will want the collider to be on the same game object as the script in charge of its reaction. This is because colliders automatically call a number of
MonoBehaviour functions, such as
OnCollisionStay(Collision), which greatly help you perform your collision logic. You do not need to set any of this up for simple 'prevent the player from walking through this' collision.
To get everything working, there are a few tiny details you need to ensure.
First of all, and it should go without saying, colliders only react to other colliders by default. Your player also needs a collider, in order to respond to attempts to move into the space of another collider. With greater complexity and character animation, this would normally be made up of a series of colliders, to represent each part the player can move. For something more simple, you should typically use a Capsule Collider.
For collision to work, one of the objects involved in the collision must have a rigidbody. This is a very important part; if two objects collider, and neither have a rigidbody, they will still pass through each other. This is also the step most commonly missed with beginner collision detection in Unity. Typically, your player game object should already have a rigidbody. If it does not, you can add one in the same way you add the collider, just make sure to add Rigidbody and not Rigidbody 2D. Rigidbodys also provide physics, using the Unity physics engine. It is important to know this, as your object will respond to physics differently, with a rigidbody attached. While this will not affect direct transform translation, you may want to ensure that the component shows
Use Gravity = false. Once things are up and running, have a play around with this component, and observe how it affects the way your character moves.
As a last matter of importance, you need to ensure that your collider shows
Is Trigger = false. Colliders set to act as a trigger will not respond to normal physics-based collision. These colliders will freely allow other objects to move through them. As an example of when you might use one of these, you might have an area the player can move through that triggers an event when the player reaches it. A trigger collider would detect that the player has reached the location, while still allowing the player to pass through.
You can read up on colliders in the Unity Manual. There is also a good Unity tutorial on colliders that will show you the basics. If you are more experienced with the programming side of Unity, you might also want to read through the API entries, including Collider which acts as a parent class for all 3D colliders.