Unity Beginner to Hero Part 3: CharacterController and Wall Collision

Welcome back to the third tutorial in this series. In the previous lesson we learned about Vectors and how to poll for input in Unity. At this point, we are able to move around our Capsule in 2-directions in an empty scene. Now, while this may be enough to get you through Steam Greenlight, it’s not quite enough to call it a good game. In this lesson, we will be learning how to add some more objects to the scene and have our Capsule collide with them.

First things first, add a Box to the scene (GameObject->3D Object->Cube). Move it to 0,0,0, and set it’s scale to be 20,1,20. Name it “Floor”. Rename your Capsule to be “Character”, and move him so he’s standing on the floor (0,1.5,0 will do). Add a few more boxes as obstacles and scale and place them appropriately.

Do you ever feel you are a capsule in the land of cubes?

Do you ever feel you are a capsule in the land of cubes?

Press play, and move your character around your little scene. Try to walk him into the boxes, and you’ll notice he slides right through them. Probably not the behaviour you desire.

Clicking on all the objects in the scene, you’ll notice they have some form of Collider component attached to them. For all the basic primitives (Cubes, Spheres, Capsules, and so on), Unity automatically attaches a Collider to them on creation. Colliders are what Unity uses to detect collision between objects. So if all objects concerned have colliders, why isn’t anything happening?

The reason is that Unity won’t actually check for collisions if you just arbitrarily move around an object via it’s Transform (as we have been doing). Instead, it will only check for collisions if you move objects with specific tools. The one we will use today will be the Character Controller.

Our capsule clipping through a box.

Our capsule clipping through a box.

Remove the Capsule Collider from your Character, and add the Character Controller component (although you can navigate through the menus to find this, using the Add Component button is my preferred way). Now that we have the component attached, we need to have our BasicMovement script make use of it. Open the script and replace the transform.position line with the following.

gameObject.GetComponent<CharacterController>().Move(input * speed * Time.deltaTime);

Instead of using transform.position to directly move our character, we’re going to call the CharacterController.Move method to do it for us. Since there’s a fair amount of new stuff in this line, let’s again read it left to right. The first word—gameObject—retrieves the GameObject that our script is attached to (Similar to how we used transform to get the Transform component). Then, we call the method GetComponent. This is used to retrieve a component attached to that object. You can put any component in between the angle brackets. In fact, we could have written our original line of code as:

gameObject.GetComponent<Transform>().position += input * speed * Time.deltaTime;

…But because you tend to use Transforms an awful lot, Unity caches it in the transform property for ease of access.

Righto. Push play and move your character around again. This time, when hitting walls it should be stopped short and no longer move through them. Next lesson we’ll explore adding more controls to the character to rotate his direction and allow more varied movement.

Lesson 3 Complete Project

Thanks for reading! If you have any problems with the above tutorial, general comments or suggestions, please post below. It’s always appreciated!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s