大家好,我是Zander,我们接着来开发Rts3D即时战略游戏开发。今天我们来讲游戏的最后部分:伤害更新信息显示、销毁单位、爆炸效果。
上一章我们的单位能相互攻击了,但是还不能刷新数据,这里我们将修改脚本使选中的时候能刷新数据,找到ShowUnitInfo脚本,在其顶部声明一个bool变量,用来确定是否显示数据,编辑如下:
using UnityEngine;
using System.Collections;
public class ShowUnitInfo : Interaction {
public string Name;
public float MaxHealth, CurrentHealth;
public Sprite ProfilePic;
bool show = false; //是否显示信息
public override void Select ()
{
show = true; //选中的时候信息显示
}
void Update ()
{
if (!show)
return;
InfoManager.Current.SetPic (ProfilePic);
InfoManager.Current.SetLines (
Name,
CurrentHealth + "/" + MaxHealth,
"Owner: " + GetComponent<Player> ().Info.Name);
}
public override void Deselect ()
{
InfoManager.Current.ClearPic ();
InfoManager.Current.ClearLines ();
show = false;
}
}
这样就可以明显的看到玩家伤害实时刷新了,接下来我们要在HP<0的时候销毁单位。在Battle文件夹下新建一个DestroyOnNoHealth脚本,并编辑:
using UnityEngine;
using System.Collections;
public class DestroyOnNoHealth : MonoBehaviour {
public GameObject ExplosionPrefab; //死亡效果
private ShowUnitInfo info; //显示信息
// Use this for initialization
void Start () {
info = GetComponent<ShowUnitInfo> ();
}
// Update is called once per frame
void Update () {
if (info.CurrentHealth <= 0) {
Destroy (this.gameObject); //销毁物体
GameObject.Instantiate (ExplosionPrefab, transform.position, Quaternion.identity); //添加爆炸效果
}
}
}
返回到Unity中,找到DroneUnit预设,为其添加DestroyOnNoHealth脚本,然后找到AttackInRange脚本,声明一个变量来表示冲击波特效预设
public GameObject ImpactVisual; //表示可见的冲击波,只是一个射击效果
然后在Attack方法中初始化
void Attack()
{
if (target == null)
return;
target.CurrentHealth -= AttackDamage;
GameObject.Instantiate (ImpactVisual, target.transform.position, Quaternion.identity); //产生伤害的时候出现冲击波效果
}
这样就在代码中设置好了爆炸效果了,接下来我们为其添加爆炸预设,爆炸特效我们就不自己制作了,我们直接在UnityAssetStore中使用这个免费插件,如图
这个自己在官网下载就可以了,当然如果你有特NB的特效也可以不用这个,找到DroneUnit为其添加特效,如图:
这样我们就拥有了自己的RTS即时战略游戏了。最终源码:链接:http://pan.baidu.com/s/1i4XPnLj 密码:83k7 欢迎大家下载
到了这里,我们的游戏开发就结束了。如有什么疑问希望大家可以在QQ群或者公众号留言。欢迎大家加入QQ群:280993838 或者关注我的公众号: