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;
}
}
}
这里也是针对单一物体敌机的,完成了加分以及退出,这里我就不多做解释了,其实也没什么解释的。
好了,至此太空射击游戏学习圆满完成。
首先我先进行一下解释,我们需要两段代码,其中一个放在玩家的子弹中,另一个为单独的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;
}
}
}
这里也是针对单一物体敌机的,完成了加分以及退出,这里我就不多做解释了,其实也没什么解释的。
好了,至此太空射击游戏学习圆满完成。
相关文章推荐
- Unity官方游戏实例视频中文教程之太空射击学习笔记
- [精华分享][Unity&模型制作&blender]适合独立游戏开发的官方免费3D人物模型软件以及教程合集
- 对《[Unity官方实例教程 秘密行动] Unity官方教程《秘密行动》(十二) 角色移动》的一些笔记和个人补充,解决角色在地形上移动时穿透问题。
- 【木头Cocos2d-x 015】游戏实例-《跑跑跑》制作教程(第七篇)——添加能吃的物品以及胜利条件
- Unity官方第一个人称射击游戏<恶魔射手>—学习笔记四(游戏中的管理)
- [blender][blender教程].obj.fbx导入Unity的材质无颜色问题以及解决办法
- 不知道怎么开发VR游戏?Unity5.3官方VR教程重磅登场-系列8 VR开发的更多资料
- Unity官方教程|使用Raycast显示射击轨迹
- Cocos2d-x游戏实例-《跑跑跑》制作教程(第七篇)——添加能吃的物品以及胜利条件
- 【四圣龙神录】 开源仿东方 弹幕射击游戏教程 官网(译)以及国内参考翻译资料
- 将Unity官方射击游戏 Space Shooter 改为实时对战小游戏,使用天梯实时对战服务(NanoLink)
- 不知道怎么开发VR游戏?Unity5.3官方VR教程重磅登场-系列3 VR中的交互方式
- 不知道怎么开发VR游戏?Unity5.3官方VR教程重磅登场(转)
- Unity官方第一人称射击游戏<恶魔射手>_学习笔记三(敌人脚本)
- 【开坑】针对unity,sourcetree,以及各种windows应用在游戏开发中的问题
- [Unity&多人游戏插件]Playfab 的下载 安装以及简单的说明
- Unity官方教程学习笔记之Roll A Ball篇---(二)创建地面
- 【Unity】遇到的莫名其妙的问题以及其解决办法
- unity官方教程Space shooter学习笔记-01
- C#程序员整理的Unity 3D笔记(二十):2D Toolkit之官方教程《Whack a Mole》