Physics based character controller in Unity

Daniel Kirwan
3 min readJul 19, 2021

--

Objective: Create a basic character controller system from scratch

First, I made sure that I had created a game object for the player. Once I had my capsule created a then remove the capsule collider and replaced it with a different component called Character controller.

This controller will not be using rigidbody to move around. I decide on this because I want to be in control of all aspects of the controller including the physics. I created a Player script an attached it to the player in Unity.

I need a few things to get the player moving around and that is speed, gravity and the jumpheight I want for the player. I have made these serialised fields so that I or a designer can edit them in the editor without changing the code.

There is one more thing I need to add and that is the velocity for the yaxis.

In this script called Player.cs I have created a new method called PlayerMovement and I am calling this inside the update method.

There are a few things that need to be setup in this method that will allow the player to move. The CharacterController class already has some methods that I can use for moving my player around and even has a handy isGrounded bool that let’s you know if the player is on the ground. Particularly useful if you need to know if the player can jump.

To find the class and its methods, create a reference to a character controller in a script and see the video below.

Above I am setting up the variables that I will need for using the character controller Move method, which is used at the end of PlayerMovement method.

Next I am checking whether the bool for isGround is true, inside of that check I am checking if the space key has been pressed, if it has then the player is allowed to jump. This is the simple set up for a single jump, I will be discussing in another article how to make this a double jump.

I am then setting the y axis of the velocity variable to the _yVelocity variable. As this stops velocity variable from being overridden every frame.

Now that the method is set up, I now need to adjust the variables in the Unity editor. You can use the same settings as me or adjust them for your individiual game.

And that is it for the initial set up of a physics based character controller in Unity. Come back for more articles.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet