2D Flappy Bird Clone Game Tutorial – Unity3D (C#)
I guess you’ve played this funny game. It’s the most downloaded mobile game in January, 2014. Is it hart to implement? The answer is no. Most probably, it’s the easiest and quickest game to implement ever. You just need to touch on screen to make the bird jump and avoid the obstacles. That’s all!
We will change the touch action with assigning a key. Lets begin.
At first, create a 2D project in Unity. You can find the sprite sheet on Google for the visual objects. Create a basic animation for the bird. To do it, download the sprite sheet from Google and add it as a new asset to your project. Choose “Multiple” as sprite mode and open the sprite editor. In editor, rename the objects that will be used in your scene(birds, ground and obstacles).
Apply the changes and close the editor. Now, your sprite sheet is divided to multiple images. Select bird1, bird2 and bird3 and drag them in scene.
Save the animation. Also, add the ground, obstacles and change the color of camera background. When you press play, you see the scene with an animated bird.
Now, we need to add some physics. Add Collider2D (circle for the bird and box for the others) to all objects and add Rigidbody2D (select fixed angle) to the bird.
That’s how my scene looks like.
Create a C# script named BirdAction. We will add a horizontal speed to the bird and make it jump when you press the space key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
private float speed = 0.01f; private float jump = 0.1f; void Start () { GetComponent<Rigidbody2D> ().velocity = Vector2.right * speed / 10; } void Update () { if (Input.GetKeyDown (KeyCode.Space)) { GetComponent<Rigidbody2D> ().AddForce (Vector2.up * jump / 3); } } void OnCollisionEnter2D(Collision2D c) { Application.LoadLevel(Application.loadedLevel); } |
Speed means movement speed and jump means jumping force. You can change gravity, mass, speed and jump variables to get the best performance for your game. Also, you can change the dividing values in start and update functions.
Lets create a simple following script for camera.
1 2 3 4 5 6 |
public Transform bird; void Update () { transform.position = new Vector3(bird.position.x, transform.position.y, transform.position.z); } |
We want to make camera to follow only horizontal direction. So, just take the x position of the bird. That’s all! Enjoy it.
You can download the source code here.
©Coffee Break Codes – 2D Flappy Bird Game Tutorial – Unity3D (C#)
Pingback: Video tutorials: How to make Flappy Bird clone Mobile App game? | How To Mobile App
Just a noob question about code – why do you divide vectors by those numbers in general? It would work without it right?
Of course it works. Its just a balance for moving action. You can change or remove them but it may be a little bit faster 🙂
Question, can you add a script for the pipes generator? Thx
Thx bro, very helpful
I feel I did not enjoy it as it motivates retailers to talk
gamers to offer recommendations.
I like blues bands! I really do! And my favourite blues band is Johnnyswim! The members Abner Ramirez and Amanda Sudano are about to perform more than 40 concerts to their fans in 2019 and 2020! To know more about Johnnyswim band in 2020 visit site Johnnyswim tour 2020. You won’t miss any show by Johnnyswim this year if you click on the link!
I like EDM songs! I really do! And my favourite EDM band is Chainsmokers! DJs Andrew Taggart and Alex Pall are about to give more than 50 concerts for their fans in 2019 and 2020! To know more about Chainsmokers band in 2019 visit site Chainsmokers tour Washington. You won’t miss concerts in 2019 if you visit the link!