Endless Runner Tutorial in Unity 3D

NSDG | Jan 27, 2020 | 0 Comments
171

In video games, no matter how large the world is, it always has an end. But there are some games that try to emulate the infinite world, such games fall under a category called Endless Runner.

Endless Runners are mostly popular on mobile platforms and characterized by a player constantly moving forward while trying to avoid the obstacles. Examples of such games are: Temple Run (Mobile), Temple Run 2 (Mobile), Subway Surfers (Mobile).

Subway Surfers Gameplay

However, considering that even modern computers/gaming devices have a limited processing power, it's impossible to make a truly infinite worlds. So how do the games above create an illusion of an Infinite World? The answer is by reusing the building blocks (a.k.a. Object Pooling).

Basically as soon as the block goes outside or behind the Camera view, it's moved to the front, and so on.

In this tutorial we will be focusing on the following things:

  • Making a platform with multiple obstacle variations that will be re-used and looped seamlessly
  • Creating a simple player controller with obstacle collision detection
  • Simple game logic

So let's begin!

Part 1: Creating a Platform

First we begin by creating a tiled platform prefab:

  • Create new GameObject and call it "TilePrefab"
  • Create new Cube (GameObject -> 3D Object -> Cube)
  • Move the Cube inside "TilePrefab" object, change its position to (0, 0, 0) and scale to (8, 0.4, 20)

  • Optionally you can add Rails to the sides by creating additional Cubes, like this:

Now for the obstacles, I will have 3 obstacle variations, but you can make as many as needed:

  • Create 3 GameObjects inside "TilePrefab" object and name them "Obstacle1", "Obstacle2" and "Obstacle3"
  • For the first obstacle, create new Cube and move it inside "Obstacle1" object
  • Scale new Cube to around the same width as platform and scale its height down (The player will need to jump to avoid this obstacle)
  • Create new Material, name it "RedMaterial" and change its color to Red, then assign it to the Cube (this is just so the obstacle is distinguished from the main platform)

  • For the "Obstacle2" create a couple of cubes and place them in triangular shape, leaving one open space at the bottom (The player will need to crouch to avoid this obstacle)

  • And lastly the "Obstacle3" will be a duplicate of "Obstacle1" and "Obstacle2"

  • Now select all the Objects inside Obstacles and change their tag to "Finish", this will be needed later to detect the collision between Player and Obstacle.

To generate an infinite platform we will need a couple of scripts that will handle Object Pooling and Obstacle activation:

  • Create new script, call it "SC_PlatformTile" and paste the code below inside it:

SC_PlatformTile.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SC_PlatformTile : MonoBehaviour
{
    public Transform startPoint;
    public Transform endPoint;
    public GameObject[] obstacles; //Objects that contains different obstacle types which will be randomly activated

    public void ActivateRandomObstacle()
    {
        DeactivateAllObstacles();

        System.Random random = new System.Random();
        int randomNumber = random.Next(0, obstacles.Length);
        obstacles[randomNumber].SetActive(true);
    }

    public void DeactivateAllObstacles()
    {
        for (int i = 0; i < obstacles.Length; i++)
        {
            obstacles[i].SetActive(false);
        }
    }
}
  • Create new script, call it "SC_GroundGenerator" and paste the code below inside it:

SC_GroundGenerator.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SC_GroundGenerator : MonoBehaviour
{
    public Camera mainCamera;
    public Transform startPoint; //Point from where ground tiles will start
    public SC_PlatformTile tilePrefab;
    public float movingSpeed = 12;
    public int tilesToPreSpawn = 15; //How many tiles should be pre-spawned
    public int tilesWithoutObstacles = 3; //How many tiles at the beginning should not have obstacles, good for warm-up

    List<SC_PlatformTile> spawnedTiles = new List<SC_PlatformTile>();
    int nextTileToActivate = -1;
    [HideInInspector]
    public bool gameOver = false;
    static bool gameStarted = false;
    float score = 0;

    public static SC_GroundGenerator instance;

    // Start is called before the first frame update
    void Start()
    {
        instance = this;

        Vector3 spawnPosition = startPoint.position;
        int tilesWithNoObstaclesTmp = tilesWithoutObstacles;
        for (int i = 0; i < tilesToPreSpawn; i++)
        {
            spawnPosition -= tilePrefab.startPoint.localPosition;
            SC_PlatformTile spawnedTile = Instantiate(tilePrefab, spawnPosition, Quaternion.identity) as SC_PlatformTile;
            if(tilesWithNoObstaclesTmp > 0)
            {
                spawnedTile.DeactivateAllObstacles();
                tilesWithNoObstaclesTmp--;
            }
            else
            {
                spawnedTile.ActivateRandomObstacle();
            }
            
            spawnPosition = spawnedTile.endPoint.position;
            spawnedTile.transform.SetParent(transform);
            spawnedTiles.Add(spawnedTile);
        }
    }

    // Update is called once per frame
    void Update()
    {
        // Move the object upward in world space x unit/second.
        //Increase speed the higher score we get
        if (!gameOver && gameStarted)
        {
            transform.Translate(-spawnedTiles[0].transform.forward * Time.deltaTime * (movingSpeed + (score/500)), Space.World);
            score += Time.deltaTime * movingSpeed;
        }

        if (mainCamera.WorldToViewportPoint(spawnedTiles[0].endPoint.position).z < 0)
        {
            //Move the tile to the front if it's behind the Camera
            SC_PlatformTile tileTmp = spawnedTiles[0];
            spawnedTiles.RemoveAt(0);
            tileTmp.transform.position = spawnedTiles[spawnedTiles.Count - 1].endPoint.position - tileTmp.startPoint.localPosition;
            tileTmp.ActivateRandomObstacle();
            spawnedTiles.Add(tileTmp);
        }

        if (gameOver || !gameStarted)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                if (gameOver)
                {
                    //Restart current scene
                    Scene scene = SceneManager.GetActiveScene();
                    SceneManager.LoadScene(scene.name);
                }
                else
                {
                    //Start the game
                    gameStarted = true;
                }
            }
        }
    }

    void OnGUI()
    {
        if (gameOver)
        {
            GUI.color = Color.red;
            GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200), "Game Over\nYour score is: " + ((int)score) + "\nPress 'Space' to restart");
        }
        else
        {
            if (!gameStarted)
            {
                GUI.color = Color.red;
                GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200), "Press 'Space' to start");
            }
        }


        GUI.color = Color.green;
        GUI.Label(new Rect(5, 5, 200, 25), "Score: " + ((int)score));
    }
}
  • Attach SC_PlatformTile script to "TilePrefab" object
  • Assign "Obstacle1", "Obstacle2" and "Obstacle3" object to Obstacles array

For the Start Point and End Point we need to create 2 GameObjects that should be placed at the start and the end of the platform respectively:

  • Assign Start Point and End Point variables in SC_PlatformTile

  • Save "TilePrefab" object to Prefab and remove it from the Scene
  • Create new GameObject and call it "_GroundGenerator"
  • Attach SC_GroundGenerator script to "_GroundGenerator" object
  • Change Main Camera position to (10, 1, -9) and change its rotation to (0, -55, 0)
  • Create new GameObject, call it "StartPoint" and change its position to (0, -2, -15)
  • Select "_GroundGenerator" object and in SC_GroundGenerator assign Main Camera, Start Point and Tile Prefab variables

Now press Play and observe how the platform moves. As soon as the platform tile goes out of the camera view, it's moved back to the end with a random obstacle being activated, creating an illusion of an infinite level (Skip to 0:11).

Part 2: Creating Player Instance

Player Instance will be a simple Sphere using a controller with ability to jump and crouch.

  • Create new Sphere (GameObject -> 3D Object -> Sphere) and remove its Sphere Collider component
  • Assign previously created "RedMaterial" to it
  • Create new GameObject and call it "Player"
  • Move the Sphere inside "Player" object and change its position to (0, 0, 0)
  • Create new script, call it "SC_IRPlayer" and paste the code below inside it:

SC_IRPlayer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]

public class SC_IRPlayer : MonoBehaviour
{
    public float gravity = 20.0f;
    public float jumpHeight = 2.5f;

    Rigidbody r;
    bool grounded = false;
    Vector3 defaultScale;
    bool crouch = false;

    // Start is called before the first frame update
    void Start()
    {
        r = GetComponent<Rigidbody>();
        r.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
        r.freezeRotation = true;
        r.useGravity = false;
        defaultScale = transform.localScale;
    }

    void Update()
    {
        // Jump
        if (Input.GetKeyDown(KeyCode.W) && grounded)
        {
            r.velocity = new Vector3(r.velocity.x, CalculateJumpVerticalSpeed(), r.velocity.z);
        }

        //Crouch
        crouch = Input.GetKey(KeyCode.S);
        if (crouch)
        {
            transform.localScale = Vector3.Lerp(transform.localScale, new Vector3(defaultScale.x, defaultScale.y * 0.4f, defaultScale.z), Time.deltaTime * 7);
        }
        else
        {
            transform.localScale = Vector3.Lerp(transform.localScale, defaultScale, Time.deltaTime * 7);
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // We apply gravity manually for more tuning control
        r.AddForce(new Vector3(0, -gravity * r.mass, 0));

        grounded = false;
    }

    void OnCollisionStay()
    {
        grounded = true;
    }

    float CalculateJumpVerticalSpeed()
    {
        // From the jump height and gravity we deduce the upwards speed 
        // for the character to reach at the apex.
        return Mathf.Sqrt(2 * jumpHeight * gravity);
    }

    void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Finish")
        {
            //print("GameOver!");
            SC_GroundGenerator.instance.gameOver = true;
        }
    }
}
  • Attach SC_IRPlayer script to "Player" object (You'll notice that it added another component called Rigidbody)
  • Add BoxCollider component to "Player" object

  • Finally, place "Player" object slightly above the "StartPoint" object, right in front of the Camera

Press Play and use W key to jump and S key to crouch. The objective is to avoid red Obstacles: