Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Complete Flappy Bird Course in Unity 2D

Complete Flappy Bird Course in Unity 2D

You will learn how to create flappy bird game with physics and box colliders. You will add game logic for game over, collecting coins, and ...

Enroll Now

Welcome to the Complete Flappy Bird Course in Unity 2D! This course will guide you through the process of creating a clone of the popular Flappy Bird game using Unity, a powerful game development engine. We'll cover everything from setting up your project, creating game assets, programming the game mechanics, and adding the finishing touches to make your game look polished and professional.

Part 1: Setting Up the Project

  1. Download and Install Unity:

    • If you haven't already, download and install Unity from Unity's official website. Make sure to install the Unity Hub, which will help you manage different versions of Unity and your projects.
  2. Create a New Project:

    • Open Unity Hub, click on "New Project," select the 2D template, and name your project "Flappy Bird." Choose a location for your project and click "Create."

Part 2: Setting Up the Scene

  1. Create the Main Scene:

    • In the Project window, navigate to the "Scenes" folder, and create a new scene called "MainScene." Open this scene to start building your game.
  2. Set Up the Camera:

    • In the Hierarchy window, select the Main Camera. In the Inspector window, set the background color to a light blue to mimic the sky.

Part 3: Creating Game Assets

  1. Design the Bird:

    • Create a simple bird sprite using any image editing software, or find a free bird sprite online. Import the sprite into your Unity project by dragging it into the "Assets" folder.
    • In the Project window, create a new folder called "Sprites" and move the bird sprite into this folder.
  2. Design the Pipes:

    • Create pipe sprites for the top and bottom pipes. Ensure they have a consistent style with the bird sprite. Import these sprites into the "Sprites" folder in Unity.
  3. Create the Background:

    • Design a background sprite that will scroll continuously to give the illusion of movement. Import this sprite into the "Sprites" folder.

Geting Started whit Unity

Part 4: Setting Up the Bird

  1. Create the Bird GameObject:

    • In the Hierarchy window, create an empty GameObject and name it "Bird." Attach the bird sprite to this GameObject by dragging it from the Project window into the "Bird" GameObject in the Hierarchy.
  2. Add Components to the Bird:

    • With the Bird GameObject selected, add a Rigidbody2D component in the Inspector window. Set the Gravity Scale to 1.
    • Add a BoxCollider2D component to detect collisions.
  3. Write the Bird Script:

    • In the "Scripts" folder (create one if it doesn't exist), create a new C# script called "BirdController." Attach this script to the Bird GameObject.
    • Open the script in your preferred code editor and write the following code:
csharp
using UnityEngine; public class BirdController : MonoBehaviour { public float flapForce = 250f; private Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { if (Input.GetMouseButtonDown(0)) { rb.velocity = Vector2.zero; rb.AddForce(Vector2.up * flapForce); } } void OnCollisionEnter2D(Collision2D collision) { // Handle game over logic here } }

Part 5: Setting Up the Pipes

  1. Create Pipe Prefabs:

    • In the Hierarchy window, create two empty GameObjects and name them "TopPipe" and "BottomPipe." Attach the respective pipe sprites to these GameObjects.
    • Add BoxCollider2D components to both pipes for collision detection.
  2. Create a Pipe Spawner:

    • In the Hierarchy window, create an empty GameObject and name it "PipeSpawner."
    • Create a new C# script called "PipeSpawner" and attach it to the PipeSpawner GameObject. Write the following code:
csharp
using UnityEngine; public class PipeSpawner : MonoBehaviour { public GameObject pipePrefab; public float spawnRate = 2f; public float heightOffset = 2f; void Start() { InvokeRepeating("SpawnPipe", 1f, spawnRate); } void SpawnPipe() { float height = Random.Range(-heightOffset, heightOffset); Vector3 spawnPosition = new Vector3(transform.position.x, transform.position.y + height, 0); Instantiate(pipePrefab, spawnPosition, Quaternion.identity); } }
  1. Create the Pipe Prefab:

    • In the Hierarchy window, create an empty GameObject called "Pipe" and make the TopPipe and BottomPipe GameObjects its children. Adjust their positions so they form a gap for the bird to pass through.
    • Drag the Pipe GameObject into the "Prefabs" folder in the Project window to create a prefab.
  2. Assign the Pipe Prefab:

    • In the Inspector window for the PipeSpawner GameObject, assign the Pipe prefab to the "Pipe Prefab" field.

Part 6: Adding the Background

  1. Create a Scrolling Background:

    • In the Hierarchy window, create an empty GameObject and name it "Background."
    • Attach the background sprite to this GameObject and set its position to (0, 0, 10) to place it behind all other GameObjects.
  2. Create a Scrolling Script:

    • Create a new C# script called "BackgroundScroller" and attach it to the Background GameObject. Write the following code:
csharp
using UnityEngine; public class BackgroundScroller : MonoBehaviour { public float scrollSpeed = 0.5f; private Vector2 offset; private Material material; void Start() { material = GetComponent<Renderer>().material; offset = new Vector2(scrollSpeed, 0); } void Update() { material.mainTextureOffset += offset * Time.deltaTime; } }

Part 7: Finishing Touches

  1. Add a Score System:
    • Create a new UI Text element in the Canvas and name it "ScoreText." Position it at the top center of the screen.
    • Create a new C# script called "ScoreManager" and attach it to an empty GameObject named "GameManager." Write the following code:
csharp
using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { public Text scoreText; private int score = 0; void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Pipe")) { score++; scoreText.text = "Score: " + score; } } }
  1. Game Over Logic:
    • In the BirdController script, modify the OnCollisionEnter2D method to display a "Game Over" message and stop the game.
csharp
void OnCollisionEnter2D(Collision2D collision) { // Show Game Over message Debug.Log("Game Over!"); // Stop the game Time.timeScale = 0; }
  1. Playtest and Polish:
    • Playtest your game, tweaking values like flap force, spawn rate, and pipe height offset to make the game fun and challenging.
    • Add sound effects and background music to enhance the gaming experience.

Congratulations! You've completed the Flappy Bird clone in Unity 2D. With these basics, you can now experiment with adding new features, improving graphics, or even creating your own unique game. Happy coding!