A Physics Based Character Controller for Unity — Part 1 Basic Movement

Andrea Zilio
2 min readJul 10, 2021

For fun we are going to write our character controller for a 2.5D Platform Game from scratch.

Character controller and script for the player

To get started we need to add a character controller component to our player object and remove the capsule collider. With the character controller we get a basic collider without physics and therefore can control it manually. We then create a player script and attach it to the player object.

Player Inspector with added character controller and player script

In player script, we add a reference to the character controller.

Horizontal Movement

Now we can implement the basic horizontal movement.

//get the horizontal movements
//get the direction based on input
//move in this direction

Here is how the code looks:

And yes it works :).

+

Velocity and Gravity

We need to add two more things before we can teach the player to jump: velocity and gravity. Velocity is speed * direction, that’s easy enough. But what about gravity. Normally it is simply a setting we apply to physics in Unity. But when we code it we need a variable

private float _gravity = 1.0f;

Then we can simply check if the character is grounded and if he is not, reduce the y value of the velocity vector by the value of the gravity variable.

if(_controller.isGrounded == true)
{
//do nothing for now
}
else
{
velocity.y -= _gravity;
}

And again it works.

In the next article I will be adding a single and double jump, so that we can get to the next platform.

--

--

Andrea Zilio

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.