Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Complete Endless Runner Game Course in Unity 2D

Complete Endless Runner Game Course in Unity 2D

Learn to create your very own Endless Runner Game with powerups, particle systems, and ground movement.

Enroll Now

Endless runner games have become incredibly popular due to their simple yet addictive gameplay mechanics. In this course, we will guide you through creating a complete endless runner game in Unity 2D. By the end of this course, you will have a fully functional game, along with a strong understanding of essential Unity concepts and techniques.

Setting Up the Project

  1. Installing Unity: Ensure you have the latest version of Unity installed. You can download it from the Unity website and use the Unity Hub to manage your projects.
  2. Creating a New Project: Open Unity Hub, click on the "New" button, select the 2D template, and name your project "EndlessRunner."

Basic Game Setup

  1. Organizing the Project: Create folders for Scripts, Sprites, Prefabs, and Scenes to keep your project organized.
  2. Setting Up the Scene: In the Scene view, adjust the camera to fit your game design. Set the background color and add a simple ground sprite to serve as the base of your game world.

Getting Started with Unity

Player Character

  1. Creating the Player Sprite: Import a player sprite into the Sprites folder. Drag it into the Scene and add a Rigidbody2D and BoxCollider2D component to it.

  2. Player Movement: Create a new C# script called "PlayerController." Attach it to the player object and write code to handle the player's movement and jumping.

    csharp
    using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 5f; public float jumpForce = 10f; private Rigidbody2D rb; private void Start() { rb = GetComponent<Rigidbody2D>(); } private void Update() { rb.velocity = new Vector2(moveSpeed, rb.velocity.y); if (Input.GetKeyDown(KeyCode.Space)) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); } } }

Generating Platforms

  1. Platform Prefab: Create a platform prefab by dragging a ground sprite into the Scene, adding a BoxCollider2D, and saving it as a prefab in the Prefabs folder.

  2. Platform Spawner: Create a new GameObject called "PlatformSpawner" and attach a new script called "PlatformSpawner" to it. Write code to spawn platforms at regular intervals.

    csharp
    using UnityEngine; public class PlatformSpawner : MonoBehaviour { public GameObject platformPrefab; public float spawnInterval = 2f; private float timer; private void Update() { timer += Time.deltaTime; if (timer >= spawnInterval) { SpawnPlatform(); timer = 0f; } } private void SpawnPlatform() { float spawnYPosition = Random.Range(-3f, 3f); Instantiate(platformPrefab, new Vector3(transform.position.x, spawnYPosition, 0), Quaternion.identity); } }

Infinite Scrolling Background

  1. Background Setup: Import a background sprite, set it as a repeating texture, and create a quad in the Scene. Attach a material with the background texture to the quad.

  2. Scrolling Script: Create a new script called "BackgroundScroller" and attach it to the background quad. Write code to move the background to create an infinite scrolling effect.

    csharp
    using UnityEngine; public class BackgroundScroller : MonoBehaviour { public float scrollSpeed = 0.5f; private Vector2 offset; private void Update() { offset = new Vector2(Time.time * scrollSpeed, 0); GetComponent<Renderer>().material.mainTextureOffset = offset; } }

Obstacles and Hazards

  1. Creating Obstacles: Design obstacle sprites and save them as prefabs. Similar to platforms, create a spawner script to generate obstacles at random intervals.

  2. Collision Handling: In the PlayerController script, add collision detection to handle what happens when the player hits an obstacle.

    csharp
    private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "Obstacle") { // Handle game over logic Debug.Log("Game Over"); } }

UI Elements

  1. Score System: Create a UI Text element to display the player's score. Write a script to update the score as the player progresses.

    csharp
    using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { public Text scoreText; private float score; private void Update() { score += Time.deltaTime; scoreText.text = "Score: " + Mathf.FloorToInt(score).ToString(); } }
  2. Game Over Screen: Design a game over UI that appears when the player loses. Include buttons for retrying and exiting the game.

Audio and Polish

  1. Sound Effects: Import audio clips for jumping, landing, and colliding with obstacles. Attach AudioSource components to the relevant game objects and trigger the sounds in the scripts.
  2. Particle Effects: Add particle effects for jumping and collisions to give visual feedback and enhance the game feel.

Optimization and Final Touches

  1. Object Pooling: Implement object pooling for platforms and obstacles to improve performance by reusing objects instead of constantly creating and destroying them.
  2. Mobile Controls: Adjust the controls to make the game playable on mobile devices. Implement touch input for jumping and navigating menus.

Exporting the Game

  1. Building for Different Platforms: Use Unity's build settings to export the game for various platforms such as PC, Android, and iOS.
  2. Testing: Thoroughly test the game on all target platforms to ensure smooth performance and functionality.

Conclusion

By following this course, you've created a complete endless runner game in Unity 2D. You've learned how to handle player movement, generate platforms, implement an infinite scrolling background, add obstacles, and create a user interface. You've also polished the game with audio and visual effects and optimized it for better performance. With these skills, you're now equipped to create more complex and engaging games in Unity. Happy developing!