Unity 2D Tutorial (ver 4.6) – Flappy Bird part 2

Hi people! My name is Julio and today we’re going to start coding (hurray!!!), for this week we are going to make our Flappy Bird to jump, nice and easy.
But first we need to open the scene we left working, if you are re-opening Unity of course, go to Scene and double-click on flappyScene.Unity.

It’s time to create a folder, click on Assets, then Right Click, choose Create, the folder and change the name to Scripts.

Now the Script, click in the Scripts folder, right click choose Create -> C# Script and change the name to FlappyBirdScript

If you double Click the Script you will see the MonoDevelop IDE open it, you will see something like this:

ide mono develop unity

This is the tool where you are going to code, now replace that code with this:

using UnityEngine;
using System.Collections;

public class FlappyBirdScript : MonoBehaviour {
	//We are going to declare the initial velocity of the bird to be equal to zero
	//vector3.zero = 0,0,0
	private Vector3 velocity = Vector3.zero;
	//Lets declare a vector that controls the gravity, we aren't going to use the Unity's Physics this time
	public Vector3 gravity;
	//It's time to declare the vector which is going to define the jump of the bird
	public Vector3 jumpVelocity;
	//Now we declare a bool that it's going to control the first touch of the screen
	//or if the player pressed the space key
	private bool started = false;
	//We need to declare the maximun velocity of the bird rotation
	public float topRotationVelocity;

	// Use this for initialization
	void Start () {


	// Update is called once per frame
	void Update (){
		//If the player pressed the space key or clicked the mouse for the first time
		if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
			started = true;

	//This is the Update function for the physics, it's slower than the Update Function above
	void FixedUpdate () {
		//We add the gravity to the velocity (So the bird goes down)
		velocity += gravity * Time.deltaTime;

		//If the player pressed the space key or clicked the mouse for the first time
		if (started == true)
			//The game starts only the first time
			started = false;
			//The velocity receive an impulse, and the bird goes up
			velocity.y = jumpVelocity.y;
		//We pass the velocity and change the transform Y position of the bird
		transform.position += velocity * Time.deltaTime;
		//It's time to declare an angle for the bird rotation
		float angle = 0;
		if (velocity.y >= 0) {
			//We change angle, if it is positive the bird'll look up
			angle = Mathf.Lerp (0, 25, velocity.y/topRotationVelocity);
		else {
			//We change angle, if it is negative the bird'll look down
			angle = Mathf.Lerp (0, -75, -velocity.y/topRotationVelocity);
		//Time to rotate
		transform.rotation = Quaternion.Euler (0, 0, angle);

The code above is self explained, but it’s important for you to read this information:

It’s time to make the code works, so select the FlappyBird object in the Hierarchy tab (it’s inside the Player object) and add the script to it. Now in the inspector tab change the gravity’s Y to -8 , the jump velocity’s Y to 4 and the top rotation velocity to 5.

If you hit play and click the bird, you will see it jumping around.

That’s it for this week.

Good Luck!


One thought on “Unity 2D Tutorial (ver 4.6) – Flappy Bird part 2

  1. Pingback: Unity 2D Tutorial (ver 4.6) – Flappy Bird part 1 | Single Tech Games

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s