Getting Started in Unity: Beginning my game.

Benjamin Talbot
7 min readJun 14, 2020

--

In my last blog post, I briefly went over a few of favourite games that utilise the Unity engine.

In this post, I will sit at my desk clenching every single muscle in my body; flexing with a fervour yet unseen in the entire history of the human race. I will channel the creative and technical energy of the developers of yore to create something dangerously bold, something that will part the heavens and fall upon the Earth like a celestial meteor. I will make a game of my own.

That being said its going to be a Flappy Bird clone. But believe me when I say that it’ll move mountains.

A high school favourite for me

Joking aside, I did some preliminary research for the tools and techniques I would need to create a Flappy Bird clone and stumbled across this video, made by Renaissance Coders. With a solid foothold, I set forward to make my version of what could very well be the most copied mobile game in history. Just as an aside, this post is more of a summary of the steps i’m taking to create this game along with a few helpful tips, and should not be considered as a comprehensive step-by-step guide. Surprisingly, creating a game is naturally quite complex, and a detailed guide could probably fill up a small novel.

With that being said, I booted up Unity and created a new project. I named it Flappy Ben (naturally) and specified that I wanted to create a 2D project. I made sure my game scene was set up to display in a standard phone aspect ratio, just to make sure everything looked right as a mobile experience.

It quickly became apparent that its pretty hard to make any game without assets. Assets are basically raw resources that are used when developing a game, they range from music to art to images to fonts to animations, you get the picture. Luckily for me, mobile games aren’t usually renowned for their exceptional game art, so I decided to quickly throw together a few of my own assets in Photoshop.

Different layers of background assets

I first set out to create the background. My original thought was to just make a single image for the entire thing and loop it. I discovered that if you created each element of the background in separate layers, you could apply parallax to the entire background. This allows you to move certain background elements at different speeds, giving the illusion of of motion relative to the general “distance” of the element. Clouds are closer to Earth than stars, so they move faster relative to the stars.

An example of parallax in a 2D environment

Next up was the U.I elements. I first created a “Canvas” object that would act as a sort of container for all my U.I assets, this was useful for keeping things organised. Next, I needed to create separate objects that would hold the various menu’s in the game. I created an object to hold the main menu when you start up the game, an object to hold the countdown timer when the game starts, an object to hold the current score, and an object to hold the game over menu. I then added each individual menu element to the menu objects. For example, the start menu needs to have a play button and some text to deliver the current highest score, so I added those to the menu object as elements.

The various elements and game objects housed within the canvas object

In order to get all the U.I elements in the canvas to “stick” to the game scene, I paired the U.I canvas to the main game camera. That way every U.I object in the canvas would inherit the camera’s movement. My button and text elements were then positioned in the scene accordingly. To prevent the elements from moving whenever the perspective of the scene view changed, I anchored them using the camera anchors.

Anything within the 4 triangles is considered “anchored” to the camera object, keeping them in the same place in the scene regardless of perspective shifts.

With the U.I squared away for now, I moved on to applying some gravity to my big dumb head. I selected the object holding my head and added a “Rigidbody” component to it. Rigidbodies has physics properties baked into them, which would allow my head to be pulled down by gravity. I also added a “circle collider” component, which would detect collisions with other objects.

My head with Rigidbody physics

Playing with the Rigidbody properties reminded me of one of the most important quirks of the Unity engine. Remember at the very beginning of this blog post where I created my project and specified that it was a 2D game? I actually discovered that every game made with Unity is technically a 3D game. Every project actually starts off as a 3D project, and if the developer decides to enable the 2D option, the engine will just lock off the Z axis in the scene view. This essentially removes a whole dimension from the projects view, making it 2D. However, you can still switch between 2D and 3D perspectives in the scene view. Doing this sort of drives home the fact that the coders who created Unity really intended it to be a 3D engine above all else.

Switching between the 2D and 3D views on a 2D game

I added my ground asset to the scene and threw a “box collider” on it, again to detect collisions. For example, if my head ever touched the ground (the collider), it would trigger a game over with a little help from a script. After that I added both set of pipes and threw more box colliders over them. Its important to distance the pipes properly to ensure the game isn’t too easy or too hard. I guessed what would work for now, but changes will probably need to be made when I test the game. I applied one more box collider, this time between the pipes. This collider would act as a sort of flag that increments the score by one when my head passes through it.

The green lines represent the bounds of the colliders

With all of completed, it was time to finally start writing some code!

I first needed a script to make my head “hop” whenever the screen was pressed. I created a script named TapController and got to work scripting the tap logic. While I won’t go over the code in detail, I’ll provide a brief overview of what i’m trying to achieve. Keep in mind that scripts in Unity are usually coded in C#, which is what I will be using.

I first created a function that specified the start and end rotation of my head each time the screen is pressed. This made my head start looking upwards and end looking downwards, kinda like my head is… flapping? (yeah I know stick with me). This was done by creating a two Quaternion objects to represent the down and forward rotations (Quaternions are used to represent rotations in the 2D space). I set one Quaternion to the starting rotation (-45 degrees) and the other Quaternion to the ending rotation (+45 degrees).

Next I made a function that took those two positions and smoothed the transition between them every time the screen was pressed, so it was a gradual transition rather than a jarring one. I achieved this using the Lerp function built into the Quaternion class. The Lerp function essentially interpolates movement between two positions and makes the whole thing look smooth and pretty. This second function also added upward vertical velocity to my head upon every screen press. My head is technically a rigidbody object, which can have force applied to it using the AddForce function. I specified that an upwards force should be applied to my head every time the screen was tapped.

The purpose of functions 1 and 2 respectively

The third function I wrote dealt with the collisions. I wrote two conditional statements that would preform an action whenever a “Deadzone” was collided with (the ground and pipes), and whenever a “Scorezone” was passed (between the pipes). What action is being preformed?… Uh i’ll get back to you later, I haven’t gotten that far in the video yet.

With the script logic added to my head object, I finally had something that resembled a game!

Insert airhead joke here

The is the end of the first blog post where I make my game. So far I created my project, made some custom assets, prepared my scene, and wrote my first script. In the second post, we’ll (hopefully) be finishing the game and publishing it to the Google Play store!

--

--