You can approach Unity3D character controllers from several angles. If you use a library with powerful classes and functions, creating a physics-based character controller can be a fun place to start your game development.

Step 1: Create a Scene With Player & Terrain Objects

Once Unity is open, and you have created a new project, you can start by adding a couple of objects to your scene. You can create these objects in any order you like, but be careful to make sure that you use the correct settings. While this project is basic, it’s a great way to get started with Unity.

A 3D Plane Object for Terrain

The first object to add to your scene is a plane. Right-click inside the hierarchy pane, hover over 3D Object, and select Plane from the list. You can adjust the size of the plane to provide a larger testing area, but you don’t need to do anything else to it.

A Cube Object Player Model

Next, it’s time to add a cube to the scene to act as a player model. Right-click inside the hierarchy pane, hover over 3D Object, and select Cube from the drop-down menu. Position the new cube so that it sits above the plane you’ve also added. You also need to add a rigidbody component to make the cube into a physics object.

Select the cube and go to the Inspector. Click on Add Component at the bottom of the pane, search for rigidbody, and select Rigidbody from the list when it appears. Tick the Use Gravity tickbox and leave the rest of the settings as they are.

You can find free Unity assets online to spruce your project up.

A Third-Person Camera

Your scene should already have a camera you can use as your third-person camera. Position the camera so that sits in a comfortable third-person position above your cube. Go to the hierarchy pane before dragging and dropping the camera onto the cube to create a parent-child relationship. The camera will automatically follow your cube with this done.

A Quick Test

While your scene doesn’t have any code yet, you can test the work you’ve done so far. Click the Play button at the top of the screen to load your game. You should see the cube fall and land on top of the Plane, and the Camera should follow the cube as it falls.

Step 2: Set Up a C# File

Now it’s time to create a C# file so that you can start programming some movement. Go to the Project section, right-click, hover over Create, and select Folder from the list. Name the folder Scripts or something similar.

Navigate within your new folder and repeat the process, but select C# Script from the list. You can give this file any name, but this will also be the name of the main function within it. Make sure your new C# looks like this.

Step 3: Use C# to Create Forwards & Backwards Motion With Velocity

Before adding to the functions in your script file, declare some public variables. First up is a Rigidbody to store the player model. Also declare a float to keep track of movement speed. These variables go in the main class, outside of any functions.

Next, it’s time to add a single line of code to the Start function. This uses a GetComponent declaration to assign the Cube’s rigidbody component to the rigidbody variable you created.

Now you can add some code that will get your player model moving. This code is nice and simple; you just need two if statements, one for forward movement, and one for moving backward. You can use Input.GetKey to determine when a key is being pressed. In this case, you are looking for presses of the W and S keys.

Pressing either of these keys adds a force to the cube rigidbody using rigidbody.AddForce. You can calculate the direction and speed of the added force by multiplying its Z-axis position (transform.forward) by the speed variable. There is no transform.backward class for you to use for backward motion, but this is achievable by multiplying transform.forward by -1.

Save the C# file and navigate back to Unity. Find the script inside the Project pane and drag and drop it onto the character model Cube inside the hierarchy to assign the script to that object. When you select the Cube you should see the script as a component in the Inspector pane. Click inside the rigidbody variable box and select your cube’s rigidbody.

You can now use the Play button to launch the game and test out your code. But wait! There’s a problem; your Cube is going to roll rather than move straight forwards. Go back to the Cube’s inspector, find the Rigidbody component, and tick the boxes to Freeze Rotation on the X and Z axis before launching the game again. Now it should work.

Step 4: Use C# to Create Left & Right Turning With Torque

Unlike moving backward and forward, turning your Cube player model requires a force called torque. You need another variable for this: a public float to assign a torque value.

The code to turn left and right in this script is very similar to that used to move forward and backward, too. There’s an if statement for each direction that looks for presses of either the D key (right turn) or the A key (left turn).

The first step in each if statement is to determine the direction of the turn using the Input.GetAxis method and assign the result to a float variable. Following this, use rigidbody.AddTorque to apply rotational force to the Cube character model by multiplying the torque, turn, and Z axis of the cube.

Save your code and head back to Unity to test it out. You can adjust the speed and ramp-up time of your player model rotation by selecting the Cube and editing the rigidbody’s mass, drag, and torque variables in your script. This example project settles on 1 mass, 1 drag, and 2 torque.

Step 5: Use C# to Program Jumping

As the final element of this Unity character controller, it’s time to create your jump. Creating a jump is more complex than the other basic controls you’ve worked on, given that a jump has to have a limited height. Start by adding a private boolean variable to track whether or not the player is jumping.

This variable won’t do anything without a statement to test it. In this case, a basic if statement to check if the variable is false will do the trick. Adding the same condition to the other movement if statements will stop the player from moving during jumps.

Adding the same condition to the other movement if statements will stop the player from moving during jumps.

Within the !isJumping if statement, you need another if statement, this time to check for a keypress of the jump button. Setting the isJumping variable to true is the first thing to do in this if statement, followed by making a rigidbody.angularVelocity declaration to remove the forces currently applied to the cube.

Now you can use the rigidbody.AddForce declaration to add force to the cube on its Z axis, creating the upward motion of a jump. Finally, it’s time to use an invoke declaration to call a different function after 0.8 seconds.

This function sets the isJumping variable back to false so that movement/jumping is possible once again, and the 0.8 second delay stops it from triggering too soon.

Step 6: Test Your Character Controller Code

Save your file and head back into Unity to test the code you’ve written. Like most compilers, Unity provides information about errors and other issues with your code to make it easier to see what you might need to change. You can view the full code for this project on our GitHub page.

Creating Unity Character Controllers

You can choose from a variety of different approaches when creating a character controller in Unity. Using physics is one option, but you can also use the built-in character controller system if you want.

Exploring different options like this is a great way to learn about tools like Unity.