如何使用 Unity 制作一款受《Flappy Bird》启发的游戏

在本 Unity 教程中,我们将逐步介绍创建 Flappy Bird 游戏 的过程。这款经典手机游戏涉及引导一只小鸟穿过一系列管道,方法是点击使其拍打翅膀并避开障碍物。让我们深入了解分步说明。

步骤 1:设置您的 Unity 项目

  • 如果还没有,请打开Unity并创建一个新的 2D 项目
  • 设置您的项目设置,包括分辨率和平台目标。

第 2 步:导入游戏资产

  • 为鸟、管道和背景查找或创建资产。
  • 将这些资产导入您的 Unity 项目。

步骤 3:创建 Flappy Bird

  • 为鸟添加 2D 精灵。
  • 实现简单的点击控制,让小鸟振翅飞翔。
  • 施加重力使鸟自然落下。

步骤 4:设计管道

  • 使用 2D 精灵创建管道 prefab
  • 设置生成系​​统以定期生成管道。

第五步:实现游戏逻辑

  • 添加成功穿过管道的计分系统。
  • 实现碰撞检测,当鸟撞到管道或地面时结束游戏。

检查下面的脚本,它封装了第 3、4 和 5 部分。

'FlappyBird.cs'

using UnityEngine;
using System.Collections.Generic;

public class FlappyBird : MonoBehaviour
{
    public float jumpForce = 5f;
    public Transform pipeSpawnPoint;
    public GameObject pipePrefab;
    public float pipeSpawnInterval = 2f;
    public float pipeSpeed = 2f;

    private Rigidbody2D rb;
    private Transform mainCameraTransform;

    private List<GameObject> pipes = new List<GameObject>();

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        mainCameraTransform = Camera.main.transform;

        // Start spawning pipes
        InvokeRepeating("SpawnPipe", 2f, pipeSpawnInterval);
    }

    void Update()
    {
        // Flap when the screen is tapped or clicked
        if (Input.GetMouseButtonDown(0))
        {
            Flap();
        }

        // Move towards the pipes
        transform.Translate(Vector3.right * pipeSpeed * Time.deltaTime);

        // Move and manage spawned pipes
        foreach (GameObject pipe in pipes)
        {
            if (pipe != null)
            {
                pipe.transform.Translate(Vector3.left * pipeSpeed * Time.deltaTime);

                // End the game when colliding with pipes or ground
                if (pipe.CompareTag("Pipe") && IsCollidingWithPipe(pipe))
                {
                    EndGame();
                    return; // Exit the loop and update immediately
                }

                if (pipe.CompareTag("Ground") && IsCollidingWithGround(pipe))
                {
                    EndGame();
                    return; // Exit the loop and update immediately
                }

                // Remove pipes that are out of camera view
                if (pipe.transform.position.x < mainCameraTransform.position.x - 10f)
                {
                    Destroy(pipe);
                    pipes.Remove(pipe);
                    break; // Exit the loop to avoid modifying a collection while iterating
                }
            }
        }
    }

    void Flap()
    {
        // Apply force to make the bird jump
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    }

    void SpawnPipe()
    {
        GameObject newPipe = Instantiate(pipePrefab, pipeSpawnPoint.position, Quaternion.identity);
        pipes.Add(newPipe);
    }

    bool IsCollidingWithPipe(GameObject pipe)
    {
        Collider2D pipeCollider = pipe.GetComponent<Collider2D>();
        return pipeCollider != null && pipeCollider.bounds.Intersects(GetComponent<Collider2D>().bounds);
    }

    bool IsCollidingWithGround(GameObject ground)
    {
        Collider2D groundCollider = ground.GetComponent<Collider2D>();
        return groundCollider != null && groundCollider.bounds.Intersects(GetComponent<Collider2D>().bounds);
    }

    void EndGame()
    {
        // Implement game over logic (e.g., display score, restart menu)
        Debug.Log("Game Over!");
    }
}

提供的 Unity 脚本代表简化的 Flappy Bird 游戏,其中玩家控制的小鸟在滚动环境中导航。 小鸟可以根据用户输入跳跃,游戏会检查与管道和地面的碰撞,如果检测到则触发游戏结束。 管道会定期动态生成并向玩家移动。 该脚本包括移除超出摄像头视野的管道以优化性能的逻辑。 'EndGame' 函数在发生碰撞时被调用,它可以扩展以处理各种游戏结束场景,例如显示分数或重新开始游戏。 该代码旨在在 Unity 环境中提供 Flappy Bird 机制的基本实现。

步骤 6:用户界面和菜单

  • 设计一个用于显示分数的 UI。
  • 创建 menus 用于启动和重启游戏。

第 7 步:微调游戏玩法

  • 调整游戏physics和速度以获得平衡和愉快的体验。
  • 测试并迭代您的游戏以确保游戏流畅且富有挑战性。

步骤 8:添加音效

  • 导入或创建拍打、得分和碰撞的声音效果。
  • 将这些音效集成到您的游戏中。

在 'FlappyBird.cs' 中添加音效的修改示例:

using UnityEngine;
using System.Collections.Generic;

public class FlappyBird : MonoBehaviour
{
    // Existing variables...

    public AudioClip jumpSound;
    public AudioClip collisionSound;
    public AudioClip gameOverSound;

    private AudioSource audioSource;

    void Start()
    {
        // Existing Start() code...

        // Add AudioSource component and reference
        audioSource = gameObject.AddComponent<AudioSource>();
    }

    void Flap()
    {
        // Apply force to make the bird jump
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);

        // Play jump sound
        audioSource.PlayOneShot(jumpSound);
    }

    void EndGame()
    {
        // Play game over sound
        audioSource.PlayOneShot(gameOverSound);

        // Implement other game over logic...
    }

    // Existing code...
}

步骤 9:构建和部署

  • 为你的目标平台(iOS、Android 等)构建游戏。
  • 在您选择的设备或模拟器上部署并测试。

结论

本教程介绍了在 Unity 中重现这款经典 Flappy Bird 游戏 的基本步骤。尝试使用附加功能和改进功能来打造您自己的游戏。祝您游戏开发愉快!