Day 76 — Creating a Character Controller in Unity

Connor Fullarton
3 min readMay 30, 2021

--

Hey and welcome!

Now that we’ve got our project set up and the pieces in place it’s time to implement a physics based character controller for our player that will handle movement and gravity.

To get things started, go into the inspector for your player object and add in a Character Controller component.

This component is what we’re going to be referencing and manipulating through code. There’s quite a few handy built in properties we can make use of but we’ll just be using one of them for the time being so create a C# script for your Player and add in this code:

private float _gravity = 2.0f;
private float _speed = 6.0f;
private CharacterController _playerController;
void Start()
{
_playerController = GetComponent<CharacterController>();
}
void Update()
{
float _horizontalInput = Input.GetAxis("Horizontal");
Vector3 direction = new Vector3(_horizontalInput, 0, 0);
Vector3 velocity = direction * _speed;
if (_playerController.isGrounded == true)
{
// Code for jumping
}
else
{
velocity.y -= _gravity;
}
_playerController.Move(velocity * Time.deltaTime);
}

You should be able to recognise most of the code going on here from the 2D space shooter project. We have our usual variables being declared as well as a reference to the Character Controller component that’s getting stored in _playerController.

If you remember Input.GetAxis(“Horizontal”) returns a float value between -1 and 1 in order to determine which key has been pressed and then we can use that value in the next line in order to set the direction that our player will move in.

Something a bit new here though is velocity which we use to determine the speed of the direction we’re heading in, this is important as we use velocity in order to implement gravity as without it we get this issue:

So when we’re assigning the velocity.y to be minus what our _gravity is it would make the Vector3 look like this new Vector3(_horizontalInput, -2.0f, 0) * _speed. The negative value for the y is what causes our player to fall.

Last thing to mention here is that we’re making use of a built in property and a built in method for the Character Controller. The property is isGrounded which lets our code know if the object that this component is attached to is colliding with the ground or not, returning the value as a bool. We make use of this knowledge in order to implement gravity, and later the ability to jump.

The method we use is Move() which does what it says on the tin and moves the Player based on the direction that we pass into it.

With that all done you should have a pretty successful player object that you can move about that’s also affected by gravity:

Not bad eh? That’s three different ways now we’ve implemented movement in our projects here. Next time I’ll go over how to make the player jump!

--

--

Connor Fullarton
Connor Fullarton

Written by Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

No responses yet