Endless Runner Controller

Complete endless runner character controller with auto-forward movement, lane switching, jump and slide mechanics. Speed increases over time with death and restart handling.

File
EndlessRunnerController.cs
Unity
2022.3+
Level
intermediate
Size
4.8 KB · 220 lines
Added
2026-03-23
License
MIT
View source

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

What it adds to your GameObject

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

Inspector
Script EndlessRunnerController
Movement
Initial Speed 8
Max Speed 25
Speed Increase Rate 0.1
Lane Switch Speed 10
Lanes
Lane Width 2.5
Total Lanes 3
Jump & Slide
Jump Force 10
Gravity -30
Slide Duration 0.6
Slide Collider Height 0.5
Swipe Input
Min Swipe Distance 50
Events
On Jump ()
List is Empty
On Slide ()
List is Empty
On Lane Switch ()
List is Empty
On Death ()
List is Empty
On Restart ()
List is Empty

When to use it

Built for Subway Surfers-style endless runners, hyper-casual auto-run games, and any mobile game with lane-based obstacle avoidance. Use this when you need a complete runner controller with swipe input, progressive difficulty, and collision-based death.

Setup

  1. Attach to your player GameObject and add a CharacterController component
  2. Set lane count and lane width to match your level geometry
  3. Configure jump force, gravity, and slide duration in the inspector
  4. Wire up onJump, onSlide, onDeath, and onRestart events for animations and UI
  5. Call Die() from obstacle collisions and RestartGame() from your retry button

Features

  • Auto-forward movement with speed ramp
  • Lane switching (3 lanes)
  • Jump and slide mechanics
  • Speed increases over time
  • Death and restart handling

Inspector fields

Every field Endless Runner Controller exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Initial SpeedinitialSpeedfloat8Movement
Max SpeedmaxSpeedfloat25Movement
Speed Increase RatespeedIncreaseRatefloat0.1Movement
Lane Switch SpeedlaneSwitchSpeedfloat10Movement
Lane WidthlaneWidthfloat2.5Lanes
Total LanestotalLanesint3Lanes
Jump ForcejumpForcefloat10Jump & Slide
Gravitygravityfloat-30Jump & Slide
Slide DurationslideDurationfloat0.6Jump & Slide
Slide Collider HeightslideColliderHeightfloat0.5Jump & Slide
Min Swipe DistanceminSwipeDistancefloat50Swipe Input
On JumponJumpUnityEventNo listenersEvents
On SlideonSlideUnityEventNo listenersEvents
On Lane SwitchonLaneSwitchUnityEventNo listenersEvents
On DeathonDeathUnityEventNo listenersEvents
On RestartonRestartUnityEventNo listenersEvents

Public API

What other scripts can call on EndlessRunnerController.

MemberReturns
StartRun()void
Restart()void
CurrentSpeed propertyfloat
CurrentLane propertyint
IsAlive propertybool
DistanceRun propertyfloat

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

The laneWidth must match your actual level geometry spacing — if lanes are 3 units apart but laneWidth is 2.5, the player won't align with the track. Obstacles must be tagged "Obstacle" with trigger colliders for OnTriggerEnter to detect them. The slideColliderHeight directly modifies the CharacterController height, so set it to match your slide animation or the player will clip through overhead obstacles.

Source code

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

EndlessRunnerController.cs
C#
using UnityEngine;
using UnityEngine.Events;

[RequireComponent(typeof(CharacterController))]
public class EndlessRunnerController : MonoBehaviour
{
    [Header("Movement")]
    [SerializeField] private float initialSpeed = 8f;
    [SerializeField] private float maxSpeed = 25f;
    [SerializeField] private float speedIncreaseRate = 0.1f;
    [SerializeField] private float laneSwitchSpeed = 10f;

    [Header("Lanes")]
    [SerializeField] private float laneWidth = 2.5f;
    [SerializeField] private int totalLanes = 3;

    [Header("Jump & Slide")]
    [SerializeField] private float jumpForce = 10f;
    [SerializeField] private float gravity = -30f;
    [SerializeField] private float slideDuration = 0.6f;
    [SerializeField] private float slideColliderHeight = 0.5f;

    [Header("Swipe Input")]
    [SerializeField] private float minSwipeDistance = 50f;

    [Header("Events")]
    public UnityEvent onJump;
    public UnityEvent onSlide;
    public UnityEvent onLaneSwitch;
    public UnityEvent onDeath;
    public UnityEvent onRestart;

    public float CurrentSpeed { get; private set; }
    public int CurrentLane { get; private set; }
    public bool IsAlive { get; private set; }
    public float DistanceRun { get; private set; }

    private CharacterController controller;
    private Vector3 moveDirection;
    private int targetLane;
    private float verticalVelocity;
    private float originalColliderHeight;
    private float originalColliderCenterY;
    private float slideTimer;
    private bool isSliding;

    // Swipe tracking
    private Vector2 swipeStart;
    private bool swiping;

    private void Awake()
    {
        controller = GetComponent<CharacterController>();
        originalColliderHeight = controller.height;
        originalColliderCenterY = controller.center.y;
    }

    private void Start()
    {
        StartRun();
    }

    public void StartRun()
    {
        IsAlive = true;
        CurrentSpeed = initialSpeed;
        targetLane = totalLanes / 2;
        CurrentLane = targetLane;
        DistanceRun = 0f;
        verticalVelocity = 0f;
        isSliding = false;
        controller.height = originalColliderHeight;
        controller.center = new Vector3(0f, originalColliderCenterY, 0f);
    }

    private void Update()
    {
        if (!IsAlive) return;

        HandleSwipeInput();
        UpdateMovement();
        UpdateSlide();

        CurrentSpeed = Mathf.Min(CurrentSpeed + speedIncreaseRate * Time.deltaTime, maxSpeed);
        DistanceRun += CurrentSpeed * Time.deltaTime;
    }

    private void HandleSwipeInput()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Began)
            {
                swipeStart = touch.position;
                swiping = true;
            }
            else if (touch.phase == TouchPhase.Ended && swiping)
            {
                swiping = false;
                ProcessSwipe(touch.position - swipeStart);
            }
        }

        // Keyboard fallback
        if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
            SwitchLane(-1);
        if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
            SwitchLane(1);
        if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
            Jump();
        if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
            Slide();
    }

    private void ProcessSwipe(Vector2 delta)
    {
        if (delta.magnitude < minSwipeDistance) return;

        if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
        {
            SwitchLane(delta.x > 0 ? 1 : -1);
        }
        else
        {
            if (delta.y > 0) Jump();
            else Slide();
        }
    }

    private void SwitchLane(int direction)
    {
        targetLane = Mathf.Clamp(targetLane + direction, 0, totalLanes - 1);
        CurrentLane = targetLane;
        onLaneSwitch?.Invoke();
    }

    private void Jump()
    {
        if (!controller.isGrounded) return;

        verticalVelocity = jumpForce;

        if (isSliding) EndSlide();

        onJump?.Invoke();
    }

    private void Slide()
    {
        if (isSliding || !controller.isGrounded) return;

        isSliding = true;
        slideTimer = slideDuration;
        controller.height = slideColliderHeight;
        controller.center = new Vector3(0f, slideColliderHeight * 0.5f, 0f);
        onSlide?.Invoke();
    }

    private void UpdateSlide()
    {
        if (!isSliding) return;

        slideTimer -= Time.deltaTime;
        if (slideTimer <= 0f)
            EndSlide();
    }

    private void EndSlide()
    {
        isSliding = false;
        controller.height = originalColliderHeight;
        controller.center = new Vector3(0f, originalColliderCenterY, 0f);
    }

    private void UpdateMovement()
    {
        // Forward movement
        moveDirection.z = CurrentSpeed;

        // Lane position
        int centerLane = totalLanes / 2;
        float targetX = (targetLane - centerLane) * laneWidth;
        float currentX = Mathf.Lerp(transform.position.x, targetX, Time.deltaTime * laneSwitchSpeed);
        moveDirection.x = (currentX - transform.position.x) / Time.deltaTime;

        // Gravity
        if (controller.isGrounded && verticalVelocity < 0f)
            verticalVelocity = -2f;

        verticalVelocity += gravity * Time.deltaTime;
        moveDirection.y = verticalVelocity;

        controller.Move(moveDirection * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Obstacle"))
        {
            Die();
        }
    }

    private void Die()
    {
        if (!IsAlive) return;
        IsAlive = false;
        CurrentSpeed = 0f;
        onDeath?.Invoke();
    }

    public void Restart()
    {
        transform.position = Vector3.zero;
        StartRun();
        onRestart?.Invoke();
    }
}

Next step

Stack MechanicTap-to-place stacking mechanic: stop the moving block over the last one. Trims the overhang, detects perfect placements and tracks stack height.