Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Flappy Bird game, C#, Game development and Unity3D.

With the new release of Unity game engine, Unity 5, I am showing you how easy is to create a Flappy Bird clone, in a step by step tutorial with a lot of pictures, just like my other Unity prototype, which you can find it in the post Tutorial: your first Unity2D project – a Circle Chain prototype.

Remember Unity is free for companies earning under $100,000 with some limitations such as you can’t have your custom splash screen.

First, let’s create a new project, give it a name and above all set it to a 2D project.

Our first prpject will be a game to be played on the web, so let’s change the resolution to 640×480

In the Main Camera inspector, set the size to game height/200, that is 480/200 = 2.4

It’s now time to import some graphic assets: I am using the same png files used in the post HTML5 Flappy Bird prototype using Phaser states, extended classes and Arcade physics. Just drag and drop them in the project window, or create your own bird and pipe png images, then drag them in the project window.

The assets you just uploaded are just plain images, so we need to create something more complex for Unity to use in the game. Unity features prefabs, which are complex objects which can have images, scripts and component. Right click with the mouse in the project window, then create -> prefab. Call your newly created prefab bird prefab.

To add an image to a prefab, first we drag the image in the hierarchy window, then we drag the image instance in the prefab, then we can delete the image from the hierarchy window. Now our prefab has an image assigned to it. Look how I placed the bird image in bird prefab:

Now you should be able to apply the same concept to create a pipe prefab and place pipe image in it.

We just said a prefab an can also have components, and our prefabs are going to have two components: select the bird prefab, from inspector window select “Add Component” then under “Physics 2D” add both “Rigidbody 2D” and “Box Collider 2D”. Leave all options at their default values. This will assign the bird a physics rigid body and a physics collider.

Do the same for pipe prefab, just check “Is Kinematic” checkbox so pipes won’t be affected by gravity.

Prefabs can also include scripts, so we are going to create a script right clicking in project window and selecting “Create” -> “C# Script”. Call the script BirdScript.

In the same way create PipeScript and MainScript.

While it’s obvious PipeScript and BirdScript will be assigned respectively to pipe and bird prefabs, MainScript will be the engine of the game and will be assigned to an empty object we will create by right clicking on hierarchy window and selecting “Create Empty”. Call this object “Game Engine”.

Now it’s time to write the script themselves. Let’s start from MainScript. Double click on the script and a text editor called MonoDevelop will launch. This is the fully commented code of MainScript:

using UnityEngine;
using System.Collections;

public class MainScript : MonoBehaviour {

	// thiese global variables will be set from the inspector.
	public GameObject pipeObject;	// which prefab will represent the pipe?
	public GameObject birdObject;	// which prefab will represent the bird?	
	public float pipeHole;			// how large is the gap between upper and lower pipes?

	// function to be executed once the script started
	void Start () {
		// placing the bird
		Instantiate(birdObject);
		// calling "CreateObstacle" function after 0 seconds, then each 1.5 seconds, 
		InvokeRepeating("CreateObstacle", 0f, 1.5f);
	}

	// function called by InvokeRepeating function
	void CreateObstacle(){
		// generating random upper pipe position
		float randomPos = 4f-(4f-0.8f-pipeHole)*Random.value;
		// adding upper pipe to stage
		GameObject upperPipe = Instantiate(pipeObject);
		// setting upper pipe position
		upperPipe.transform.position = new Vector2(4f,randomPos);
		// adding lower pipe to stage
		GameObject lowerPipe = Instantiate(pipeObject);
		// setting lower pipe position
		lowerPipe.transform.position = new Vector2(4f,randomPos-pipeHole-4.8f);
	}
}

How can we add the script to Game Engine object? Select Game Engine object, then from inspector window click “Add Component” and under “Scripts” select “Main Script”

All global variables can be defined directly in inspector window by dragging prefabs or manually entering values. Set the global variables now:

In the same way we created MainScript, here is the source of BirdScript:

using UnityEngine;
using System.Collections;

public class BirdScript : MonoBehaviour {

	// this global variable will be set from the inspector. Represents bird jump force
	public Vector2 jumpForce = new Vector2();

	// function to be executed once the bird is created
	void Start () {
		// placing the bird
		transform.position = new Vector2(-2f,0f);
	}

	// function to be executed at each frame
	void Update () {
		// waiting for mouse input
		if (Input.GetButtonDown("Fire1")) {
			// setting bird's rigid body velocity to zero
			GetComponent<Rigidbody2D>().velocity = Vector2.zero;
			// adding jump force to bird's rigid body
			GetComponent<Rigidbody2D>().AddForce(jumpForce);
		}	
		// getting the real position, in pixels, of the bird on the stage
		Vector2 stagePos = Camera.main.WorldToScreenPoint(transform.position);
		// if the bird leaves the stage...
		if (stagePos.y > Screen.height || stagePos.y < 0){
			// ... call die function
			die();
		}
	}

	// function to be executed once the bird enters in collision with anything
	void OnCollisionEnter2D(){
		// call die function
		die();
	}
	
	void die(){
		// reload the current scene - actually restart the game
		Application.LoadLevel(Application.loadedLevel);
	}
}

And this is the source of PipeScript:

using UnityEngine;
using System.Collections;

public class PipeScript : MonoBehaviour {

	// this global variable will be set from the inspector. Represents pipe velocity
	public Vector2 pipeVelocity = new Vector2();

	// function to be executed once the pipe is created
	void Start () {
		// setting the velocity of the rigid body component attached to the pipe
		GetComponent<Rigidbody2D>().velocity = pipeVelocity;
	}

	// function to be executed at each frame
	void Update () {
		// checking x position
		if(transform.position.x<-4){
			// destroying the pipe and freeing memory and resources
			Destroy(gameObject);
		}
	}
}

Assign them to their respective prefabs as seen before, assigning global variable values:

And you already have your Flappy Bird prototype made with Unity:

You should know how to play: mouse click to flap and fly through pipes.

You will see a lot more Unity tutorials, meanwhile download the full project.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.