Unity 中的迷你游戏 | CUBEavoid
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 步:设置
创建两个主要脚本后,让我们继续设置游戏:
- 如果尚未创建新场景
- 选择主摄像头,将其位置更改为 (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
让我们创建一个简单的 UI:
- 创建新的 UI 文本(游戏对象 -> UI -> 文本),将其重命名为 "GameOverText"
- 确保新文本的 RectTransform 对齐方式设置为“中间中心”
- 将文本位置 X 和位置 Y 设置为 0
- 将高度改为 100
- 对于文本组件,设置以下文本(确保选中富文本属性):
Game Over
<size=15>Click to Try Again</size>
- 将字体大小设置为 25
- 将文本对齐方式设置为居中
- 将文本颜色设置为红色
最后,让我们分配脚本:
- 选择 "Player" 立方体并 assign 将 SC_PlayerCube 脚本分配给它
- 将 "Enemy" 立方体分配给 Enemy 变量
- 将 "GameOverText" 分配给 Game Over Text 变量
- 选择 "Enemy" 立方体并将 assign SC_EnemyCube 脚本分配给它
现在按下“播放”按钮时,蓝色立方体应该开始在屏幕上移动,您需要使用鼠标光标调整黑色立方体的大小来避免这种情况。
请随意以任何方式改进这个游戏。