In the last chapter, Chapter 3, “Components,” you learned how to make the character move by adding a Rigidbody, adding colliders and writing a script to make your character move. This was great for learning purposes, but there’s a simpler way provided by Unity out of the box: The character controller.
A character controller is a built-in component that makes it easier to make characters in your game move based on user input. It combines all the components that you added manually last chapter into one, and provides a simple API to use it.
Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.
Adding a character controller
Sorry to do this to you after all that work in Chapter 3, but you need to strip the marine of his Rigidbody. Select the SpaceMarine GameObject in the Hierarchy, and, in the Inspector, click the Rigidbody’s component settings (three dots) and select Remove Component.

Click the Add Component button, go to the Physics category and choose Character Controller.

You’ll notice a bunch of fields that influence the character’s movement.
Set Center to (-1.82, 6.53, 0.7), Radius to 2.5 and Height to 11.08. These properties affect the size of the collider.

In the Scene view, you’ll see a fine capsule around the marine.

Your marine is almost ready.
Updating your code
Now that you’ve added a character controller, you need to make a few tweaks to your PlayerController script. Start by double-clicking the PlayerController script to open it in your editor.
First, you need to get a reference to the CharacterController
. You could get this in Update()
, but it’s best to store it in an instance variable during Start()
. That way, the game doesn’t spend unnecessary time fetching components.
Add the following beneath the moveSpeed
variable declaration:
private CharacterController characterController;
This creates an instance variable to store the CharacterController
.
In Start()
, add the following to get a reference to the component:
characterController = GetComponent<CharacterController>();
GetComponent()
gets a reference to the current component passed into the script. Replace all the code in Update()
with the following:
Vector2 moveInput = move.ReadValue<Vector2>();
Vector3 moveDirection = new Vector3(moveInput.x, 0, moveInput.y);
characterController.SimpleMove(moveDirection * moveSpeed);
The first line gets the current input from the input system. The next line creates a Vector3
from the information. Finally, you call the SimpleMove()
method multiplying by the speed. SimpleMove()
is a built-in method that automatically moves the character in the given direction, but not allowing the character to move through obstacles.
Adding additional colliders
With the character controller in place, you now get collisions in your game. Play the game and run to the nearest wall. You’ll find that you can’t walk through it.

Unfortunately, you still have the problem of a character who runs through columns, which is easy to fix with colliders. Go ahead and make the fix now.
Stop your game. In the Project Browser, select the BobbleArena-Column in the Prefab’s folder then click the Add Component button. From the Physics category, select the Box Collider. In the Inspector, set Center to (0, 2.66, 0) and Size to (2.07, 5.25, 2).

Once you make changes to the column, your next thought is probably that you need to apply those changes to the rest of the prefabs. You don’t have to because you’ve modified the source prefab. Those changes are added automatically to all the instances!
Now play your game and charge at a column. You’re now blocked!

Where to go from here
Character controllers are useful to simplify your code to control your character. It’s common to see all sorts of custom controllers. You’ll continue to use the Unity supplied controller for the rest of the tutorial series.
At this point, you have many objects in the playfield, and you have even more once you start firing your gun. Your next task is to actually destroy some objects in code. You’ll do this in the next tutorial.
Discover more from Jezner Blog
Subscribe to get the latest posts sent to your email.