您的位置:首页 > 其它

吃盒子游戏

2014-12-27 19:45 141 查看
今天来学习一下吃盒子游戏

界面设计



首先需要在unity中创建出如下图所示的wall和food还有一个球Sphere

给food中的cube添加标签为Cube1给sphere添加Player

 



完成之后如图





下面是sphere上挂载的脚本

    public Transform enemy;

    public Transform enemyRocket;

    public Transform player;

    float speed = 0.5f;

    float i = 0f;

    int height = 10;

    public float l = 20f;

    void Update()

    {

      transform.Translate(0, 0, -speed * Time.deltaTime);

        i -= Time.deltaTime;

        if (i <= 0)

        {

            i = 1;

            if (player != null)

            {

                Vector3 pos = enemy.position - player.position;

                GameObject o = GameObject.Instantiate(enemyRocket, enemy.position, Quaternion.LookRotation(pos)) as GameObject;

                Destroy(o,0.1f);

            }

        }

    }

    void OnTriggerEnter(Collider col)

    {

        if (col.tag == "rocket")

        {

            l--;

            if (l <= 0)

            {

                Destroy(this.gameObject);

            }

        }

        if (col.tag == "Player")

        {

            l--;

            if (l <= 0)

            {

                Destroy(this.gameObject);

            }

        }

}

 

下面是cube上挂载的脚本(为关键代码)

 

void Update () {

        transform.Rotate(new Vector3 (10,0,0));

       }

        void OnTriggerEnter(Collider col)

      {    

         Destroy(this .gameObject );   

      }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: