(一)場景設置
1.攝像機正交
2.創建一個Quad(create ->3D)當背景,上面放上方塊,這樣全都能顯示出來,不能用畫布,顯示不出來
3.材質球,物體有陰影,把材質球的shader改成Unlit/color
4.分辨率,設置成16:9
5.調整攝像機的視角,(23)
(二)頭部移動
掛一個Move腳本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Move : MonoBehaviour {
public GameObject head;
Vector2 direction = Vector2.up;//不指明一開始的方向,運行頭部不移動
public GameObject food;
// Use this for initialization
void Start () {
InvokeRepeating ("MoveSnack",0.5f,0.5f);
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W)){
direction = Vector2.up;
}
if(Input.GetKey(KeyCode.S)){
direction = Vector2.down;
}
if(Input.GetKey(KeyCode.A)){
direction = Vector2.left;
}
if(Input.GetKey(KeyCode.D)){
direction = Vector2.right;
}
}
void MoveSnack(){
transform.Translate (direction);//transform前沒有東西
}
(三)食物生成
using UnityEngine;
using System.Collections;
public class Food : MonoBehaviour {
public GameObject foodss;
public int xx=30;
public int yy=20;
// Use this for initialization
void Start () {
InvokeRepeating ("CreateFood",0.5f,5);
}
// Update is called once per frame
void Update () {
}
void CreateFood(){
int x = Random.Range (-xx,xx);
int y = Random.Range (-yy,yy);
Instantiate (foodss,new Vector2(x,y),Quaternion.identity);
}
}
(四)
食物碰撞後消失
void OnTriggerEnter(Collider other){
if(other.gameObject.CompareTag("food")){
Destroy(other.gameObject);
}}
放在Move腳本上
同時head頭部上要掛上剛體Rigidoby,不使用重力,盒子碰撞體,並且勾選trigger,
food上掛盒子碰撞體,不勾選trigger,這樣頭部碰撞到食物就會消失,爲了防止頭部擦過食物,食物消失,所以把食物的盒子碰撞器的scale由原來的1變成0.5
(五)生成下一段身體,並且隨着頭部移動(核心)
也寫在Move腳本上
public GameObject body;
public float time = 0.8f;
public bool Switch;//true的時候實例化出來身體
List<Transform> bodyv = new List<Transform>();
void MoveSnack(){
Vector3 VPosition = transform.position;//頭的位置,
transform.Translate (direction);
if(Switch){
GameObject Bodya = (GameObject)Instantiate (body,VPosition,Quaternion.identity);//在頭的位置那生成身體
bodyv.Insert(0,Bodya.transform);
Switch = false;
}
else if(bodyv.Count >0){
bodyv.Last ().position = VPosition;
bodyv.Insert (0,bodyv.Last());
bodyv.RemoveAt (bodyv.Count-1);
}
}
void OnTriggerEnter(Collider other){
if(other.gameObject.CompareTag("food")){
Switch = true;
Destroy(other.gameObject);
}}
(六)死亡判定
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag ("food")) {
Switch = true;
Destroy (other.gameObject);
} else {
SceneManager.LoadScene (0);//重新加載
}
}