Your first Unity 5 project: a Flappy Bird prototype

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:

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:

And this is the source of PipeScript:

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.

  • Shival Gupta

    This is very enlightening. Never thought it was this easy to work with Unity

  • Thanks for a great tutorial. I would love to see how you would integrate a score counter on screen. For beginners, it might be fun to add challenges to your tutorials, like “change the Input from mouse click to space bar,” and then provide the answer at the bottom or on a separate link. Thanks again! Great stuff.

  • Pingback: Lesson 4: Unity Editor | CTE Courses()

  • Pingback: Lesson 3: Flappy Bird Clone | CTE Courses()