Home and Learn: Games Programming Course
One thing you will notice when you play your game from our previous lessons is that the zombie enemycan walk right through the player. This is because we have no collision detection. We'll also need a collider object and a rigidbody on the zombie.
We already have a Box Collider on the zombie. But this is attached to his head so we can detect zombie kill shots. We'll need another collider for his body.
Select your zombie on the Hierarchy on the left. In the Inspector on the right, click the Add Component button and add a Capsule Collider. You'll need to tweak the Center Y, the Radius, and the Height. We set ours to these values:
Center Y: 0.75
Radius: 0.4
Height: 1.56
You're looking to have the capsule go from his feet to his neck. Here's our Inspector:
And here's our zombie enemy in Scene view:
Notice the box around his head and the capsule around his body.
Next, we can add a Rigidbody. This is a physics game object and the one that actually stops the zombie. The Capsule Collider is the game object that we can use to detect if one game object collides with another.
With your zombie still selected in the Hierarchy, click the Add Component button in the Inspector again. This time, add a Rigidbody component. You can leave everything on the defaults:
Try it out. You should find that the zombie can no longer walk through the player.
There is another problem, though - the player can walk through the zombie! Try it out. Play your game and walk up to the zombie. If you stop, the zombie can't walk through you. But keep moving forward. You'll find you pass through the zombie.
The solution is to add a Box Collider to the Player.
Highlight your Player object in the Hierarchy:
In the Inspector on the right, click the Add Component button to add a Box Collider to the player. Position your box collider as we did in previous lessons. Play your game again and you should find the problem is now fixed - the player can't walk through the zombie.
In the next lesson below, we'll add a sky box and some fog.
Add a Sky Box to your Unity Games -->
<--Back to the Unity 3D Course Contents Page
Email us: enquiry at homeandlearn.co.uk