TExcelTAD
Home › Unity with C# › CharacterController

CharacterController

Players are not crates. A crate should tumble when hit; a player should stand upright, stop instantly, climb steps and never slide down a gentle slope. That's hard to get from a physics-driven Rigidbody, so Unity offers the CharacterController component: a capsule you move kinematically with Move(). It collides and slides along walls, climbs steps up to Step Offset, refuses slopes steeper than Slope Limit, and reports isGrounded. Gravity, jumping and momentum are your job.

CharacterController Rigidbody character
Control exact, snappy physical, can feel floaty or sliding
Gravity / jumping you write it built in
Pushing objects manual (OnControllerColliderHit) automatic
Moving platforms manual (parenting or adding platform velocity) friction-based, often fiddly
Typical games FPS, third-person action, platformers physics-heavy games, vehicles, ball games

A complete controller

csharp
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(CharacterController))]
public class ThirdPersonMover : MonoBehaviour
{
    [SerializeField] Transform cameraTransform;
    [SerializeField] float speed = 6f, jumpHeight = 1.4f, gravity = -25f;
    [SerializeField] float coyoteTime = 0.12f, jumpBuffer = 0.15f;

    CharacterController controller;
    InputAction move, jump;
    float verticalVelocity, lastGroundedTime = -10f, lastJumpPressTime = -10f;

    void Awake()
    {
        controller = GetComponent<CharacterController>();
        move = InputSystem.actions.FindAction("Move");
        jump = InputSystem.actions.FindAction("Jump");
    }

    void Update()                                     // CharacterController moves in Update, not FixedUpdate
    {
        // Camera-relative direction on the ground plane.
        Vector2 input = move.ReadValue<Vector2>();
        Vector3 forward = Vector3.ProjectOnPlane(cameraTransform.forward, Vector3.up).normalized;
        Vector3 right = Vector3.ProjectOnPlane(cameraTransform.right, Vector3.up).normalized;
        Vector3 horizontal = (forward * input.y + right * input.x) * speed;

        if (controller.isGrounded)
        {
            lastGroundedTime = Time.time;
            if (verticalVelocity < 0f) verticalVelocity = -2f;   // keep a small push so isGrounded stays true
        }
        if (jump.WasPressedThisFrame()) lastJumpPressTime = Time.time;

        bool canJump = Time.time - lastGroundedTime <= coyoteTime;
        bool wantsJump = Time.time - lastJumpPressTime <= jumpBuffer;
        if (canJump && wantsJump)
        {
            verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * gravity);   // v = sqrt(2gh)
            lastGroundedTime = lastJumpPressTime = -10f;
        }

        verticalVelocity += gravity * Time.deltaTime;
        controller.Move((horizontal + Vector3.up * verticalVelocity) * Time.deltaTime);   // one Move per frame

        if (horizontal.sqrMagnitude > 0.01f)
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(horizontal), 12f * Time.deltaTime);
    }

    void OnControllerColliderHit(ControllerColliderHit hit)   // push light Rigidbodies
    {
        Rigidbody body = hit.rigidbody;
        if (body != null && !body.isKinematic && hit.moveDirection.y > -0.3f)
            body.AddForce(hit.moveDirection * 2f, ForceMode.Impulse);
    }
}

isGrounded reflects the last Move call: it is true only if that move collided below. Call Move exactly once per frame with the combined motion, or isGrounded will flicker.

Coyote time and jump buffering

Two tricks make jumping feel fair, and players notice when they're missing:

  • Coyote time: a jump still works for a short moment after walking off a ledge.
  • Jump buffer: a jump pressed shortly before landing is remembered and performed on landing.

The model below runs a scripted sequence at 60 FPS with both features on and off. The player walks off a ledge at frame 6 and presses jump at frame 8 (late); later they're in the air and press jump at frame 40, just before landing at frame 43 (early):

csharp · try it yourself
using System;

foreach (bool assists in new[] { false, true })
{
    var log = Simulate(assists);
    Console.WriteLine(
quot;{(assists ? "with assists " : "without assists")}: {log}"); } static string Simulate(bool assists) { const double Dt = 1.0 / 60; double coyote = assists ? 0.12 : 0, buffer = assists ? 0.15 : 0; double lastGrounded = -1, lastPress = -1; string log = ""; for (int frame = 0; frame <= 50; frame++) { double t = frame * Dt; bool grounded = frame < 6 || frame >= 43; // off the ledge at 6, lands at 43 if (grounded) lastGrounded = t; if (frame == 8 || frame == 40) lastPress = t; // late press, early press bool canJump = t - lastGrounded <= coyote + 1e-9; bool wantsJump = t - lastPress <= buffer + 1e-9; if (canJump && wantsJump) { log +=
quot;jump@{frame} "; lastGrounded = lastPress = -1; } } return log.Length == 0 ? "no jumps" : log.Trim(); }
without assists: no jumps
with assists   : jump@8 jump@43

Without assists, both presses are "wrong" by a few frames and the player blames the game. With 0.12 s of coyote time and a 0.15 s buffer, both jumps happen. (In the real game the late jump would change the landing frame; the model keeps the script fixed to show the decision only.)

Moving platforms

A CharacterController doesn't inherit a platform's motion. Two common fixes: parent the player to the platform while grounded on it (unparent on leaving), or measure the platform's movement since the last frame and add it to the Move call. For physics-heavy games, consider a Rigidbody-based controller or an Asset Store character controller package instead.

Test yourself

What does CharacterController.isGrounded report?

  1. Whether a raycast below hits ground
  2. Whether the last Move call collided below
  3. Whether gravity is enabled
  4. Whether the capsule touches any collider

What is 'coyote time'?

  1. A cooldown between jumps
  2. A short window where a jump still works after leaving a ledge
  3. Slow motion during a jump
  4. The time before respawn

Which jump velocity reaches a height h with gravity g (negative)?

  1. h * g
  2. Mathf.Sqrt(h * -2f * g)
  3. h / g
  4. 2 * h