本文是Unity3D貪吃蛇游戲從制作到部署的相關(guān)細(xì)節(jié)
項目開源代碼:https://github.com/zstar1003/3D_Snake
試玩鏈接:http://xdxsb.top/Snake_Game_3D
效果預(yù)覽:
試玩鏈接中的內(nèi)容會和該效果圖略有不同,后面會詳細(xì)說明。
游戲規(guī)則
經(jīng)典貪吃蛇游戲:蛇身隨著吃食物的增加不斷變長,通過A/D或方向鍵←→控制方向,蛇頭撞在蛇身上或四周墻壁會導(dǎo)致游戲失敗。
蛇身控制和碰撞檢測
蛇身控制和碰撞檢測的邏輯寫在SnakeController.cs
文件中。
蛇頭運動的思路是將蛇頭不斷朝forward
的方向前進(jìn),前進(jìn)速度等于速度數(shù)值x當(dāng)前時間。同時通過一個list來記錄蛇頭運動的歷史軌跡,蛇身通過該軌跡進(jìn)行運動。
為了區(qū)分延申出來的蛇身是初始蛇身還是新延申的蛇身,對新延申的蛇身打上Block標(biāo)簽,不進(jìn)行區(qū)分則會導(dǎo)致剛開始碰撞即觸發(fā)蛇頭蛇身碰撞,導(dǎo)致游戲結(jié)束。
完整代碼:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SnakeController : MonoBehaviour
{
// 設(shè)置
public float moveSpeed = 5f;
public float steerSpeed = 180f;
public float bodySpeed = 5f;
public int Gap = 10;
// 預(yù)制體
public GameObject bodyPrefab; //身體組件
// 身體組件集合
private List<GameObject> _bodyParts = new List<GameObject>();
private List<Vector3> _positionHistory = new List<Vector3>();
//音樂控制器
public AudioController audioController;
private void Start()
{
addBodyPart();
audioController = GameObject.FindGameObjectWithTag("Audio").GetComponent<AudioController>();
}
private void Update()
{
// 向前移動
transform.position += transform.forward * moveSpeed * Time.deltaTime;
// 方向操控
float steerDirection = Input.GetAxis("Horizontal"); // 返回值從 -1 到 1
transform.Rotate(Vector3.up * steerDirection * steerSpeed * Time.deltaTime);
// 保存位置移動史
_positionHistory.Insert(0, transform.position);
// 移動身體組件
int index = 0;
foreach (var body in _bodyParts)
{
Vector3 point = _positionHistory[Mathf.Clamp(index * Gap, 0, _positionHistory.Count - 1)];
// 讓貪吃蛇的身體組件沿著頭部的移動軌跡運動
Vector3 moveDirection = point - body.transform.position;
body.transform.position += moveDirection * bodySpeed * Time.deltaTime;
// 讓身體組件朝向頭部移動的方向
body.transform.LookAt(point);
index++;
}
}
// 蛇身延長
private void addBodyPart()
{
GameObject body = Instantiate(bodyPrefab, new Vector3(0, transform.position.y, 0), Quaternion.identity);
_bodyParts.Add(body);
}
// 后續(xù)添加的body打上Block標(biāo)簽
private void addBodyPart_Block()
{
GameObject body = Instantiate(bodyPrefab, new Vector3(0, _bodyParts.Last().transform.position.y, 0), Quaternion.identity);
body.tag = "Block";
_bodyParts.Add(body);
}
//觸發(fā)檢測
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Food")
{
//必須先刪除,否則會導(dǎo)致多次觸發(fā)
Destroy(other.gameObject);
addBodyPart_Block();
GameObject.Find("SpawnPoint").GetComponent<SpawnItem>().SpawnItems();
audioController.PlaySfx(audioController.eat);
}
else if (other.tag == "Block")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
}
食物旋轉(zhuǎn)
控制食物旋轉(zhuǎn)比較簡單,在update中加入Rotate即可。
Food.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Food : MonoBehaviour
{
void Start()
{
}
void Update()
{
//旋轉(zhuǎn)
transform.Rotate(Vector3.up);
}
}
食物隨機(jī)生成
食物隨機(jī)生成我并沒有采用隨機(jī)數(shù)的方式,三維場景容易出現(xiàn)問題。因此這里在場景中添加了6個食物生成的點位,當(dāng)食物被觸發(fā)之后,在隨機(jī)的一個點位上生成新的食物。
SpawnItem.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnItem : MonoBehaviour
{
public Transform[] SpawnPoints;
public float spawnTime = 2.5f;
public GameObject Items;
void Start()
{
}
void Update()
{
}
public void SpawnItems()
{
int spawnIndex = Random.Range(0, SpawnPoints.Length);
Instantiate(Items, SpawnPoints[spawnIndex].position, SpawnPoints[spawnIndex].rotation);
}
}
場景切換
這里對于游戲開始界面和結(jié)束界面分別用不同的場景進(jìn)行隔離,切換時只需一行代碼:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
這里的Index為打包時場景的序號順序。
本地WebGL測試
使用WebGL打包之后,會得到3個文件夾和一個index.html文件,直接打開index.html會報錯,需要使用服務(wù)器方式去運行。
首先在win10上配置服務(wù)器相關(guān)組件,參考之前的博文【實用技巧】Win10搭建局域網(wǎng)FTP服務(wù)器。
之后在打包的文件夾下新建一個文件web.config
,輸入以下內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<!--
有關(guān)如何配置 ASP.NET 應(yīng)用程序的詳細(xì)信息,請訪問
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.webServer>
<httpProtocol>
<!-- 允許跨域配置 -->
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="X-Requested-With,Content-Type,Authorization" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE,OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
<staticContent>
<remove fileExtension=".mem" />
<remove fileExtension=".data" />
<remove fileExtension=".unity3d" />
<remove fileExtension=".jsbr" />
<remove fileExtension=".membr" />
<remove fileExtension=".databr" />
<remove fileExtension=".unity3dbr" />
<remove fileExtension=".jsgz" />
<remove fileExtension=".memgz" />
<remove fileExtension=".datagz" />
<remove fileExtension=".unity3dgz" />
<remove fileExtension=".json" />
<remove fileExtension=".unityweb" />
<mimeMap fileExtension=".mem" mimeType="application/octet-stream" />
<mimeMap fileExtension=".data" mimeType="application/octet-stream" />
<mimeMap fileExtension=".unity3d" mimeType="application/octet-stream" />
<mimeMap fileExtension=".jsbr" mimeType="application/octet-stream" />
<mimeMap fileExtension=".membr" mimeType="application/octet-stream" />
<mimeMap fileExtension=".databr" mimeType="application/octet-stream" />
<mimeMap fileExtension=".unity3dbr" mimeType="application/octet-stream" />
<mimeMap fileExtension=".jsgz" mimeType="application/x-javascript; charset=UTF-8" />
<mimeMap fileExtension=".memgz" mimeType="application/octet-stream" />
<mimeMap fileExtension=".datagz" mimeType="application/octet-stream" />
<mimeMap fileExtension=".unity3dgz" mimeType="application/octet-stream" />
<mimeMap fileExtension=".json" mimeType="application/json; charset=UTF-8" />
<mimeMap fileExtension=".unityweb" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>
之后在iis中,新建一個http服務(wù)器,選擇一個不被占用的端口,我這里選擇8080端口。
開啟網(wǎng)站后,在瀏覽器輸入http://localhost:8080/
,即可訪問測試。
Github部署
Github部署非常容易,新建一個倉庫,將打包出的內(nèi)容直接上傳。
然后在Settings/Pages選擇main分支,點擊Save,過幾分鐘就會在上方出現(xiàn)訪問網(wǎng)址。文章來源:http://www.zghlxwxcb.cn/news/detail-725744.html
遺留問題:打包前后測試不一致
目前該項目在untiy運行測試時正常, 但打包出webgl或exe時,卻出現(xiàn)蛇身分離的情況,看了一些打包時的選項,仍未解決該問題,有了解這一問題的讀者歡迎在評論區(qū)交流。文章來源地址http://www.zghlxwxcb.cn/news/detail-725744.html
到了這里,關(guān)于【Unity】3D貪吃蛇游戲制作/WebGL本地測試及項目部署的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!