gitmyhub

CharacterController

C# ★ 416 updated 14d ago

A character controller for Unity that covers many edge cases.

A drop-in replacement for Unity's built-in character controller that fixes physics timing and slope/stair movement issues.

C#UnityRigidbodysetup: easycomplexity 3/5

This repository contains a character controller for Unity, the popular game development platform. A character controller is the code that handles how a player-controlled character moves through a game world, including how it reacts to collisions, slopes, and steps. Unity has a built-in character controller, but it does not handle all situations correctly, and this project exists to fill those gaps.

One key difference is how physics updates are timed. In a standard Unity game, physics runs on a fixed schedule that does not always align with how fast the game is rendering frames. This can cause visual inconsistencies, especially when the game slows down and physics has to run multiple times per frame to catch up. This project includes a Physics Manager script that instead runs physics once per frame using the actual time that passed since the last frame, keeping movement visually consistent.

The controller also runs its corrections inside the regular Update loop rather than the FixedUpdate loop, which is the more common choice. The reason for this is that corrections applied in FixedUpdate happen before the physics engine resolves them, leaving the raw unfiltered output visible on screen. By running corrections afterward, the controller can apply things like stepping up over small obstacles and matching motion to moving platforms before the frame is drawn.

Understanding the physics engine, the controller is built on a Rigidbody, which is Unity's standard physics-simulated object type. This means the character will push and be pushed by other physics objects in the expected way.

For slope and stair detection, the controller uses a pattern of raycasts, which are invisible lines cast downward to measure ground height at multiple points. By averaging those measurements, it treats stair steps as a smooth slope rather than a sharp edge, allowing the character to walk over them without catching. The project is licensed under MIT.

Where it fits