A comprehensive and flexible character controller that provides smooth, responsive movement with support for multiple movement types and interactions.
- Smooth movement with acceleration/deceleration
- First-person camera controls
- Jumping mechanics
- Crouching system with camera adjustment
- Sprint functionality
- Air control for mid-jump movement
- Slope handling and ground detection
- Fully customizable through Unity Inspector
- Built-in movement smoothing
- Ground normal projection for realistic slope movement
- Debug visualization for ground checking
- Create a new script in your Unity project called
CharacterController.cs - Copy the entire script content into this file
- Create a player object in your scene with the following hierarchy:
PlayerObject └── Main Camera
-
Add required components to your player object:
- The CharacterController script will automatically add a Rigidbody
- Add a Capsule Collider or custom collider
- Ensure you have a camera as a child object
-
Configure the ground layer:
- Create a layer for ground objects (e.g., "Ground")
- Set the ground objects to use this layer
- In the CharacterController component, set the Ground Mask to include this layer
-
Configure the controller in the Unity Inspector:
Move Speed: Base movement speed (default: 6)Sprint Multiplier: Speed multiplier when sprinting (default: 1.5)Crouch Multiplier: Speed multiplier when crouching (default: 0.5)Jump Force: Force applied when jumping (default: 5)Air Control: Movement control multiplier while in air (default: 0.3)
Ground Mask: Layer mask for ground detectionGround Check Distance: How far to check for ground (default: 0.2)Slope Limit: Maximum angle for walkable slopes (default: 45)
Rotation Speed: How quickly the player turns (default: 10)Movement Smoothing: Smoothing factor for acceleration/deceleration (default: 0.1)
Player Camera: Reference to the camera transformMouse Sensitivity: Mouse look sensitivity (default: 2)Max Look Angle: Maximum up/down look angle (default: 80)
WASD: MovementMouse: Look aroundSpace: JumpC: Toggle crouchLeft Shift: Sprint
// Example of setting up the controller in code
public class GameManager : MonoBehaviour
{
public CharacterController playerController;
void Start()
{
// Customize controller settings
playerController.SetMouseSensitivity(2.5f);
playerController.SetMoveSpeed(8f);
}
}// Enable/disable movement
controller.SetMovementEnabled(bool enabled);
// Adjust mouse sensitivity
controller.SetMouseSensitivity(float sensitivity);
// Change movement speed
controller.SetMoveSpeed(float speed);
// Check player states
bool isGrounded = controller.IsGrounded();
bool isCrouching = controller.IsCrouching();
bool isSprinting = controller.IsSprinting();- Set the Rigidbody's Collision Detection to Continuous
- Adjust the player's mass and drag values for desired feel
- Use physics materials to control friction
- Adjust the ground check distance based on your game's scale
- Use appropriate layer masks for ground checking
- Consider the performance impact of movement smoothing
- Position the camera at the character's head level
- Adjust Field of View (FOV) for desired perspective
- Consider adding camera bob or effects for more immersion
- Check the ground normal angle calculation
- Adjust the slope limit
- Verify physics material friction settings
- Increase the movement smoothing value
- Check for framerate issues
- Verify rigidbody interpolation settings
- Adjust the camera's near clip plane
- Check for colliders between the camera and player
- Verify camera position during crouching
The controller can be extended with additional features such as:
- Footstep system
- Animation integration
- Wall running
- Sliding mechanics
- Climbing system
- Swimming capabilities
- Advanced state machine