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

Hi people! Long time no see :P, I was a little busy this days traveling, well today we’re going to start making the pipes move, so we can produce an illusion of the flappy bird going forward, but the sad true is that the bird doesn’t even move.

The first step is to duplicate the pipes, right click on the Pipes object and Duplicate and change the name of the new object to Pipes1 & Pipes2, then change the X position of the Pipes2 to 6.

Create a new Script, PipesScript, and open MonoDevelop to start coding:

using UnityEngine;
using System.Collections;

public class PipesScript : MonoBehaviour {
    //Velocity of the pipe
    public Vector3 velocity;
    //Distance Between Pipes1 & Pipes2
    public Vector3 distanceBetweenColumns;
    void Update () {
        movePipe ();

    //Move the pipes
    private void movePipe()
        //The pipes are going from Right To Left
        this.transform.position = this.transform.position + (velocity*Time.deltaTime);

        //If the Pipe reach the end of the Screen
        if (this.transform.position.x <= -13.5f)
                //if not, increase the distance between Columns when we reach the -13.5 X position
                Vector3 TemPosition = this.transform.position + distanceBetweenColumns;
                //Change the Y position to a Random One.
                TemPosition.y = Random.Range (-3f, 0.6f);
                //And the move the Pipes to that position.
                this.transform.position = TemPosition;

Then add the Script to the Pipes1 & Pipes2 Objects. Change the X Velocity to -2 and Distance Between Columns to 12 in both objects

Now we have something like this:

It’s time to make Flappy hit the Pipes, in order to do that you need to add a RigidBody to the Flappy Bird, Click on the Flappy Object, then Add Component, Physics 2D, RigidBody 2D and change the Mass and the gravity to 0.

Now add a Box Collider 2D (Like the real Flappy Bird), Click on the Flappy Object, then Add Component, Physics 2D, Box Collider 2D.

It’s time to code again, open the FlappyBird Script and add 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;

    public PipesScript pipe1;
    public PipesScript pipe2;
    // 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);

    //Every time Flappy hits an object, the collider is going to active this function
    //Collider: Box Collider 2D, Circle Collider 2D, and so on.
    void OnCollisionEnter2D (Collision2D colision)
        //If we hit a pipe or the floor, stop the game
        if(colision.gameObject.name == "Pipe1" | colision.gameObject.name == "Pipe2"|
           colision.gameObject.name == "BrownFloor"| colision.gameObject.name == "BrownFloor2")
            //the pipes velocity become 0
            pipe1.velocity = new Vector3(0,0,0);
            pipe2.velocity = new Vector3(0,0,0);

Now we need to add some colliders to the pipes and floor, click on Pipe1 and Pipe2 from both Pipes, BrownFloor, BrownFloor2 and add a box Collider 2D.

And Finally add the Pipes to the FlappyBirdScript, Click the Flappy Bird Object and drag both pipes to the Script.

If you try the game you are going to see Flappy searching for oil, that’s ok for now.

Good Luck


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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s