Step-by-Step Tutorial: LoopDash Runner in Unity2D


 Step-by-Step Tutorial: Simple Endless Runner in Unity2D

1. Set up Your Project

  1. Open Unity → Create a new 2D Project.
  2. Import your player sprite (running, jumping, etc.), ground tile, and obstacles.
  3. Set up the sorting layers (Player, Ground, Obstacles, UI) so everything renders correctly.

2. Create the Player

  1. Add Player Sprite
    • Drag your player sprite into the scene.
    • Add a Rigidbody2D (set Gravity Scale = 3).
    • Add a BoxCollider2D.
  2. Create Player Script (PlayerController.cs)
    • Attach it to your Player.

using UnityEngine;

public class PlayerController: MonoBehaviour

{

    public float jumpForce = 10f;

    private Rigidbody2D rb;

    private bool isGrounded;

    void Start()

    {

        rb = GetComponent<Rigidbody2D>();

    }

    void Update()

    {

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)

        {

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);

            isGrounded = false;

        }

    }

    private void OnCollisionEnter2D(Collision2D collision)

    {

        if (collision.gameObject.CompareTag("Ground"))

            isGrounded = true;

        if (collision.gameObject.CompareTag("Obstacle"))

            Debug.Log("Game Over!"); // Later replace with game over screen

    }

}

3. Scrolling Ground

  1. Create a Ground Tile Sprite and add a BoxCollider2D (set as static).

Create a script GroundScroller.cs and attach it to your ground prefab. using UnityEngine;

public class GroundScroller: MonoBehaviour

{

    public float speed = 5f;

    private float groundWidth;

    void Start()

    {

        groundWidth = GetComponent<SpriteRenderer>().bounds.size.x;

    }

    void Update()

    {

        transform.Translate(Vector2.left * speed * Time.deltaTime);

        if (transform.position.x < -groundWidth)

        {

            transform.position = new Vector2(transform.position.x + groundWidth * 2, transform.position.y);

        }

    }

}

4. Spawn Obstacles

  1. Create an Obstacle prefab (e.g., a box or spike sprite with collider).

Create ObstacleSpawner.cs and attach it to an empty GameObject in your scene. using UnityEngine;

public class ObstacleSpawner : MonoBehaviour

{

    public GameObject obstaclePrefab;

    public float spawnRate = 2f;

    public float obstacleSpeed = 5f;

    void Start()

    {

        InvokeRepeating(nameof(SpawnObstacle), 1f, spawnRate);

    }

    void SpawnObstacle()

    {

        GameObject obj = Instantiate(obstaclePrefab, transform.position, Quaternion.identity);

        obj.AddComponent<Rigidbody2D>().gravityScale = 0;

        obj.GetComponent<Rigidbody2D>().velocity = Vector2.left * obstacleSpeed;

        Destroy(obj, 10f); // cleanup

    }

}

5. Game Over & Restart

When the player collides with an obstacle, stop the game. Update Player script: if (collision.gameObject.CompareTag("Obstacle"))

{

    Time.timeScale = 0; // pause game

    Debug.Log("Game Over!");

}

Add a Restart Button in UI that reloads the scene: using UnityEngine;

using UnityEngine.SceneManagement;

public class GameOverManager : MonoBehaviour

{

    public void RestartGame()

    {

        Time.timeScale = 1;

        SceneManager.LoadScene(SceneManager.GetActiveScene().name);

    }

}

Files

Loop Dash.zip Play in browser
Aug 21, 2025

Leave a comment

Log in with itch.io to leave a comment.