Unity Guide

Creating a Physics based character controller | Unity

A quick guide about how to create a Physics based character controller with Unity

Fernando Alcantara Santana
Nerd For Tech

--

Objective: Create a Physics based character controller without having to use a rigidbody in Unity.

In the last post I talked about the new 2.5D project that I’m starting with Unity. Now, it’s time to create a Physics based character controller for my new game with Unity.

Current scene

To start, let’s take a look at our current scene. We have the next primitive assets:

  • A capsule that represents the player.
  • 5 modified cubes that represent the platforms to jump.
  • 3 spheres that represent collectable items for the player.

Creating the character controller

So, in order to begin developing the game mechanics, let’s create the Physics based character controller that will control the movements of the player. To do it, let’s get rid of the capsule collider from the capsule (as we don’t need it for our purpose) and attach a new Character Controller component through the inspector:

As you can see the Character Controller contains its own collider.

Handling the Physics

Now, in order to handle our own Physics system, let’s create and attach a new script to the player:

Once attached, let’s open the script and create the next private variables:

  • Speed

This variable will store the speed value of the player.

  • Gravity

This variable will store the value of the gravity effect to modify (on each frame) the gravity impulse on the player.

  • Jump height

This variable will store the value of the power of each jump that the player performs.

  • Gravity impulse

This variable will store the gravity impulse to be applied on the player on each frame.

By using [SerializeField] we’re able to modify the private values through the inspector.

Also, create a variable to store a reference to the Character Controller component of the player:

Then, in the Start method, let’s initialize the variable using the GetComponent method and then check if it isn’t null:

Now, to handle the player’s movement, let’s do this in the Update method:

  • Get the horizontal input value by using the GetAxis method.
  • Convert the horizontal input into a Vector3 to represent the direction applied.
  • Multiply the direction applied by the speed variable to obtain the velocity in a Vector3 value.

Then, to handle the Physics, let’s check if the player is touching the ground by using the isGrounded property from the Character Controller component in the player:

If the player is touching the ground let’s check if the space bar is pressed. If that’s the case, let’s assign the jump height power value to the gravity impulse variable:

Otherwise, if the player isn’t touching the ground, let’s subtract the gravity value from the gravity impulse variable:

And now, to apply the respective gravity impulse, let’s set the y value of the velocity (whish is a Vector3) to be the gravity impulse:

Finally, let’s apply the velocity smoothly by multiplying it by the Time.deltaTime value and using the Move method from the Character Controller component of the player:

If you want to know more about the Character Controller component you can check the Unity docs:

Testing the Physics

Now, in order to test the Physics applied to our player, we can modify the respective variable values through the inspector:

If we run the game with Unity, we’ll see that the Physics are applied to the character controller of our player to move across the scene:

And that’s it, we created a Physics based character controller with Unity! :D. I’ll see you in the next post, where I’ll be showing how to implement a double jump to the player with Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana
Nerd For Tech

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).