Your first Unity 5 project: a Flappy Bird prototype

Read all posts about "" game

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
		// 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 =;
			// adding jump force to bird's rigid body
		// 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

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

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
			// destroying the pipe and freeing memory and resources

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers