unity3d 貪喫蛇移動

頭部控制

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);
        }



    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章