T echnojs
Already have an account?
login Login

Or,

Don't have an account?
group_add Register

Showing suggestions...

close
cookie
Cookies Consent

This site uses cookies to offer you a better browsing experience.

Unity Scripts: Bird, PipeMiddle, PipeSpawn, PipeMove & Logic

Unity C# Scripting for BirdScript, PipeMiddleScript, PipeSpawnScript, PipeMoveScript, and LogicScript to build the Flappy Bird 2D game.

Create a simple Flappy Bird 2D game using unity C#. It includes backgrounds, pipes and bird to create a game environment where scripting for bird movement, collision detection, scrore updates and game over is done:

  • BirdScript
  • PipeMiddleScript
  • PipeSpawnScript
  • LogicScript

Flappy Bird

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BirdScript : MonoBehaviour
{
    public Rigidbody2D myRigibody;
    public float flapStrength;
    public LogicScript logic;
    public bool birdIsAlive = true;

    void Start() 
    {
        myRigibody = GetComponent<Rigidbody2D>(); 
        // Alternative: you can drag the Rigidbody 2D component to this myRigibody property directly from Inspector

        logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
        birdIsAlive = true;
    }

    void Update() 
    {
        if (Input.GetKeyDown(KeyCode.Space) && birdIsAlive) {
            myRigibody.velocity = Vector2.up * flapStrength;
        }

        if (transform.position.y > 17 || transform.position.y < -17) {
            logic.gameOver();
            birdIsAlive = false;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision) 
    {
        logic.gameOver();
        birdIsAlive = false;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PipeMiddleScript : MonoBehaviour
{
    public LogicScript logic;

    void Start()
    {
        // Don't forget to add Logic tagged to Logic Manager gameobject
        logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
    }

   
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == 3) { // 3 means the layer number that we assign it
            logic.addScore(1);
        }

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PipeSpawnScript : MonoBehaviour
{
    public GameObject pipe;
    public float spawnRate = 2;
    public float heightOffset = 10;
    private float timer = 0;


    void Start()
    {
        spawnPipe();
    }

    void Update()
    {
        if(timer < spawnRate) {
            timer += Time.deltaTime;
        } else {
            spawnPipe();
            timer = 0;
        }
    }

    void spawnPipe()
    {
        float lowestPoint = transform.position.y - heightOffset,
            heighestPoint = transform.position.y + heightOffset;

        Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, heighestPoint)), transform.rotation);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PipeMoveScript : MonoBehaviour
{
    public float moveSpeed = 5;
    public float deadZone = -45;

    void Update()
    {
        transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime;

        if(transform.position.x < deadZone) {
            Destroy(gameObject);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LogicScript : MonoBehaviour
{
    public int playerScrore;
    public Text scroreText;
    public GameObject gameOverScreen;

    [ContextMenu("Increase Score")]
    public void addScore(int scoreToAdd)
    {
        playerScrore = playerScrore + scoreToAdd;
        scroreText.text = playerScrore.ToString();
    }

    public void restartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void gameOver()
    {
        gameOverScreen.SetActive(true);
    }
}
by
question_answer Comments
bar_chart Views
people_alt Contributors
category Category workspaces Group
public Visibility
monetization_on Monetisation
checklist Authorization
save Created
today Updated

Any Question / Leave a comment ?
--- Thank you for your attention! ---

Related Posts