Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Complete 2D Runner game in Unity for Beginners


Complete 2D Runner game in Unity for Beginners

 Be able to create dynamic and challenging obstacles for your game. Implement scoring and game over mechanics to enhance the player experience.

Enroll Now

Creating a 2D runner game in Unity is an excellent way to learn the basics of game development. This tutorial will guide you through the steps to create a simple yet complete 2D runner game. We'll cover setting up the Unity project, creating the player and environment, adding obstacles, and implementing game mechanics such as scoring and player controls. Let's get started!

Setting Up the Unity Project

1. Install Unity

First, ensure you have Unity installed on your computer. You can download it from the Unity website. Once installed, open Unity Hub and create a new 2D project.

2. Project Setup

  1. Open Unity Hub and click on "New Project".
  2. Select the "2D" template.
  3. Name your project (e.g., "2D Runner Game").
  4. Choose a location for your project and click "Create".

Creating the Player

1. Importing Assets

You can either create your own sprites or download free assets from the Unity Asset Store. For this tutorial, we'll assume you have a basic player sprite.

  1. Create a new folder in the "Assets" directory named "Sprites".
  2. Import your player sprite into the "Sprites" folder by dragging and dropping it from your file explorer.

2. Setting Up the Player GameObject

  1. In the "Hierarchy" window, right-click and select "Create Empty".
  2. Name this GameObject "Player".
  3. Drag your player sprite from the "Sprites" folder to the "Player" GameObject in the "Hierarchy" window.
  4. Adjust the position of the "Player" GameObject to (0, 0, 0).

3. Adding Components to the Player

  1. With the "Player" GameObject selected, click "Add Component" in the "Inspector" window.
  2. Add a "Rigidbody2D" component. Set "Gravity Scale" to 0 (as we don't want the player to fall).
  3. Add a "Box Collider2D" component to define the player's collision area.

Creating the Environment

1. Adding a Ground

  1. Create a new 2D object by right-clicking in the "Hierarchy" window, then selecting "2D Object" > "Sprite".
  2. Name this GameObject "Ground".
  3. Assign a ground sprite to this GameObject from your "Sprites" folder.
  4. Position the "Ground" GameObject below the player.

2. Creating a Background

  1. Add another 2D object in the "Hierarchy" window and name it "Background".
  2. Assign a background sprite to this GameObject.
  3. Adjust its position to fill the camera view.

3. Making the Ground Infinite

We'll create an illusion of an infinite ground by moving the ground and resetting its position.

  1. Create a script named "GroundController" in the "Scripts" folder.
  2. Attach this script to the "Ground" GameObject.
  3. Open the script and add the following code:
csharp
using UnityEngine; public class GroundController : 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 + 2 * groundWidth, transform.position.y); } } }

Adding Obstacles

1. Creating an Obstacle

  1. Import an obstacle sprite into the "Sprites" folder.
  2. Create a new 2D object in the "Hierarchy" window and name it "Obstacle".
  3. Assign the obstacle sprite to this GameObject.
  4. Add a "Box Collider2D" component to the "Obstacle" GameObject.

2. Spawning Obstacles

  1. Create a script named "ObstacleSpawner" in the "Scripts" folder.
  2. Attach this script to an empty GameObject named "GameController".
  3. Open the script and add the following code:
csharp
using UnityEngine; public class ObstacleSpawner : MonoBehaviour { public GameObject obstaclePrefab; public float spawnRate = 2f; private float nextSpawn = 0f; void Update() { if (Time.time > nextSpawn) { Instantiate(obstaclePrefab, new Vector2(10, Random.Range(-1, 2)), Quaternion.identity); nextSpawn = Time.time + spawnRate; } } }

Implementing Player Controls

1. Creating a Player Controller Script

  1. Create a script named "PlayerController" in the "Scripts" folder.
  2. Attach this script to the "Player" GameObject.
  3. Open the script and add the following code:
csharp
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 = Vector2.up * jumpForce; } } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; } if (collision.gameObject.CompareTag("Obstacle")) { // Handle game over logic here Debug.Log("Game Over!"); } } void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = false; } } }

2. Adding Tags

  1. Select the "Ground" GameObject in the "Hierarchy" window and add the tag "Ground".
  2. Select the "Obstacle" GameObject and add the tag "Obstacle".

Implementing Scoring

1. Creating a UI for Score

  1. In the "Hierarchy" window, right-click and select "UI" > "Text".
  2. Name this GameObject "ScoreText".
  3. Adjust the position and font size of the text so it's visible on the screen.

2. Adding Score Logic

  1. Create a script named "ScoreManager" in the "Scripts" folder.
  2. Attach this script to an empty GameObject named "ScoreController".
  3. Open the script and add the following code:
csharp
using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { public Text scoreText; private int score = 0; void Update() { score++; scoreText.text = "Score: " + score.ToString(); } }

Adding Game Over Logic

1. Creating a Game Over UI

  1. In the "Hierarchy" window, right-click and select "UI" > "Text".
  2. Name this GameObject "GameOverText".
  3. Adjust the position and font size of the text to be centered on the screen.
  4. Set the text to "Game Over" and disable the GameObject in the "Inspector" window.

2. Modifying PlayerController

Update the PlayerController script to handle game over logic:

csharp
using UnityEngine; using UnityEngine.SceneManagement; public class PlayerController : MonoBehaviour { public float jumpForce = 10f; private Rigidbody2D rb; private bool isGrounded; public GameObject gameOverText; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { if (Input.GetKeyDown(KeyCode.Space) && isGrounded) { rb.velocity = Vector2.up * jumpForce; } if (gameOverText.activeSelf && Input.GetKeyDown(KeyCode.R)) { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; } if (collision.gameObject.CompareTag("Obstacle")) { gameOverText.SetActive(true); Time.timeScale = 0; // Pause the game } } void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = false; } } }
  1. Assign the "GameOverText" GameObject to the gameOverText field in the "PlayerController" script through the "Inspector" window.
  2. Save all your scripts and test the game.

Final Touches

1. Adding Sound Effects

  1. Import sound effects for jumping and game over into the "Assets" folder.
  2. Add an "Audio Source" component to the "Player" GameObject.
  3. Modify the PlayerController script to play these sounds at appropriate times.

2. Enhancing Graphics

  1. Polish your sprites or import higher-quality assets.
  2. Add particle effects for jumping and collisions for a more engaging visual experience.

3. Building the Game

  1. Go to "File" > "Build Settings".
  2. Select your target platform (e.g., PC, Mac, Android).
  3. Click "Build" and follow the instructions to export your game.

Congratulations! You have created a complete 2D runner game in Unity. This tutorial covered the basics, but you can expand and improve your game by adding more features, levels, and enhancing the overall gameplay experience. Happy game development!