Flappy bird game

Flappy bird assets

// PlayerMovement
// Rigidbody2D gravity and mass set to 2

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D rb;
    float jumpForce = 10f;
    float speed = 4f;

    void Awake() => rb = GetComponent<Rigidbody2D>();
    void Start()
    {
        rb.velocity = new Vector2(speed, rb.velocity.y);
    }

    void Update()
    {
        if (Input.GetButtonDown("Fire1")) Jump();
    }

    void Jump() => rb.velocity += new Vector2(0f, jumpForce);
}
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    [SerializeField] Transform target;
    Vector3 offset;
    Vector3 tempPos;
    void Start() => offset = transform.position - target.position;

    void LateUpdate()
    {
        if (target != null)
        {
            tempPos = target.position + offset;
            tempPos.y = 0f;
            transform.position = tempPos;
        }
    }
}
// Ground

using UnityEngine;

public class Ground : MonoBehaviour
{
    SpriteRenderer spriteRenderer;

    void Awake() => spriteRenderer = GetComponent<SpriteRenderer>();

    private void OnTriggerEnter2D(Collider2D collision)
    {
        MoveForward();
    }

    void MoveForward()
    {
        float width = spriteRenderer.size.x;
        transform.localPosition += new Vector3(width * 2, 0f, 0f);
    }
}
// Pipe

using System.Collections;
using UnityEngine;

public class Pipe : MonoBehaviour
{
    PointsUI pointsUi;
    float pipeDistance = 5f;
    private void Start() => pointsUi = FindObjectOfType<PointsUI>();

    void OnTriggerEnter2D(Collider2D collision)
    {
        pointsUi.AddPoint();
        StartCoroutine(PreparePipe());
    }

    IEnumerator PreparePipe()
    {
        yield return new WaitForSeconds(0.5f);
        float height = Random.Range(-1.8f, 1.4f);
        transform.localPosition = new Vector3(transform.localPosition.x + 5 * pipeDistance, height, 0f);
    }
}
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public void OnGameOver()
    {
        SceneManager.LoadScene(0);
    }
}
using UnityEngine;

public class PlayerCollision : MonoBehaviour
{
    [SerializeField] GameManager gameManager;

    void OnCollisionEnter2D(Collision2D collision)
    {
        gameManager.OnGameOver();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class PointsUI : MonoBehaviour
{
    [SerializeField] TMP_Text txtPoints;
    int points = 0;

    public void AddPoint()
    {
        points++;
        txtPoints.text = points.ToString();
    }
}
Scroll to Top