유니티 C# 간단한 플래피 버드 게임 만들기 간단 구현 Flappy Bird

반응형

Bird 코드

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

public class Bird : MonoBehaviour
{
    public float upForce = 200f;       // 상향 힘
    private bool isDead = false;       // 새의 생존 여부
    private Rigidbody2D rb2d;          // Rigidbody2D 컴포넌트

    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (isDead == false && Input.GetMouseButtonDown(0))
        {
            rb2d.velocity = Vector2.zero;
            rb2d.AddForce(new Vector2(0, upForce));
        }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        rb2d.velocity = Vector2.zero;
        isDead = true;
        // 게임 오버 로직 추가 가능
    }
}

 

Pipe 코드

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

public class Pipe : MonoBehaviour
{
    public float speed = 2f;           // 파이프 이동 속도

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

        if (transform.position.x < -10)
        {
            Destroy(gameObject);       // 화면 밖으로 나가면 파이프 삭제
        }
    }
}

 

Pipe Spawner 코드

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

public class PipeSpawner : MonoBehaviour
{
    public GameObject pipePrefab;       // 파이프 프리팹
    public float spawnRate = 3f;        // 파이프 생성 간격
    public float heightOffset = 1f;     // 파이프 높이 오프셋

    private float timer = 0f;

    void Start()
    {
        SpawnPipe();
    }

    void Update()
    {
        timer += Time.deltaTime;

        if (timer >= spawnRate)
        {
            SpawnPipe();
            timer = 0f;
        }
    }

    void SpawnPipe()
    {
        float yPos = Random.Range(-heightOffset, heightOffset);
        Vector3 spawnPosition = new Vector3(transform.position.x, yPos, 0);
        Instantiate(pipePrefab, spawnPosition, Quaternion.identity);
    }
}

 

GameManager 코드

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

public class GameManager : MonoBehaviour
{
    public void GameOver()
    {
        // 게임 오버 로직
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);  // 현재 씬을 다시 로드하여 게임을 재시작합니다.
    }
}
반응형