Creating a Physics Based Character Controller in Unity

James Lafritz
Dev Genius
Published in
5 min readJun 8, 2021

--

I Could use the Rigidbody on the Character and use rigidbody physics. However I want complete control over all of the physics interactions so I am going to be using a Character Controller.

According to the Unity Manual Character Controller

The Character Controller is mainly used for third-person or first-person player control that does not make use of Rigidbody physics.

And the Scripting API

A CharacterController allows you to easily do movement constrained by collisions without having to deal with a rigidbody.

A CharacterController is not affected by forces and will only move when you call the Move function. It will then carry out the movement but be constrained by collisions.

This is exactly what I want. I want something that I can use to move around my scene and restrict it’s movement based on collisions.

I removed the Rigidbody from my Player and added a Character Controller

Now I need a script to control my player.

Player Movement

Since this is a Platformer I will be moving the character horizontally, I will be applying my own Gravity and allowing the player to Jump. I need a reference to the Character Controller that is attached to the Player. since I will be using the character controller for movement I made it a Required Component, which will add the Character Controller to a Game Object When I add the Player to a Game Object, this will also prevent the Character Controller from being removed from the Game Object. I also do not have to add any error checking in my Behavior as I am guaranteeing that the Character Controller will be there.

Moving Horizontally

I cache the Horizontal input. Then I set the x axis of the move direction to the horizontal movement multiplied by the movement speed. Lastly I tell the Character Controller to move using CharacterControler.Move

I can now move left and right, notice that when I hit the platform It stops me from moving.

Applying Velocity

Next I changed my movement to direction and velocity. This is so I can add in Gravity and Jumping later. I removed the horizontal move speed from the direction. I added a Vector3 for the Velocity. I then set the velocity x to the move direction multiplied by the horizontal move speed. I then tell the character controller to move by the velocity.

Applying Gravity

For gravity I have a choice I can adda value to use for the amount of gravity to use or I can use the value supplied by Unity’s Physics System. I can modify this value by going to Edit->Project Settings then Selecting the Physics and there is a value for the Gravity. There is also one for 2D Physics but I will be Using 3D Physics.

I am going to add a value to determine if I want to use gravity of not.

In the Update Code I cache the Character Controller is Grounded. I then check if it is grounded, this is where I will be adding the addition of being able to jump for now I will do nothing. If I am not grounded then I check to see if I am using gravity, if i am i add Physics Gravity time delta time to my velocity.

Now the player falls to the platform.

Allowing the Player to Jump

I need to know how high the player can jump. In the Update Method if the Player is Grounded I check to see if the Jump Button is pressed (Edit->Project Settings->Input Manager Tab->Axis->Jump). If the Jump Button was pressed I set the velocity.y to the jump height.

I can Now perform a Jump as long as the player is standing on a platform.

I am using POLYGON Prototype — Low Poly 3D Art by Synty from Unity’s Asset store. I can achieve a similar look using the Free POLYGON Starter Pack — Low Poly 3D Art by Synty. Since I am currently in the Prototyping stage using these Assets just gives me a little better look then using Unity’s built in primitives.

To get the new look I turned of all of the Mesh Renders of the Objects that I added in the scene and then added the different prefabs provided by the assets to my Game Objects.

--

--

Excited about changing my hobby into a new carer with GameDevHQ course.