If you have ever wondered how to make a flappy bird 2d game in unity. Then in this unity 2d flappy bird tutorial we will be building a flappy bird clone using the unity game engine from scratch.

We will rename our game to flappy fish which will be similar in style to flappy bird. See below example of what we will be building in this flappy bird unity tutorial.

Flappy bird assets and sprites are available for download at the bottom of this post.

Unity 2d flappy bird clone tutorial

Start off by creating a new unity project called Unity 2d flappy bird tutorial.

You should now have a screen like this:

I created some basic sprite sheets for our fish in inkscape. The images look something like this. If you want to get hold of a copy of these images please download the source code for this project here.

I created a basic background and some pipes as well for our player to swim over.

To start off we want to organize our project so that it is more clean and easy to work with. So for that in ours assets folder I have created a few folders, Background, Sprites,SpriteSheets like below.

Unity 2d flappy bird: infinite scrolling background

Let’s now start off with the easiest part first and that is to create a scrolling infinite background. This will allow us to have the feeling of an infinite level this is great for the hyper casual game style which flappy bird falls into.

So in order to do that lets start off with creating a quad.

For that right click in the hierarchy and create a quad like below.

Resize the quad up to be the size of the unity camera view like so.

To add your background. You now want to drag and drop the background image on top of the quad in the scene view. This will create a new background material.

Unity 2d flappy bird tutorial – infinite scrolling background

If you did this correctly you should see that unity created a new material folder automatically as well as see your background in the materials slot like this.

So one more thing which you need to configure before we jump into the code for the scrolling background.

Click on the background in your assets folder.

Then on the right hand side you want to make sure that you have checked the repeat option and clicked apply.

Scrolling background script

Great so now we can start with our background scrolling script. You could make this a parallax effect as well if you like by scrolling at different speeds. I have a tutorial on background parallax effects here: {todo}

To create our background scrolling script head back into your assets folder and create a new folder called scripts.

Inside your scripts folder create a new c# script.

Let’s call our script BackgroundScroller.cs. In order to now get the best experience in using Visual Studio I would suggest opening up Visual Studio first and not opening up your c# script directly from Unity.

If you do do this sometimes Visual Studio doesn’t become project aware and might not give you the best coding suggestions so in order to do that open up visual studio. Then open up your project like this.

The trick to infinite scrolling backgrounds is to shift the texture offset. So as you can tell this tutorial is not just to make a flappy bird game in unity but also will teach you some techniques in unity which could help for your own projects.

With this in mind to demonstrate how this works we first need to get our background to show properly. So you might have some dark background like this at the moment.

To fix this we need to choose another shader which does not require lighting. So in order to do that head over to your background quad in the hierarchy.

Let’s at this point maybe just rename it to Background so we know what the gameobject is for.

With this selected on your right. You will see this section called shader.

Change your shader from standard to unlit/texture.

Your background should now look correct like below.

Great so now let’s look at how we will be doing the infinite scroll. So if you head over to your Background -> Materials folder and double click on the Background Material.

On the right you will see this.

To make our background scroll we need to change the offset of x. The values need to be between 0 and 1. So in our script we will want to rotate this value counting up to 1 then resetting this value back to 0.

Here is our code for the scrolling background.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScrollingBackground : MonoBehaviour { private Renderer renderer; public float scrollSpeed = .1f; void Start() { renderer = GetComponent<Renderer>(); } // Update is called once per frame void Update() { float x = Mathf.Repeat(Time.time * scrollSpeed, 1); Vector2 offset = new Vector2(x, 0); renderer.sharedMaterial.SetTextureOffset("_MainTex", offset); } }

The first few things we do is declare some variables. One is our scrollspeed and another is the renderer. So essentially our renderer is part of this component in unity.

It will allow us to manipulate our material. So once we have declared our scroll speed and our renderer. We need to assign our renderer in our start method. Then in our update method we need to do some math which will clamp our values between 0 and 1 based on our time.

So we use Mathf.Repeat and pass it a multiplier of our scrollSpeed and our time, then we clamp it to a max value of 1. Here is the official definition.

We then offset our texture with:

Vector2 offset = new Vector2(x, 0);

renderer.sharedMaterial.SetTextureOffset(“_MainTex”, offset);

Once done save that and drag and drop your scroll script to your background quad and you should have something like this.

Your background should now scroll like this.

Unity 2D Flappy Bird: Creating our player

We now have a scrolling background let’s now move on to bringing in our player and creating a basic animation.

To start with our player animation we need to first split our sprite sheet. To do that we need to head over to our SpriteSheets folder.

Click on our fish sprite and then in the right make sure you have the following settings.

Click apply and then click on sprite editor.

With the sprite editor open we want to use automatic slicing. So follow the below steps.

You will see a box will be drawn around each image. Once done click Apply.

If you now go back to your Fish sprite and click the play icon you should see something like this.

This means we have now split our sprite sheet into 3 frames which we can now use in our animation step for our player.

In our assets folder we want to create a new folder called animations.

Now we can head back over to our player fish sprite and drag and drop it into our scene to get this.

Rename your player game object to Player.

We now want to open up the Animation. So head over to Window -> Animation -> Animation.

This should give you a window like this where we can create our first animation.

Click on create and save a animation called Fly.anim.

It is a little hard to explain up front how we will setup our animation so here is a over view of where we want to end up.

So to do this start off by clicking Add Property. Then click on Sprite Renderer.

Click on frame 0 like this.

Then you want to click on the record icon.

You should now have a keyframe which looks like this.

Next click on frame 10 like this and change the sprite we are using. To the 2nd sprite in our frame.

Now like wise do the same by clicking on frame 20 and adding the sprite for Fish_2. Once done click on the record button again to stop recording. You can now click the play button and you should hopefully have a player animation which looks like this.

Convert our player to a prefab for good measure. By creating a folder called prefabs in our assets folder. Then drag your player into that folder. Like so.

Let’s now isolate our prefab by clicking on this arrow.

Since we aren’t doing any strange animations we won’t need to do too much to our animation controller in this tutorial so leave that as is for now. We do however now need to setup our player to have some physics and gravity applied to it.

Unity 2D Flappy Bird: Setting up our player controller

To setup our player so that our player can fall and fly or swim in our game level we need to add a first of all a RigidBody2D component. You can do this by clicking on the player in the hierarchy on the left then on the right click on add component. Add the rigidbody2d.

We going to keep all the default settings for now. If you click on the play button to play the game your player should now start falling.

We now want to just resize our player slightly to fit our level. So let’s just use the handle bars to do that like this.

We want to also make sure we zero out our transform. Just as good practice so that we don’t get any weird effects when we want to add more features to our game later on.

Next thing we want to add is a box collider 2d so that our player has collision detection. To do that add component again and add boxcollider2d.

The default collider settings should work fine. If you find that the box collider is not accurate enough you could switch to a polygon collider 2d.

Great so we have now setup all the unity configuration for our player. We now need a way to control our player. For that we need a player controller script. So let’s head back to our scripts folder and create a c# script called Player.cs.

Unity 2d flappy bird: Player controller script

So to control our player we just need a variable and some basic force methods being called. So very basic and short script. Here is what it is going to look like.

public class Player : MonoBehaviour { public float flyvelocity = 200f; private Rigidbody2D body; // Start is called before the first frame update void Start() { body = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { body.velocity = Vector2.zero; body.AddForce(new Vector2(0, flyvelocity)); } } }

So we first start off with a fly or swim velocity of 200f. We can adjust this in the inspector if it doesn’t look natural. For now we just going to set it to 200. Then we need to get our rigidbody2d so that we can apply physics to it.

In our update method we just check for a mouse click or finger tap. Then we zero out the velocity so that we don’t have any uneven velocity. Then we simply add a force in the y direction to make our player bounce. Now simply drag and drop your script to your player game object.

If we play our game now we should have something like this.

Unity flappy bird: Creating the floor

We want to now just create a floor so that our player doesn’t fall through the world. To do that I just brought in this floor sprite and added a box collider.

If you have done that correctly you should now have a floor which you player can fall onto.

We now need to move to adding our pipes which we will need to dodge. So for our game we will not be propelling our player forward we will rather be spawning pipes which move through our level.

Setting up our pipes

We need to do a few things before our pipes are setup. We will also need to make our pipes a prefab so that we can spawn our pipes with code.

Let’s start by creating a empty game object in our scene and converting that to a prefab. So go ahead and create a empty game object like this.

Rename it to pipes and drag it into our prefabs folder.

Open the prefab by clicking on the arrow next to it in the hierarchy.

Make sure you zero out the transform.

Once you have done that you want to bring in your pipes sprites so that we can start setting something up. So drag in your pipe sprite into the prefab scene viewer.

Make sure you group it under the parent object like this.





We now want to add a boxcollider2d to this object. So on the right click on add component and click on the boxcollider 2d.

Click on pipe on the left again and press ctrl+d to duplicate the pipe.

We now want to space our pipes so that there is a gap between our pipes where the playing can fly or swim through.

Once you have done that we need to consider how we will give the player a score based on the distance they have traveled. The original flappy bird game would allocate score based on how many pipes the player has flown through.

So we need some type of trigger which can tell us when this has happened. So in order to do this we will create a new empty game object under our parent and rename it like this.

On the right you want to add a boxcollider2d component and change our boxcollider to a trigger. You can do this by copying these settings.

We now need to resize our boxcollider to fit the gap between our pipes. So click on edit collider and resize it to look like this.

Great so you can now save the project and close out your prefab. In the main scene we want to resize our prefab to fit our level. To something like this.

Once done we next want to setup a script to handle some movement and more.

Pipe controller script

So we will be moving our pipes in our level using a rigidbody2d. So before we can begin with our script open up the prefab again and add a rigidbody2 on the parent game object. Like below.

You need to change the rigidbody to a kinematic body. Change the settings as per below.

Awesome so let’s create our PipeController script. Go back to your scripts folder and create PipeController.cs.

Drop that onto your root pipe game object in your prefab.

This script needs to do a few basic things, move the pipes through our level at a particular speed. We also need it to clean up after itself so when a pipe is at a certain position in the level it needs to be auto removed.

So here is the script, it’s quite simple. Will explain what everything does.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PipeController : MonoBehaviour { // Start is called before the first frame update public float movespeed = 10f; private Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { rb.velocity = new Vector2(-movespeed, 0); if(this.transform.position.x < -15f) { Destroy(this.gameObject); } } }

So we declare movespeed which defines how quickly our pipes will move through our level. We also declare a rigidbody variable for controller our physics. In the start method we get the pipes rigidbody.

Then in the update method we just set the velocity of the pipe to move in the negative x direction towards our player at the pre defined speed.

If our x value of our pipe is out of view we simply destroy the game object freeing up some memory.

Go ahead and run this to see if your pipes move. In the next part we will be building a spawner which will spawn our pipes for our unity 2d flappy bird game.

You should have something like this now if you run your game.

Creating our Pipe Spawner

Our spawner will just be a basic empty game object. Which will give our spawner a point of reference from where to spawn our pipes.

So in our hierarchy create a empty game object and move it to spot where we want to spawn our pipes from. Like so.

To make our spawner work properly we also want to get rid of the pipes prefab in our hierarchy so you can delete that. We will be spawning pipes with code from here.

Great so we now going to go back to our scripts folder and create a c# script called PipeSpawner.

Add the PipeSpawner script to our Spawner game object.

We can now edit our code. Here is what it will look like.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PipeSpawner : MonoBehaviour { public GameObject pipes; private float variance; public float timeBetweenSpawn; public float elapsedTime; void Start() { elapsedTime = 0.0f; } public void spawnPipes() { variance = Random.Range(-2.0f, 2.0f); Vector3 pos = new Vector3(this.transform.position.x, this.transform.position.y + variance, 1.0f); Quaternion q = this.transform.rotation; Instantiate(pipes, pos, q); } // Update is called once per frame void Update() { elapsedTime += Time.deltaTime; if(elapsedTime > timeBetweenSpawn) { spawnPipes(); elapsedTime = 0.0f; } } }

We going to declare some variables, because we want to spawn our pipes on a time interval. So the first variable we add is elapsedTime. This will keep track of how much time we have waited since the last spawn.

We need a GameObject to be able to store our prefab for our pipes in. We declare a variance which will help us vary how high on the y axis a pipe will spawn. There is also a requirement for timeBetweenSpawn so we can control how often we need to spawn.

This can be quite useful for scaling game difficulty later on. So in the start method we simply set our elapsedTime to 0 as we are counting up to the first spawn.

We create a new method called spawnPipes which will do the following. Create a variance in a random range between -2 and 2. We then create a rotation(Quaternion ) and transform for our pipes based off the transform of our Spawner position and rotation in world space.

We then instantiate our game object which will hold our pipe and set it to the transform we just created and the rotation we just created.

Then in our update method we simply check if our elapsedTime is bigger than our timeBetweenSpawn and we spawn a pipe if this is true and reset the elapsedTime so our timer can start again.

We also add our deltaTime which is frame independent to our elapsedTime to use a timer which matches up with the update method.

Save up your script. Go back to unity and make sure to add your pipe prefab in the gameobject slot on your script like this.

So great that’s how that works. Give that a run and you should end up with something like this.

So we have pipes spawning at different heights. However we have some issues it’s drawing behind our background. As well as our pipes are way too big. So we need to go modify our pipe size in our prefab itself to make it more realistic.

So drag a pipe prefab into your scene to get an idea of size. Open the prefab and resize to your needed size like below.

To fix the background overlap issue go into your prefab and change the sorting layer on each of your pipes. To 1.

Then we need to see where our object are on the z axis as well in order to fix that click on the 2D button up top.

Should now have this with background selected.

Pull it back on the z axis to look something like this.

Click on the 2d button again and play your game.

Should be fixed now.

Next step is to add the scoring system to give us a score to display. Let’s get started on basic UI to finish off this tutorial.

User Interface Setup

To add our score. We will add a canvas and a text object to our hierarchy. Follow the below steps to do this.

Rename the text ui object to ScoreText like this.

We are now just going to add some mock text. So change your text to 0. Like this and change your text color to white. As well as size up your text.

Click on your Canvas object and make sure to switch to screen space – camera.

Then drag and drop your camera into the render camera slot.

Resize your ScoreText etc to make it look like this.

Next we need to create a very basic GameManager which can manage our UI and score for us.

So go over to your scripts folder and create a c# script called GameManager.

So our gamemanager will just have a few variables and a slot where we can add our scoretext ui object. Here is the code.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameManager : MonoBehaviour { public Text scoreText; public static int score; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { scoreText.text = GameManager.score + ""; } }

To bring this into our game we create a empty game object called GameManager and drop our script onto that like this.

Make sure to drop your ScoreText UI element into the open slot as well.

Once you have done that we need a way now to increase our score when the player goes through our score trigger. So for that we need to create another script which we will call Goal.

So go ahead and create that. In that script we will handle the trigger and increase our score. All very simple. It will look like this.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Goal : MonoBehaviour { private void OnTriggerEnter2D(Collider2D col) { GameManager.score++; } }

Attach that script to our ScoreTrigger inside our pipe prefab like this.

Run your game jump through some of the pipes and your score should increase like this.

There we have it we have a fully functional flappy bird clone built in unity from scratch. You can now easily expand this to have sounds, have death animations and restart screens.

Other useful resources

If you are looking for useful YouTube videos be sure to check out my YouTube channel here.

Also check out some of my other tutorials on this blog on unity.

2d top down player movement controllers in unity 2d

Unity spawn prefab at position tutorial

Unity 3D Procedural Terrain Generation

For more Unity tutorials why not check out some of my YouTube videos.

If you liked this tutorial consider sharing it on social media, following me on twitter and subscribing to my YouTube channel. All your support helps me create more useful tutorials like this.