首先創建一個頭部,編寫腳本利用WASD控制頭部的移動。
Vector3 up=new Vector3(0,1,0);
Vector3 down=new Vector3(0,-1,0);
Vector3 left=new Vector3(-1,0,0);
Vector3 right=new Vector3(1,0,0);
Vector3 now;//頭部實際前進方向
float timer=0f;
float timerGap=0.1f;
void Start ()
{
now = up;
}
void Update ()
{
if (now!=up&&now!=down&&Input.GetKey (KeyCode.W))
{
now = up;
}
if (now!=up&&now!=down&&Input.GetKey (KeyCode.S))
{
now = down;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.A))
{
now=left;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.D))
{
now = right;
}
timer += Time.deltaTime;
if (timer > timerGap)
{
//每隔0.1s向當前方向移動一個單位(0.5爲頭部大小)。
timer = 0;
transform.position = 0.5f * now + transform.position;
}
}
然後就是創建初始身體,實現身體跟隨頭部。採用的方法是將身體放進一個數組,然後下標0的身體移動到頭部之前的位置,然後下標 i 的身體移動到 i-1 的position。
創建初始身體,並放入數組。
public GameObject body;//身體預設體
List<GameObject> snakeBody = new List<GameObject>();
void Awake()
{
for (int i = 0; i < 3; ++i)
{
GameObject newbodynext=Instantiate (body,
transform.position-(i+1)*new Vector3(0,0.5f,0),
Quaternion.identity)as GameObject;
snakeBody.Add (newbodynext);
}
}
實現跟隨
void Update ()
{
if (now!=up&&now!=down&&Input.GetKey (KeyCode.W))
{
now = up;
}
if (now!=up&&now!=down&&Input.GetKey (KeyCode.S))
{
now = down;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.A))
{
now=left;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.D))
{
now = right;
}
timer += Time.deltaTime;
if (timer > timerGap)
{
Vector3 tmpPosition = transform.position;//記錄頭部變化前的位置
List<Vector3> tmpList = new List<Vector3> ();//記錄身體變化前的位置
for (int i = 0; i < snakeBody.Count; ++i)
{
tmpList.Add (snakeBody [i].transform.position);
}
timer = 0;
transform.position = 0.5f * now + transform.position;
snakeBody [0].transform.position = tmpPosition;//將0移到頭部之前的位置
//依次前移身體的位置
for (int i = 1; i < snakeBody.Count; ++i)
{
snakeBody [i].transform.position = tmpList [i - 1];
}
}
}
初始蛇創建好後,就開始添加食物,和增長蛇的身體。還有檢測遊戲失敗,即撞到身體或者邊界,採用事件觸發檢測完成。
創建食物
public GameObject foodPrefab;//食物預設體
void Start () {
now = up;
createFood ();
}
void createFood()
{
float x = Random.Range(-6.5f, 6.5f);
float y = Random.Range(-4.5f, 4.5f);
Instantiate(foodPrefab,new Vector3(x,y,0f),Quaternion.identity);
}
觸發檢測
void OnTriggerEnter(Collider other)
{ //這個other就是被碰撞體
if (other.gameObject.tag.Equals("Food"))
{
Destroy(other.gameObject);
GameObject newbodynext = Instantiate (body,
snakeBody[snakeBody.Count-1].transform.position,
Quaternion.identity)as GameObject;
snakeBody.Add (newbodynext);//增加蛇的身體
createFood();
}
else if(other.gameObject.tag.Equals("Body"))
{
SceneManager.LoadScene("Snake", LoadSceneMode.Single);//重新開始
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag.Equals("Boundary"))
SceneManager.LoadScene("Snake", LoadSceneMode.Single);
}
完整代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class HeadMove : MonoBehaviour {
public GameObject body;
public GameObject foodPrefab;
Vector3 up=new Vector3(0,1,0);
Vector3 down=new Vector3(0,-1,0);
Vector3 left=new Vector3(-1,0,0);
Vector3 right=new Vector3(1,0,0);
Vector3 now;
float timer=0f;
float timerGap=0.1f;
List<GameObject> snakeBody = new List<GameObject>();
// Use this for initialization
void Awake()
{
for (int i = 0; i < 3; ++i)
{
GameObject newbodynext=Instantiate (body, transform.position-(i+1)*new Vector3(0,0.5f,0),Quaternion.identity)as GameObject;
snakeBody.Add (newbodynext);
}
}
void Start () {
now = up;
createFood ();
}
void createFood()
{
float x = Random.Range(-6.5f, 6.5f);
float y = Random.Range(-4.5f, 4.5f);
Instantiate(foodPrefab,new Vector3(x,y,0f),Quaternion.identity);
}
// Update is called once per frame
void Update ()
{
if (now!=up&&now!=down&&Input.GetKey (KeyCode.W))
{
now = up;
}
if (now!=up&&now!=down&&Input.GetKey (KeyCode.S))
{
now = down;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.A))
{
now=left;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.D))
{
now = right;
}
timer += Time.deltaTime;
if (timer > timerGap)
{
Vector3 tmpPosition = transform.position;
List<Vector3> tmpList = new List<Vector3> ();
for (int i = 0; i < snakeBody.Count; ++i)
{
tmpList.Add (snakeBody [i].transform.position);
}
timer = 0;
transform.position = 0.5f * now + transform.position;
snakeBody [0].transform.position = tmpPosition;
for (int i = 1; i < snakeBody.Count; ++i)
{
snakeBody [i].transform.position = tmpList [i - 1];
}
}
}
void OnTriggerEnter(Collider other)
{ //這個other就是被碰撞體
if (other.gameObject.tag.Equals("Food"))
{
Destroy(other.gameObject);
GameObject newbodynext = Instantiate (body,snakeBody[snakeBody.Count-1].transform.position,Quaternion.identity)as GameObject;
snakeBody.Add (newbodynext);
createFood();
}
//由於身體和頭部一開始就接觸,所以將身體的碰撞半徑減小到0.4
else if(other.gameObject.tag.Equals("Body"))
{
SceneManager.LoadScene("Snake", LoadSceneMode.Single);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag.Equals("Boundary"))
SceneManager.LoadScene("Snake", LoadSceneMode.Single);
}
}
將該腳本掛載在頭部對象上然後添加身體和食物預設體,再添加邊界即可。