3D Player Controller

First/third person character controller with smooth movement, jumping, and slope handling using CharacterController.

File
PlayerController3D.cs
Unity
2022.3+
Level
intermediate
Size
4.1 KB · 76 lines
Added
2026-02-19
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

A Pro version adds more: 3D Player Controller Pro.

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 9 serialized fields with their defaults.

Inspector
Script PlayerController3D
Movement
Walk Speed 6
Sprint Speed 10
Rotation Speed 10
Jumping
Jump Height 1.5
Gravity -20
Ground Check
Ground Check None (Transform)
Ground Distance 0.3
Ground Mask Nothing
Camera
Camera Transform None (Transform)

When to use it

Ideal for third-person action games, RPGs, adventure games, and horror games that need solid ground movement with sprinting. Use this when you want CharacterController-based movement (no Rigidbody physics) with camera-relative input. Works great as the foundation for any 3D game with a controllable character.

Setup

  1. Attach to your player GameObject
  2. Add a CharacterController component
  3. Create an empty child at the feet for Ground Check
  4. Set Ground Mask to your terrain layer
  5. Sprint with Left Shift

Features

  • Camera-relative movement so the player moves where the camera faces
  • Sprint toggle with Left Shift and separate walk/sprint speeds
  • Smooth rotation via LerpAngle for natural turning
  • Configurable gravity and jump height using physics formula
  • Ground detection via Physics.CheckSphere with Gizmo visualization
  • Automatic CharacterController requirement via RequireComponent

Inspector fields

Every field 3D Player Controller exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Walk SpeedwalkSpeedfloat6Movement
Sprint SpeedsprintSpeedfloat10Movement
Rotation SpeedrotationSpeedfloat10Movement
Jump HeightjumpHeightfloat1.5Jumping
Gravitygravityfloat-20Jumping
Ground CheckgroundCheckTransformNoneGround Check
Ground DistancegroundDistancefloat0.3Ground Check
Ground MaskgroundMaskLayerMaskNothingGround Check
Camera TransformcameraTransformTransformNoneCamera

Requirements

  • Requires CharacterController on the same GameObject. Unity adds it automatically when you attach the script.
  • Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.

Common mistakes

Not assigning the cameraTransform field and having no Camera tagged MainCamera will cause a NullReferenceException on startup. The groundCheck Transform must be positioned at the character's feet, not the center — otherwise ground detection fails. Setting gravity to a positive value will launch the character upward instead of pulling them down.

Source code

PlayerController3D.cs, 76 lines, MIT licensed. Paste it into a new C# file of the same name.

PlayerController3D.cs
C#
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class PlayerController3D : MonoBehaviour
{
    [Header("Movement")]
    [SerializeField] private float walkSpeed = 6f;
    [SerializeField] private float sprintSpeed = 10f;
    [SerializeField] private float rotationSpeed = 10f;

    [Header("Jumping")]
    [SerializeField] private float jumpHeight = 1.5f;
    [SerializeField] private float gravity = -20f;

    [Header("Ground Check")]
    [SerializeField] private Transform groundCheck;
    [SerializeField] private float groundDistance = 0.3f;
    [SerializeField] private LayerMask groundMask;

    [Header("Camera")]
    [SerializeField] private Transform cameraTransform;

    private CharacterController controller;
    private Vector3 velocity;
    private bool isGrounded;

    private void Awake()
    {
        controller = GetComponent<CharacterController>();
        if (cameraTransform == null)
            cameraTransform = Camera.main.transform;
    }

    private void Update()
    {
        // Ground check
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0f)
            velocity.y = -2f;

        // Input
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        Vector3 inputDir = new Vector3(h, 0f, v).normalized;

        // Movement relative to camera
        if (inputDir.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(inputDir.x, inputDir.z) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
            float angle = Mathf.LerpAngle(transform.eulerAngles.y, targetAngle, rotationSpeed * Time.deltaTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            float speed = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : walkSpeed;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
        }

        // Jump
        if (Input.GetButtonDown("Jump") && isGrounded)
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);

        // Apply gravity
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }

    private void OnDrawGizmosSelected()
    {
        if (groundCheck != null)
        {
            Gizmos.color = isGrounded ? Color.green : Color.red;
            Gizmos.DrawWireSphere(groundCheck.position, groundDistance);
        }
    }
}

Next step

3D Player Controller ProAdvanced 3D character controller with crouch, slide, ladder climbing, swimming, stamina system, and footstep audio.