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);
}
}
Any Question / Leave a comment ?
--- Thank you for your attention! ---