您的位置:首页 > 移动开发 > Unity3D

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

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

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

这里的重点是玩家的子弹上的代码,来,上代码 public float speed;
// Use this for initialization
void 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 initialization
void 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 frame
void Update () {
if (count > 150)
{
StartCoroutine(MyMethod());

}
else
{
GetComponent<TextMesh>().text = "Count" + count;
}
}
}
这里也是针对单一物体敌机的,完成了加分以及退出,这里我就不多做解释了,其实也没什么解释的。
好了,至此太空射击游戏学习圆满完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐