Sitemap
Simon Leen

Simon Leen

Developer

Physics Based Character Controller

2 min readAug 17, 2021

Objective: Create a Physics Based Character Controller in Unity

To start we can remove any colliders on the Player as we will add the Character Controller component which brings its own collider. We create a script and attach it to the Player. Inside the script we create a handle by creating a private CharacterController and assigning it in the Start method using GetComponent.

Next we need to get the horizontal input. For that we use a float and assign it to the a horizontal axis input and use the input to determine the direction before moving the Player in that direction. We have now basic movement left and right at 1m/second.

The next part of the controller we need to set up is Velocity. For this we create a private float for speed and then create a new Vector 3 for Velocity which is the direction * speed.

Get Simon Leen’s stories in your inbox

Join Medium for free to get updates from this writer.

We need to create gravity. To do this we can check to see if our Player is grounded(On a platform and not in the air). Start with creating a private float for gravity. Next we need to check inside the update if the Player is grounded using a conditional if statement.

We need to allow the Player to jump between the platforms. To create this logic, we create a private float for jump and then inside the Update we check if a key(space) has been pressed before adding the vertical jump to the velocity.

To just add the jump to the velocity like we did with the gravity would cause a glitching issue where the Player would get a bumpy hopping situation. This is because on the very next frame the direction resets the y in velocity back to 0.

To fix this issue we need to use a cached variable for the Y velocity. We then use that variable to take our jump and gravity values before assigning the velocity.y.

Press enter or click to view image in full size

In the next post we will allow the Player to use double jump mechanics.

--

--