Creating a Physics Based Character Controller in Unity

Marcus Ansley
2 min readJun 27, 2021

I’ve recently made a start on this 2.5D platformer, and have been mainly focused on the Player script (movement and controls in particular). I’m currently using Unity’s ‘CharacterController’ component in addition to the Player script to control their movement. Since the ChararacterController detects collisions, I’m not using this in addition to a Collider component, and will be keeping the player grounded through code rather than through the use of a RigidBody.

I’ll include the current code for the Player script as it currently stands, then run through it:

Within the Update function, I’m storing the value for the player’s ‘Horizontal’ input (whether that’s with a joystick or with directional keys) and am using that to define the X axis in a direction Vector3. I’m then defining and assigning their velocity to their direction multiplied by a private _speed float. Next, I’m using a reference to the CharacterController component (‘_controller’) to tell whether the player is currently grounded. If they are, I’m allowing them to press a ‘Jump’ button (currently, the spacebar or ‘A’ button on a controller) which will then set a private _yVelocity float’s value to a pre-defined jump height. Editing their velocity.y value directly and immediately has had some pretty funky results, as their velocity.y is returned to normal immediately in the next iteration of the Update method.

If the player isn’t currently grounded, however, I’m using this _gravity float to pull them back down rapidly (until eventually, presumably, they’re grounded again).

Lastly, I’m calling the CharacterController.Move() method, passing in their velocity and Time.deltaTime to prevent them moving too quickly.

With all of this in place, we should have the core logic needed to control our character.

--

--

Marcus Ansley

Game Developer | Game Design and Literature Graduate