Create a 3D Flappy Birds clone with Unity 3D Tutorial – Part 1

I really like the idea of creating games, but there are a number of factors that hinder my ambitions – talent, being one of the most prominent. Another significant factor is that I am a programmer: I’m not an artist, or a musician (well not in a professional way), and possibly most fundamental, I’m not even somebody who plays computer games all that much. I can appreciate games, I can understand why some people seem to spend their lives playing them,  but I’m just not that into them. For my personal tastes, games reached a pinnacle with Grim Fandango (that game could have been made into a film!). In short, although it is fair to say I’ll never create a top ranking game (and I really admire those independent developers who have managed to achieve that) I do still know how to develop game mechanics, for want of a better term. As the old adage says: those who can’t do teach. In this series of Unity 3D tutorials, I will show you how to create different genres of games using Unity 3D. There will be no graphical assets as such, we will primarily use basic 3D primitives – and that is what you should really do anyway to prove out the game mechanics before investing time in creating models and artwork etc. If the game is fun with cubes then it will be fun with art assets. Can you imagine how hard it would be to create a top quality game without using a game engine?

What is Unity 3D?

Unity 3D is what’s know as a game engine. Game engines are essentially development environments (or frameworks) that capture all the functionality you would need to create a game including: rendering, physics, sound, input etc. They make exceptionally complex tasks relatively easy and accessible via an WYSIWYG interface or a scripting environment. It is, of course, possible to program a games completely from scratch, but it would be a huge and complicated task. That being said, it is a good learning experience and I can recommend the book below.

Unity 3D allows us to script in a few different language but for the purposes of this tutorial we will be using C#.

Why Unity 3D?

Because it’s good! It’s also works on many different platforms. It also has a great development environment. If you haven’t seen it before or don’t know about it then get yourself over to the Unity 3D website and download it now. It is a very impressive game engine and far more capable and mature than its peers, in my opinion.

3D Flappy Birds Clone

I did want to call this the 3D Tappy Chilli clone, but nobody would have know what I was talking about (Tappy Chilli was my attempt at a Flappy Birds clone – It was ok). Anyway, let’s get started. Flappy Birds is a very simple game. Fundamentally we only have two distinct game elements that we need to focus on, everything else is just trimmings. We need a continuos stream of randomly placed obstacles, and we need a character that will rise when the screen is tapped, and fall when it isn’t. Open up Unity 3D and create a new project. We will not need any of the assets so don’t import any of those. Also make sure the defaults are set to 3D. You should end up with the below

unity 3d blank project - Unity 3d tutorial

Note: Your layout may not look like mine. The various windows can be dragged, docked and rearranged to suit your personal tastes, and you may save any particular layout for reuse (or select from some predefined layouts). If you play around with the dropdown at the top-right of the screen you will see what I mean. For the purposes of this article it doesn’t really matter as long as you have the basic views available. If you are a complete Unity 3D beginner then I would suggest following the video tutorials on the Unity 3D website.

Creating the Column Obstacles

Using cubes we are going to create the column obstacles (in Tappy Chilli they were cacti).

column obstacles

As I said, we will not be using artwork because all we are doing it proving out the game mechanics. From the menu, select Game Object -> 3D Object -> Cube.

freshly created 3d cube

We are now going to reset the cubes origin to 0, 0, 0 in 3D space. Go to the Inspector Panel, Transform section and click on the cog icon, from the dropdown select Reset.

reset origin

Next we want our cube to be an actual column; to do this we will simply increase the scale along the Y-Axis. Go back to the Transform section in the Inspector Panel and change the Y scale value to 5. Finally, in the heirarchy panel we are going to rename the cube to TopColum. Click on the word cube, then click again and then change the name. You should now end up with something like this.

top column

In the hierarchy panel, right-click on TopColumn and select duplicate. Rename the duplicate calling it BottomColumn. Change the Y-Axis position of the bottom column to -3.5. Also change the Y-Axis position of the TopColumn to 3.5. You should now see something like this.

two columns

Note: It may be that when you duplicate the top column the resultant copy is back to the original cube size, if that is the case simply change the Y Scale value to 5 as we did with the first.

Now, we are going to create a 3rd cube. So repeat the steps above to create a cube and reset its origin, then rename the cube to ScoreTrigger. You should now see this

score trigger

Now we are going to create an empty game object that will become the parent of these three object. Go to the GameObject menu and select Create Empty. As before, we are going to reset its origin and then rename it to Column. Now drag the other three object on top of the Column object and they will become children, like so

game object children

The next stage is to turn our column hierarchy into a prefab.

What are Prefabs?

Prefabs are somewhat synonymous with classes in object oriented programming – but not completely so. They are a way to instantiate objects, and the object could be a simple as a single cube or hold an entire scene with many objects, sounds, scripts etc. Let’s create one so you can see. Go to Assets->Create->Prefab.

create prefab

In the project window you should now see the above. Rename the prefab to ColumnObstacle. Make sure you have the hierarchy window and the project window visible, then drag the Column parent object on top of the prefab. Your prefab should now look like this.

unity prefab

If you click on the arrow, you will see all the containing objects.

expanded unity prefab

All good so far. Our column object prefab is not quite complete, but as we do the additional work to get it there you will see some of the benefits of using prefabs.

Moving our column

In order to move our column we will need to do some scripting.

Unity provides the facility to script or program in 3 different language: Javascript, Boo and C#. In the early days of Unity Javascript seemed to be the dominate language but over time this has now changed to C#. And that is not abad thing. C# is the language I use in my professional life and it is very good ( and to be honest I don’t even know what Boo is!). Anyway, C# is what we’ll be using here.

First, lets clear up the project a little. In the Hierarchy window delete everything but the Main Camera. Now, drag out a ColumnObject prefab on to the scene and reset it’s origin. Select the ColumnObject and in the inspector panel click on add component. Name the script ColumnMotion and make sure CSharp is the language selected.

Unity script component

Double-click on the word “ColumnMotion” and Unity will launch MonoDevelop, which is the IDE we will use to do our programming. You should see the following gibberish (although the colors will probably be different to mine).

unity template code

Note: MonoDevelop is not just for Unity. It is an IDE that allows cross-platform development of C#. Click here for more information

More to follow…..

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.