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

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

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

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:
http://docs.unity3d.com/ScriptReference/Mathf.Lerp.html
http://docs.unity3d.com/ScriptReference/Quaternion.Euler.html

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!

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

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

Hi people! My name is Julio and today we are going to start a new Flappy bird tutorial using the 2D tool of Unity 3D 4.6, our target will be Android; the first possible question is: Flappy who? And the answer will be: were you living under a rock? Here is a link to the web game and a little history:
http://flappybird.io
http://mashable.com/2014/02/10/flappy-bird-story/
The first step will be to launch Unity 4.6 and select a 2D new project:
Click on File -> New, select 2D and change the name to FlappyBird:

Now we need to create a new directory, go to projects tab at the bottom, right click -> create -> folder and change the name to img:

Next Step, save this image in your disk and then drag it and drop it inside the img folder in Unity 3D

sprites


It’s time to cut the sprites, click on the Sprites image, and edit it in the inspector tap at right. Then use the automatic slicer to cut the 2D sprites and apply the changes.

We need to create 5 empty objects, to maintain the order in the scene; click on GameObject -> Create Empty, the object will be background, player, pipes, camera and floor, after that drag the Main Camera inside the Camera Object.

We are going to save the scene now, click on Assets and Right Click inside it, then Create -> Folder change the name to “Scenes” then File-> Save Scene, click in the scenes folder and save the scene as flappyScene

Now we are going to add the Background, click on the Sprites image and open it, then select sprites_2 and drag it inside the Scene Tab, go to the inspector window and change de X and Y to 0,0 and change the name to BlueSky

It’s time to set the camera pixels, Click on File->Build Settings then Choose Android and click on Switch Platform; you need the Android SDK, if you don’t have it please follow this instructions before you switch http://docs.unity3d.com/Manual/android-sdksetup.html.
Then click on the game tab (if you don’t see it, click on Window->Game) and choose WVGA Portrait (480×800) in the video I chose different, please select the portrait mode

Let’s duplicate the BlueSky to make a scrolling background, right click on the BlueSky Object select Duplicate, go to the inspector window and change de X to 6.8 and change the name to BlueSky2

It’s time to create the floor, let’s do the same as the Background, click on the Sprites image and open it, then select sprites_5 and drag it inside the Scene Tab, go to the inspector window and change de X and Y to 0,-4.65 and change the name to BrownFloor, drag the BrownFloor inside the Floor Object, then duplicate the BrownFloor to make a scrolling floor, right click on the BrownFloor Object select Duplicate, go to the inspector window and change de X to 6.8 and change the name to BrownFloor 2, in the end change the spriteRenderer change the Order in Layer to 1 in both objects.


Time to make the Flappy Fly, select the Assets Folder in the project tab and create the folder “Animations” then click in the img folder, select the sprite_1 then press CTRL and select sprite_3 and sprite_4, drag them inside the Scene Tab, find the Animations folder and open it and save the animation as FlappyBird, change the name in the hierarchy tab to FlappyBird and drag it inside the player object.


In the inspector tab change the X position of the FlappyBird object to 0, if you click Play you’ll see the Flappy Bird flying, we can smooth the animation in unity 4.6, to do that we need the animation tab, go to Window -> Animation, click in the animation tab, then select the FlappyBird object in the hierarchy tab, you’ll see 3 rhombus with 2D images, select the sprite_3 in the img folder and drag it to the animation, next to the last rhomb, then click the rec button above the animation to stop editing the animation, click play to see the results.

The last part, drag the Sprite_0 to the scene tab, in the inspector change the X and Y position to 9 and -4.65, change the order in layer to 2, change the name to pipe1, drag it inside the pipes object and duplicate it, change the name of the new object to pipe2, rotate the object by changing the Z rotation to -180, change the Y position to 7.5


Finally select the FlappyBird Object and change the “order in layer” to 3 and that’s it, next tutorial we’ll start coding in unity 3D 4.6 (here – Part 2)
Good Luck