Day 85: Creating a Physics Based Character Controller in Unity

Tyler Smallwood
3 min readSep 10, 2021

Objective: Create the logic for a Physics Based character.

Today we will get our player moving and jumping. Unity has made controlling your character a little bit easier with the use of the CharacterController component.

The first thing we need to do is add the CharacterController to the player object. If you already have a collider you can remove because the CharacterController adds one it’s self.

Now we can move into the Player script. Inside we will need to get a reference to the CharacterController component.

Now in our Movement function we will need to get our input from the player, since this is a 2.5D we really only need to get the X axis. We won’t be using the Z axis and the Y axis will be for jumping. We will need to add the input into a vector3 for direction. Finally, we will need to get our velocity, which can be done by multiplying our direction by our speed.

Now we can use the CharacterController by calling the Move function that takes in a vector3. We give it the velocity and multiply it by Time.deltaTime.

Now our player can move!

Next on the list is to get the player jumping! For physics to work we will need to introduce gravity, so we have a float for gravity. We will also need a variable to calculate the jump height.

Before using the variables, we need to check to see if we are grounded before jumping. Another useful item from the CharacterController is that it can return a bool if the player is grounded. All you need to do is call _charController.isGrounded.

Now we will need to check for player input. If the player hits space we will jump. Once we script knows the player hit space we will cache the jump height into a variable that holds the jump height. This will help with the jitter a little. If the player is not grounded we will take subtract the cached jump height by the gravity.

Last thing to do for the jump is assign the velocity.y to the cached velocity.

Now our player can jump, the only thing left is to find the right values to make the jumping feel right.

That’s it. Thank you for reading, have a wonderful day!!

--

--

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer