這篇文章主要為大家展示了Unity如何實(shí)現(xiàn)3D貪吃蛇,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。
記錄一下前段時(shí)間寫到的一個(gè)3D貪吃蛇的移動(dòng)代碼。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameManager : MonoBehaviour { List<Transform> bodyList = new List<Transform>();//身體位置的列表 private float speed = 2f;//移動(dòng)速度 public GameObject bodyObj;//用來生成的身體對(duì)象 List<Transform> bodyCreatePostion = new List<Transform>();//身體部分的transfrom 列表 private float TransOffset=1f;//位置間隔 private List<Vector2> mapAddress=new List<Vector2>();//前進(jìn)目標(biāo)在地圖上的位置, public Transform firstBody;//第一個(gè)身體部件的transfrom const int row = 100;//地圖寬 const int col = 100;//地圖高 int step=1;//步伐 int x=0, z=0;//記錄x和z軸的移動(dòng)量 // Start is called before the first frame update public Vector3[,] map = new Vector3[row, col];//地圖 // Start is called before the first frame update void Start() { for (int i = 0; i < row; i++)//生成地圖 { for (int j = 0; j < col; j++) { map[i, j] = new Vector3(i, 1.5f, j); } } transform.position =map[0,0];將當(dāng)前位置設(shè)為原點(diǎn) mapAddress.Add(new Vector2(1,0));//增加第一個(gè)目標(biāo) bodyList.Add(transform);//刷新第一個(gè)頭和一個(gè)身體的transform bodyList.Add(firstBody); mapAddress.Add(new Vector2(0,0)); } // Update is called once per frame void Update() { for (int i = 0; i < row - 1; i++) { for (int j = 0; j < col - 1; j++)//繪制地圖格子 { Debug.DrawLine(map[i, j], map[i + 1, j], Color.red); Debug.DrawLine(map[i, j], map[i, j + 1], Color.red); } } if (Input.anyKeyDown)//有任何鍵按下 { if (Input.GetKeyDown(KeyCode.W) && z != -step)//上 { z = step; x = 0; } if (Input.GetKeyDown(KeyCode.S) && z != step)//下 { z = -step; x = 0; } if (Input.GetKeyDown(KeyCode.A) && x != step)//左 { x = -step; z = 0; } if (Input.GetKeyDown(KeyCode.D) && x != -step)//右 { x = step; z = 0; } } Move(); } private void Move()//移動(dòng) { if (Vector3.Distance(bodyList[0].position, map[(int)mapAddress[0].x + x, (int)mapAddress[0].y + z]) < 0.7f)//當(dāng)前坐標(biāo)和目標(biāo)位置的距離小于0.5f { for (int i = mapAddress.Count - 1; i > 0; i--)//刷新后一個(gè)的目標(biāo)為前一個(gè)的目標(biāo) { mapAddress[i] = mapAddress[i-1]; } mapAddress[0] = new Vector2(mapAddress[0].x + x, mapAddress[0].y + z);//刷新第一個(gè)目標(biāo)的位置 } else { for (int i = bodyList.Count - 1; i > 0; i--)//移動(dòng) { bodyList[i].position = Vector3.MoveTowards(bodyList[i].position, map[(int)mapAddress[i - 1].x, (int)mapAddress[i - 1].y], Time.deltaTime * speed); } bodyList[0].position = Vector3.MoveTowards(transform.position, map[(int)mapAddress[0].x + x, (int)mapAddress[0].y + z], Time.deltaTime * speed); } } private void OnCollisionEnter(Collision collision)//碰撞到了食物就增加身體長度 { if (collision.collider.tag == "Food") { Destroy(collision.collider.gameObject); GameObject tmpGameObject=new GameObject(); Vector2 tmpVec = new Vector2(); tmpGameObject = Instantiate(bodyObj, map[(int)mapAddress[mapAddress.Count - 1].x+x, (int)mapAddress[mapAddress.Count - 1].y +z], Quaternion.identity);//生成body物體 tmpVec = new Vector2(mapAddress[mapAddress.Count - 1].x+x, mapAddress[mapAddress.Count - 1].y +z); //增加身體的transform和目標(biāo)向量 bodyList.Add(tmpGameObject.transform); mapAddress.Add(tmpVec); } } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞標(biāo)題:Unity如何實(shí)現(xiàn)3D貪吃蛇-創(chuàng)新互聯(lián)
當(dāng)前URL:http://jinyejixie.com/article38/dhdcpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、建站公司、外貿(mào)建站、標(biāo)簽優(yōu)化、動(dòng)態(tài)網(wǎng)站、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容