Flappy Bird: Part 1

Helloo World
5 min readNov 6, 2020

--

Learn how to recreate Flappy Bird with C and Unity from Mari, a member of our software team!

Today I will be showing you how to make Flappy Bird using Unity and C#!

You can access the source code of the project here.

To start things off, we’re going to create a new 2d project from the Unity Hub. Click create, and then Unity will open up on its own.

For this project, we will be needing graphics for the player and the obstacles. To use your graphics, you want to open up their file location and then drag them into the Assets folder located in the Projects section.

This allows us to place these graphics in our scene as we please, creating a new GameObject with that graphic.

To view all the objects in our scene, we go to the Hierarchy. Click on the object you just dragged into the scene. This is going to be our player.

Now that our player is in the scene, we want to have gravity act on it. In the Inspector, click on the “Add Component” button. Type Rigidbody2D and then click to add it to the player.

This component is what allows physics to act on this object. I will adjust gravity to 5 to make the object fall faster, but you can change the value as you see fit.

Next we want to be able to move the player upwards every time we click. Create a new C# script and attach it to the player. Open this script with an editor of your choice (I’m using VS Code).

Within our script we want to create two public variables. One is a float, which I will call distance. The other is a Rigidbody2d, which I named rb for short.

Rather than initializing these variables within the script, I’ll be doing so in the Inspector after we finish the code for the movement.

To move upwards, we want to make use of the update function. Within this function, create an if statement to check if the mouse button is down. This allows the code to only run when the game detects a click.

Inside the if statement, we want to change the velocity of the player’s rigidbody. We will set it to an upwards vector and multiply it by the distance variable we created.

Back in Unity, we can see the code we wrote in the Inspector. Drag the Rigidbody2D to the variable we declared earlier. We can also set the distance here.

That is it for the player movement! Feel free to delete the start function that auto-generates whenever you make a new C# script, since it is not being used.

-

Next we need to make the obstacles that move towards the player. Create an empty GameObject and place it on the right of the scene, where you want the obstacles to come from. This will be our spawner.

Add a new C# script to the spawner. Create two GameObject variables, one for the spawner itself and one for the obstacles.

When our spawner creates an obstacle, we want there to be a delay before it creates the next one. Because of this, we will take advantage of something called a coroutine. Coroutines allow us to place delays in between lines of code, so rather than spawning obstacles within the update function, we will just start the scene with a coroutine that calls itself over and over again.

We also want the obstacles to have varying heights, so we will include a random generator with a range of whatever y-coordinates fit your scene.

To make the obstacles themselves, we will take advantage of the instantiate function.

Back in Unity, we need to create the GameObject that will be our obstacle. However, we do not want the obstacle to remain in the scene and instead just want an instance of it. After creating it, drag the obstacle from the Hierarchy to the Assets folder, thus creating a prefab of it. Once the prefab is made, delete the version that is in the Hierarchy.

Click on the spawner and go to the Inspector. Where it shows the code we made for the spawner, drag the prefab into the obstacle variable. Drag the spawner itself from the Hierarchy into the spawner variable.

To move the obstacles left, we need to attach a C# script to the prefab.

In the update function, we will use the translate function to translate the object over a set number of x-coordinates. Set the x-coordinate to a negative number.

--

--

Helloo World

We’re changing the world with code, and also running a blog now, apparently. Catch us at helloo-world.com!