unity3d 贪吃蛇移动

来源:互联网 发布:js修改button的文字 编辑:程序博客网 时间:2024/06/05 14:21

头部控制

using UnityEngine;using System.Collections;using System.Threading;public class HeadMove : MonoBehaviour{    public GameObject snack_Body;    private GameObject firstSnackBody;    private GameObject lastSnackBody;    private GameObject CreatedBody;    //随机生成食物球      private float foodMaxWidth = 48f;    private float foodMinWidth = -48f;    private float foodMaxForward = 48f;    private float foodMinForwaed = -48f;    public GameObject cube_food;    private float timer = 0f;    private float time = 0.3f;    private GameObject cc;    void RandomFood()    {        //随机生成食物        float foodX = Random.Range(foodMinWidth, foodMaxWidth);        float foodZ = Random.Range(foodMinForwaed, foodMaxForward);        cc = Instantiate(cube_food, new Vector3(foodX, 0, foodZ), Quaternion.identity) as GameObject;    }    void Start()    {        RandomFood();    }    void Update()    {        if (timer > time)        {               Vector3 old = this.transform.position;            this.transform.position += transform.forward;            if (firstSnackBody != null)            {                firstSnackBody.GetComponent<CreateBody>().moveTo(old);            }            timer = 0;        }         else        {            timer += Time.deltaTime;        }        //当它得尾巴在前,头在后时,此时,它就不能往前走了(尾巴不能拖着头走)          if (Input.GetKeyDown(KeyCode.W) && Vector3.Angle(Vector3.forward, this.transform.forward) < 160f)        {            //此时可以移动了              //类似于单链表,头移动,头的坐标传给第一个身体,第一个身体传给第二个身体......              //写一个移动的方法              this.transform.forward = Vector3.forward * 1;        }        if (Input.GetKeyDown(KeyCode.S) && Vector3.Angle(Vector3.back, this.transform.forward) < 160f)        {            this.transform.forward = Vector3.back * 1;        }        if (Input.GetKeyDown(KeyCode.A) && Vector3.Angle(Vector3.left, this.transform.forward) < 160f)        {            this.transform.forward = Vector3.left * 1;        }        if (Input.GetKeyDown(KeyCode.D) && Vector3.Angle(Vector3.right, this.transform.forward) < 160f)        {            this.transform.forward = Vector3.right * 1;           }    }    void CreateSnackBody()    {        GameObject CreateSnackBodys = Instantiate(snack_Body, new Vector3(100f, 100f, 100f), Quaternion.identity) as GameObject;        if (lastSnackBody != null)        {            lastSnackBody.GetComponent<CreateBody>().nextSnackBody = CreateSnackBodys;            print("lastBody==null");        }        if (firstSnackBody == null)        {            firstSnackBody = CreateSnackBodys;            print("firstBody==null");        }        lastSnackBody = CreateSnackBodys;    }    void OnTriggerEnter(Collider other)    {        if (other.gameObject.tag.CompareTo("food") == 0)        {            //创建一个身体              Destroy(cc);            RandomFood();            CreateSnackBody();        }    }}

身体控制

using UnityEngine;using System.Collections;using System.Threading;public class CreateBody : MonoBehaviour{    public GameObject nextSnackBody;    void Start()    {    }    void Update()    {    }    public void moveTo(Vector3 pos)    {        Vector3 old = this.transform.position;        this.transform.position = pos;//当前坐标变为pos.          if (nextSnackBody != null)        {            nextSnackBody.GetComponent<CreateBody>().moveTo(old);        }    }}