unity官方教程 太空射击---问题填坑 之 计分以及游戏胜利

来源:互联网 发布:linux arp攻击 编辑:程序博客网 时间:2024/04/28 05:58

(本文仅供自己参考,文中代码可能有误,毕竟手打没有VS的帮助,请仅供理解,切莫复制粘贴)原来的代码还是不理解为什么,但现在有了新的方法,前排提醒,一下方法会与官方教程出现巨大误差,请理解后使用

首先我先进行一下解释,我们需要两段代码,其中一个放在玩家的子弹中,另一个为单独的Text

这里的重点是玩家的子弹上的代码,来,上代码

 public float speed;// Use this for initializationvoid Start () {        GetComponent<Rigidbody>().velocity = transform.forward * -speed;//z轴*speed        }    void onTriggerEnter(Collider other)    {        if (other.gameObject.tag == "Enemy")        {            StartCoroutine(MyMethod());            Destory(other.gameObject);            Text.Add();        }    }
这里可以看见加入了碰撞判断,如此简单粗暴的在碰撞后调用了Text的功能以及消灭物体的功能,十分易懂

当然游戏物体不止一个敌机,所以在判断语句上请自行修改

接下来是计分,这里就不是很重要了,当然,要配合上面的代码以其参考效果更佳

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Text : MonoBehaviour {    private int count;// Use this for initializationvoid Start () {        GetComponent < TextMesh >().text = "Count" + count;}    public void Add()    {        count += 10;                  }    IEnumerator MyMethod()    {        GetComponent<TextMesh>().text = "win";        yield return new WaitForSeconds(2);        Application.Quit();    }// Update is called once per framevoid Update () {        if (count > 150)        {            StartCoroutine(MyMethod());        }        else        {            GetComponent<TextMesh>().text = "Count" + count;        }}}
这里也是针对单一物体敌机的,完成了加分以及退出,这里我就不多做解释了,其实也没什么解释的。

好了,至此太空射击游戏学习圆满完成。

阅读全文
0 0
原创粉丝点击