This time I am going to share a tutorial to make a game like "Flappy Bird". You can download the project from dropbox link or you can go through the following tutorial to get the jist of it.
Lets start by making a 2d Game in untiy 4.x. Follow the tutorial and you will be ready to make a game like this one.
1. Make a 2d project and name it what ever you like,as I named it as Flappy Bird.
2. Second step is very important always I am repeating it always organize your projects it helps in any kind of modification. So make new folders as Prefabs, Scenes, Scripts and Textures. We will add more folders if required. So go and make folders.
3. First of all look for a wallpaper for you game. Do not judge mine as it was a random pick haha. So lets move forward - Copy the image into textures folder and drag it to the screen area(Scale it to fit the screen in my case it is x=2.4 and y=1.4)
4. Now it's time to make our bird goto google and search anything like "flappy bird png" , "flappy bird tga" or "flappy bird texture" you will surely find something of use, Copy the image to the textures folder and drag that one too to the scene. My image is hi-res that is why I have scaled it down. Name it as "Bird"(We are not using any sprite sheet because purpose of the tutorial is somewhat different I will cover that part in some other tutorial).
5. Lets add some physics to our game, add the component "Rigidbody 2D" to the "Bird". Save the scene in Scenes folder and press play and it should fall.
6. Lets make the jump script. Make a new C# script in Scripts folder and name it as Jump. Code is as follows
Lets start by making a 2d Game in untiy 4.x. Follow the tutorial and you will be ready to make a game like this one.
1. Make a 2d project and name it what ever you like,as I named it as Flappy Bird.
2. Second step is very important always I am repeating it always organize your projects it helps in any kind of modification. So make new folders as Prefabs, Scenes, Scripts and Textures. We will add more folders if required. So go and make folders.
3. First of all look for a wallpaper for you game. Do not judge mine as it was a random pick haha. So lets move forward - Copy the image into textures folder and drag it to the screen area(Scale it to fit the screen in my case it is x=2.4 and y=1.4)
4. Now it's time to make our bird goto google and search anything like "flappy bird png" , "flappy bird tga" or "flappy bird texture" you will surely find something of use, Copy the image to the textures folder and drag that one too to the scene. My image is hi-res that is why I have scaled it down. Name it as "Bird"(We are not using any sprite sheet because purpose of the tutorial is somewhat different I will cover that part in some other tutorial).
5. Lets add some physics to our game, add the component "Rigidbody 2D" to the "Bird". Save the scene in Scenes folder and press play and it should fall.
6. Lets make the jump script. Make a new C# script in Scripts folder and name it as Jump. Code is as follows
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
// Use this for initialization
public Rigidbody2D bird;
void Start () {
}
//Update is called once per frame
void Update () {
if(Input.GetKeyUp("space"))
{
bird.velocity=Vector2.zero;
bird.AddForce(new Vector2(0,250));
}
}
}
Script is easy to understand it checks for the input. Once it get the input it will turn the velocity of our "Bird" to zero and add the upward force for jump.
Make an empty Gameobject name it what ever you like and attach the script to it or you can attach the script to the main camera(I have used the later approach). At last add the "Bird" gameobject to the instance if the script.
7.Next part is making obstacles for the game. Search something like "mario pipe png" in google images you will find the obstacles. Create an empty Game object in the editor. Name it as "Obstacles pair" drag the png you just downloaded to the textures folder. Now add it 2 time to your scene. Make them children of "Obstacles pair" name one as "obstacle1" and other as "obstacle2". Now final step is rotating the obstacle. In my case I had to give "obstacle1" rotation of 180 degrees at z axis. And you should give the distance of your bird+0.3 (or more) between your obstacles so that bird can pass between them. Your game should look like this
8. Now you have to attach a rigidbody to the obstacles so that you can give it some velocity. Now here are the actions you have to take to complete this phase.
- add a component of "Rigidbody2D" to the parent object and mark it as "is Kinematic". Anything which is marked as "is Kinematic" will stop responding to gravity.
}
// Update is called once per frame
void Update () {
}
}- Create a new C# script in your scripts folder and name it as "MoveObstacle". Code is given below
using UnityEngine;
using System.Collections;
public class MoveObstacle : MonoBehaviour {
public Vector2 velocity = new Vector2(-3, 0);
// Use this for initialization
void Start () {
rigidbody2D.velocity=velocity; }
// Update is called once per frame
void Update () {
}
} - Script is self explanatory you make a new velocity variable and apply that velocity to obstacle pair gameobject. As it is not a sidescoller game so we don't have to move environment and player we just have to move obstacles. Apply this script to "Obstacles pair" gameobject.
- First of all make an empty prefab name it as "ObstaclesPair".
- Drag the "Obstacles pair" gameobject from the scene onto it.
- Now make a new C# script named as "GenerateObstacle" in Scripts folder and attach it to Main Camera as it will generate obstacles with the passage of time. Bode is below(I am not showing randomness in heights as its your work to do :D )
using UnityEngine;
public class GenerateObstacle : MonoBehaviour
{
public GameObject obstales;
public bool gen=true;
public float seconds=0;
public int updateCounter=0;
public int maxRange=170;
// Use this for initialization
void Start()
{
}
void Update()
{
if(gen)
{
gen=false;
Invoke("GenerateNewObstacle" , Random.Range(4,8));
}
else
updateCounter++;
if(updateCounter >= maxRange)
{
gen=true;
updateCounter=0;
}
}
void GenerateNewObstacle()
{
Instantiate(obstales);
}}
- Apply Box collider 2D to the Bird gameobject.
- Appy polygon collider 2D to the both of obstacles and update your Prefab for obstacles.
- on collision of birds Restart the level.
11. Download the project to see how does all of it works.