Unity 中的迷你游戏 | 立方体避免

CUBEavoid 是一款在 Unity 制作的迷你游戏。源代码,并在下面进行设置。

目标是通过用鼠标光标重新缩放大立方体来避开小立方体。

第 1 步:创建所有必要的脚本

  • 创建一个新脚本,将其命名为 SC_PlayerCube.cs,删除其中的所有内容,然后将以下代码粘贴到其中:

SC_PlayerCube.cs

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

public class SC_PlayerCube : MonoBehaviour
{
    //Assign enemy mesh renderer
    public MeshRenderer enemy;
    public Text gameOverText;

    Transform thisT;
    MeshRenderer mr;

    //Global static variable
    public static bool GameOver = false;

    // Start is called before the first frame update
    void Start()
    {
        thisT = transform;
        mr = GetComponent<MeshRenderer>();
        gameOverText.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (GameOver)
            return;

        if (gameOverText.enabled)
        {
            //Game has resumed, disable game over text
            gameOverText.enabled = false;
        }

        //Scale player cube with mouse movement
        Vector3 playerScale = (new Vector3(Screen.width / 2 - Input.mousePosition.x, 1, Screen.height / 2 - Input.mousePosition.y)).normalized * 10;
        //Keep Y scale at 10
        playerScale.y = 10;
        //Limit minimum X and Z scale to 0.1
        if (playerScale.x >= 0 && playerScale.x < 0.1f)
        {
            playerScale.x = 0.1f;
        }
        else if (playerScale.x < 0 && playerScale.x > -0.1f)
        {
            playerScale.x = -0.1f;
        }
        if (playerScale.z >= 0 && playerScale.z < 0.1f)
        {
            playerScale.z = 0.1f;
        }
        else if (playerScale.z < 0 && playerScale.z > -0.1f)
        {
            playerScale.z = -0.1f;
        }
        thisT.localScale = playerScale;

        //Check if enemy have intersected with the player, if so, stop the game
        if (mr.bounds.Intersects(enemy.bounds))
        {
            GameOver = true;
            gameOverText.enabled = true;
        }
    }
}
  • 创建一个新脚本,将其命名为 SC_EnemyCube.cs,删除其中的所有内容,然后将以下代码粘贴到其中:

SC_EnemyCube.cs

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

//This script controls enemy cube AI
public class SC_EnemyCube : MonoBehaviour
{
    //Private variables
    Camera mainCamera;
    float movementTime = 0;
    Vector3 startPoint;
    Vector3 endPoint;

    // Start is called before the first frame update
    void Start()
    {
        //Get camera tagged "MainCamera"
        mainCamera = Camera.main;
        GenerateStartEndPoint();
    }

    //Assign start and end points slightly outside the Camera view
    void GenerateStartEndPoint()
    {
        Vector3 relativeStart;
        Vector3 relativeEnd;

        //Randomly pick whether to go Left <-> Right or Up <-> Down
        if (Random.Range(-10, 10) > 0)
        {
            relativeStart = new Vector3(Random.Range(-10, 10) > 0 ? 1.1f : -0.1f, Random.Range(0.00f, 1.00f), mainCamera.transform.position.y);
            if (relativeStart.y > 0.4f && relativeStart.y < 0.6f)
            {
                if(relativeStart.y >= 0.5f)
                {
                    relativeStart.y = 0.6f;
                }
                else
                {
                    relativeStart.y = 0.4f;
                }
            }
            relativeEnd = relativeStart;
            relativeEnd.x = relativeEnd.x > 1 ? -0.1f : 1.1f;
        }
        else
        {
            relativeStart = new Vector3(Random.Range(0.00f, 1.00f), Random.Range(-10, 10) > 0 ? 1.1f : -0.1f, mainCamera.transform.position.y);
            if (relativeStart.x > 0.4f && relativeStart.x < 0.6f)
            {
                if (relativeStart.x >= 0.5f)
                {
                    relativeStart.x = 0.6f;
                }
                else
                {
                    relativeStart.x = 0.4f;
                }
            }
            relativeEnd = relativeStart;
            relativeEnd.y = relativeEnd.y > 1 ? -0.1f : 1.1f;
        }

        //Convert screen points to world points
        startPoint = mainCamera.ViewportToWorldPoint(relativeStart);
        endPoint = mainCamera.ViewportToWorldPoint(relativeEnd);

        //Reset movement time
        movementTime = 0;
    }

    // Update is called once per frame
    void Update()
    {
        //Game over, wait for click
        if (SC_PlayerCube.GameOver)
        {
            //Click to resume
            if (Input.GetMouseButtonDown(0))
            {
                SC_PlayerCube.GameOver = false;
                GenerateStartEndPoint();
            }
            else
            {
                return;
            }
        }

        //Move enemy from one side to the other
        if(movementTime < 1)
        {
            movementTime += Time.deltaTime * 0.5f;

            transform.position = Vector3.Lerp(startPoint, endPoint, movementTime);
        }
        else
        {
            //Re-generate start / end point
            GenerateStartEndPoint();
        }
    }
}

第 2 步:设置

创建 2 个主要脚本后,让我们继续设置游戏:

  • 如果您还没有创建一个新场景,请创建一个
  • 选择主摄像机,将其位置更改为 (0, 10, 0),将其旋转更改为 (90, 0, 0)
  • 更改主相机的相机组件属性:清除标志为 'Solid Color',背景为 'white',投影为 'Orthographic',尺寸为 '10'

  • 创建新的立方体(游戏对象 -> 3D 对象 -> 立方体)并为其命名 "Player"
  • 将 "Player" 位置更改为 (0, 0, 0) 并缩放为 (10, 10, 10)
  • 创建新材质(右键单击项目文件夹 -> 创建 -> 材质)并为其命名 "PlayerMaterial"
  • 将 "PlayerMaterial" Shader 更改为 Unlit/Color 并将其颜色更改为黑色

  • 将 "PlayerMaterial" 分配给 "Player" 立方体
  • 复制 "Player" 立方体并将其重命名为 "Enemy"
  • 将 "Enemy" 比例更改为 (0.7, 0.7, 0.7)
  • 复制 "PlayerMaterial" 并将其重命名为 "EnemyMaterial"
  • 将 "EnemyMaterial" 十六进制颜色更改为 157EFB
  • 最后,将 "EnemyMaterial" 分配给 "Enemy" Cube

CUBE避免场景视图

让我们创建一个简单的 UI:

  • 创建新的 UI 文本(游戏对象 -> UI -> 文本),将其重命名为 "GameOverText"
  • 确保新文本的 RectTransform 对齐方式设置为 Middle Center
  • 将文本位置 X 和位置 Y 设置为 0
  • 将高度更改为 100
  • 对于文本组件,设置以下文本(确保选中富文本属性):
Game Over
<size=15>Click to Try Again</size>
  • 将字体大小设置为 25
  • 将文本对齐方式设置为中间居中
  • 将文本颜色设置为红色

CUBE避免文字游戏

最后,让我们分配脚本:

  • 选择 "Player" 立方体并为其分配 SC_PlayerCube 脚本
  • 将 "Enemy" 立方体分配给 Enemy 变量
  • 将 "GameOverText" 分配给 Game Over Text 变量

  • 选择 "Enemy" 多维数据集并为其分配 SC_EnemyCube 脚本

现在,当按下“播放”时,蓝色立方体应该开始在屏幕上移动,您需要使用鼠标光标调整黑色立方体的大小来避免这种情况。

请随意以任何方式改进这个游戏。

推荐文章
Unity 无尽跑者教程
Unity 中的三消益智游戏教程
Unity 中的迷你游戏 | 飞扬的立方体
在 Unity 中创建 2D 打砖块游戏
在 Unity 中创建滑动益智游戏
如何在 Unity 中制作一款受 Flappy Bird 启发的游戏
农场僵尸 | 在 Unity 中制作 2D 平台游戏