贪吃蛇身子移动的问题

打个笔记吧,为了更好的理解贪吃蛇身子移动的问题,感觉自己总结一下代码比较好,看着别人的demo写的代码光看不总结感觉有点蒙
1、下面是HeadControl.cs中的代码

//根据按得按钮判断要移动的方向
    public void Turn(){
        if (Input.GetKey (KeyCode.W)) {
            nextDirection=HeadDirection.Up;//记录想要走的方向
            if(currentDirection==HeadDirection.Down){//如果方向与先前的方向相反,则不执行方向的改变
                nextDirection=currentDirection;
            }       
        }
        if (Input.GetKey (KeyCode.S)) {
            nextDirection=HeadDirection.Down;
            if(currentDirection==HeadDirection.Up){
                nextDirection=currentDirection;
            }
        }
        if (Input.GetKey (KeyCode.A)) {
            nextDirection=HeadDirection.Left;
            if(currentDirection==HeadDirection.Right){
                nextDirection=currentDirection;
            }
        }
        if (Input.GetKey (KeyCode.D)) {
            nextDirection=HeadDirection.Right;
            if(currentDirection==HeadDirection.Left){
                nextDirection=currentDirection;
            }
        }
    }
public void Move(){
        timer += Time.deltaTime;
        if(timer>=0.4f/speed){//经过多长时间移动
            //蛇头旋转
            switch (nextDirection) {
            case HeadDirection.Up:  
                transform.forward=Vector3.forward;
                currentDirection=HeadDirection.Up;
                break;  
            case HeadDirection.Down:    
                transform.forward=Vector3.back;
                currentDirection=HeadDirection.Down;
                break;
            case HeadDirection.Left:    
                transform.forward=Vector3.left;
                currentDirection=HeadDirection.Left;
                break;
            case HeadDirection.Right:   
                transform.forward=Vector3.right;
                currentDirection=HeadDirection.Right;
                break;
            }
            //记录头部移动后之前的位置
            Vector3 nextPos=transform.position;
            transform.Translate (Vector3.forward);
            timer=0f;
            if(firstBody!=null){
                firstBody.Move(nextPos);
            }
        }
    }

2、 下面是Body.cs中完整的代码,因为头部后面的身体都是一样的,所以单写一个类,用迭代法实现位置的移动

using UnityEngine;
using System.Collections;

public class Body : MonoBehaviour {
    //下一个身子的引用
    public Body next;
    public void Move(Vector3 pos){
        Vector3 nextPos = transform.position;
        transform.position = pos;
        if (next != null) {
            next.Move(nextPos);     
        }
    }
}

为了以后能看明白,把全部代码粘贴一下吧
HeadControl.cs中完整的代码

using UnityEngine;
using System.Collections;

public enum HeadDirection{
    Up,
    Down,
    Left,
    Right
}
public class HeadControl : MonoBehaviour {
    //食物的预设踢
    public GameObject foodPrefab;
    //身体的预设体
    public GameObject bodyPrefab;
    //移动的速度w
    public float speed;

    //记录移动的时间
    private float timer=0f;

    //当前移动方向
    private HeadDirection currentDirection=HeadDirection.Up;
    //接下来移动方向
    private HeadDirection nextDirection=HeadDirection.Up;
    //
    private Body firstBody;
    //
    private Body lastBody;
    //游戏是否结束
    private bool isOver=false;
    //实例化食物
    public void CreateFood(){
        float x=Random.Range(-4.5f,4.5f);
        float z=Random.Range(-4.5f,4.5f);
        GameObject obj = Instantiate (foodPrefab,new Vector3(x,0f,z),Quaternion.identity)as GameObject ;
    }

    public void OnTriggerEnter(Collider other){
        if(other.tag.Equals("Land")){
            isOver=true;
        }
        if(other.tag.Equals("Body")){
            isOver=true;
        }
        if(other.tag.Equals("Food")){
            Destroy(other.gameObject);
            Grow();
            CreateFood();
        }
    }
    public void Start(){
        CreateFood ();
    }
    // Update is called once per frame
    void Update () {
        if (!isOver) {
            Turn();
            Move ();        
        }


    }
    private void Grow(){
        //动态创建出身体,首先放在看不到的地方
        GameObject obj = Instantiate (bodyPrefab,new Vector3(1000f,1000f,1000f),Quaternion.identity)as GameObject;
        //获取身体的脚本
        Body b = obj.GetComponent (); 
        if(firstBody==null){
            firstBody=b;
        }
        if(lastBody!=null){
            lastBody.next=b;
        }
        lastBody = b;
    }
    //根据按得按钮判断要移动的方向
    public void Turn(){
        if (Input.GetKey (KeyCode.W)) {
            nextDirection=HeadDirection.Up;
            if(currentDirection==HeadDirection.Down){
                nextDirection=currentDirection;
            }       
        }
        if (Input.GetKey (KeyCode.S)) {
            nextDirection=HeadDirection.Down;
            if(currentDirection==HeadDirection.Up){
                nextDirection=currentDirection;
            }
        }
        if (Input.GetKey (KeyCode.A)) {
            nextDirection=HeadDirection.Left;
            if(currentDirection==HeadDirection.Right){
                nextDirection=currentDirection;
            }
        }
        if (Input.GetKey (KeyCode.D)) {
            nextDirection=HeadDirection.Right;
            if(currentDirection==HeadDirection.Left){
                nextDirection=currentDirection;
            }
        }
    }
    public void Move(){
        timer += Time.deltaTime;
        if(timer>=0.4f/speed){//经过多长时间移动
            //是蛇头旋转
            switch (nextDirection) {
            case HeadDirection.Up:  
                transform.forward=Vector3.forward;
                currentDirection=HeadDirection.Up;
                break;  
            case HeadDirection.Down:    
                transform.forward=Vector3.back;
                currentDirection=HeadDirection.Down;
                break;
            case HeadDirection.Left:    
                transform.forward=Vector3.left;
                currentDirection=HeadDirection.Left;
                break;
            case HeadDirection.Right:   
                transform.forward=Vector3.right;
                currentDirection=HeadDirection.Right;
                break;
            }
            //记录头部移动后之前的位置
            Vector3 nextPos=transform.position;
            transform.Translate (Vector3.forward);
            timer=0f;
            if(firstBody!=null){
                firstBody.Move(nextPos);
            }
        }
    }
}

你可能感兴趣的:(unity学习笔记)