下面,我们为游戏加一点变数——天气
最初的设想为晴天时,火焰·炮弹防御塔正常运行,弩箭·激光武器威力加1/2,冰塔减速效果削弱;雨天时,火焰·炮弹伤害减半,减速效果提升,雪天火焰·炮弹伤害减3/4,除减速的冰塔,其余防御塔攻速减半。
然后我们下载一个雨雪的插件,或者自己写也行。
unity3d 游戏插件 移动平台天气系统 UniStorm Mobile
链接: http://pan.baidu.com/s/1sjIEY41
密码: n23b
unity3d 游戏插件 天气 气候 模拟 Nuaj' v1.2 天气制作系统
链接: http://pan.baidu.com/s/1pJvIyKz
密码: r289
unity3d 游戏插件 Storm Effects 天气特效支持IOS
链接: http://pan.baidu.com/s/1qW00hqc
密码: p6qn
我使用的是第三个。
那么开始动手吧!
首先创建一个空物体,并放在在合适的位置,并创建WeatherController脚本控制天气转变,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeatherController : MonoBehaviour
{
private GameObject Wea;
public int weather = 0;
GameObject o = null;
private int oldweather;
void Awake()
{
Wea = (GameObject)Resources.Load("Weather/Clouds Light");
}
public void Change()
{
if (weather == 0)
{
Wea = (GameObject)Resources.Load("Weather/Clouds Light");
}
else if (weather == 1)
{
Wea = (GameObject)Resources.Load("Weather/Rain Mobile Heavy");
}
else if (weather == 2)
{
Wea = (GameObject)Resources.Load("Weather/Snow Medium Happy");
}
if (transform.GetChildCount() == 0)
{
o = Instantiate(Wea, transform.position , Wea.transform.rotation) as GameObject;
o.transform.parent = transform;
oldweather = weather;
}
else if (transform.GetChildCount() > 0 && weather != oldweather)
{
Destroy(transform.GetChild(0).gameObject);
o = Instantiate(Wea, transform.position , Wea.transform.rotation) as GameObject;
o.transform.parent = transform;
oldweather = weather;
}
}
}
我暂时打算分波次改变天气(随机也行,固定也可,随你喜欢),weather记录当前天气编号,oldweather记录上一次天气编号,如果前后不同才更新,否则不变(减少实例化和销毁次数),然后在EnemyCreatePos 脚本的Test02协程中,随机weather值,达到改变天气从而改变防御塔的作用(防止玩家过分依赖某一种防御塔)。
然后更改CheckEnemy脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckEnemy : MonoBehaviour
{
private WeatherController wc;
/// <summary>
/// 是否能打到飞行怪
/// </summary>
public bool IsHitFly;
/// <summary>
/// 开火频率
/// </summary>
public float firewaittime = 2;
private float fw;
private float dv;
/// <summary>
/// 炮塔价格
/// </summary>
public float value = 100;
/// <summary>
/// 临时ID,测试
/// </summary>
public int idi = 0;
/// <summary>
/// 是否是特效炮塔,比如激光,火焰枪
/// </summary>
public bool IsEffectTow;
/// <summary>
/// 炮塔转动速度
/// </summary>
public float turnSpeed = 10;
/// <summary>
/// 最远攻击距离
/// </summary>
public float disFire = 12;
/// <summary>
/// 是否有正在攻击的目标
/// </summary>
private bool isAlarm;
/// <summary>
/// 子弹
/// </summary>
public GameObject ziDan;
/// <summary>
/// 子弹生成位置
/// </summary>
public Transform firePos;
/// <summary>
/// 攻击目标
/// </summary>
private Transform targetEnemy;
/// <summary>
/// 存储所有检测到的敌人
/// </summary>
private List<Transform> enemyList;
/// <summary>
/// 开火的协程是否开启
/// </summary>
private bool isFire = false;
void Start ()
{
fw = firewaittime;
wc = GameObject.Find("Weather").GetComponent<WeatherController>();
enemyList = new List<Transform>();
//if (ziDan.name == "ZhaDan" || ziDan.name == "FireIce")
//{
// IsHitFly = false;
//}
//else {
// IsHitFly = true;
//}
//如果是特效炮塔,关闭特效
if (IsEffectTow) {
if (transform.GetComponentInChildren<ParticleSystem>() != null) {
transform.GetComponentInChildren<ParticleSystem>().Stop();
}
}
}
void Update () {
//攻击队列里有敌人
if (enemyList.Count != 0)
{
if (enemyList[0] == null)
{
if (enemyList.Count != 1)
{
enemyList[0] = enemyList[1];
enemyList.Remove(enemyList[1]);
}
else {
enemyList = new List<Transform>();
}
}
else
{
if (!isFire)
{
isFire = true;
StartCoroutine("Fire");
}
//瞄准敌人
//transform.LookAt(enemyList[0]);
if (transform.name != "HighTower")
{
Vector3 direction = enemyList[0].position - transform.GetChild(0).GetChild(0).position + new Vector3(0, 1.2f, 0);
Quaternion qua = Quaternion.LookRotation(direction);
transform.GetChild(0).GetChild(0).rotation = Quaternion.Lerp(transform.GetChild(0).GetChild(0).rotation, qua, Time.deltaTime * turnSpeed);
}
////如果敌人脱离攻击范围,或者死亡
if (enemyList[0].GetComponent<EnemyContrl>().Blood <= 0)
{
enemyList.Remove(enemyList[0]);
}
}
}
else {
if (isFire)
{
StopCoroutine("Fire");
if (IsEffectTow) {
transform.GetComponentInChildren<ParticleSystem>().Stop();
}
isFire = false;
enemyList = new List<Transform>();
}
}
}
/// <summary>
/// 检测触发物是否为敌人,是的话放入攻击列表
/// </summary>
void OnTriggerEnter(Collider other)
{
if (IsHitFly)
{
if (other.transform.tag == "Enemy" || other.transform.tag == "FlyEnemy")
{
if (!enemyList.Contains(other.transform))
{
enemyList.Add(other.transform);
}
}
}
else
{
if (other.transform.tag == "Enemy")
{
if (!enemyList.Contains(other.transform))
{
enemyList.Add(other.transform);
}
}
}
}
void OnTriggerExit(Collider other)
{
if (other.transform.tag == "Enemy" || other.transform.tag == "FlyEnemy")
{
if (enemyList.Contains(other.transform))
{
enemyList.Remove(other.transform);
}
}
}
/// <summary>
/// 炮塔射击敌人
/// </summary>
IEnumerator Fire() {
//播放特效
if (IsEffectTow) {
transform.GetComponentInChildren<ParticleSystem>().Play();
}
GameObject go = null;
while (true)
{
yield return new WaitForSeconds(firewaittime);
if (enemyList.Count != 0 && enemyList[0] != null)
{
//生成子弹并指定发射的目标
go = Instantiate<GameObject>(ziDan, firePos.transform.position, firePos.transform.rotation);
if (transform.tag == "Pao")
{
dv = go.GetComponent<ZhaDan>().damageValue;
go.GetComponent<ZhaDan>().target = enemyList[0];
}
else if(transform.tag == "NoPao")
{
go.GetComponent<Bullet>().target = enemyList[0];
}
if (go.tag == "Bomb")
{
go.GetComponent<ZhaDan>().damageValue = dv;
firewaittime = fw;
if (wc.weather == 1)
{
go.GetComponent<ZhaDan>().damageValue = dv * 0.75f;
firewaittime = fw * 0.75f;
}
else if (wc.weather == 0)
{
go.GetComponent<ZhaDan>().damageValue = dv * 1.25f;
}
}
else if (go.tag == "Ice")
{
firewaittime = fw;
if (wc.weather == 1)
{
go.GetComponent<ZhaDan>().damageValue = dv * 1.25f;
}
else if (wc.weather == 2)
{
firewaittime = fw * 1.25f;
}
else
{
go.GetComponent<ZhaDan>().damageValue = dv * 0.75f;
firewaittime = fw * 0.75f;
}
}
else if (go.tag == "Untagged")
{
firewaittime = fw;
if (wc.weather == 0)
{
firewaittime = fw * 1.25f;
}
else if (wc.weather == 1)
{
firewaittime = fw * 1.25f;
}
}
}
else if (enemyList.Count != 0 && enemyList[0] == null)
{
if (IsEffectTow)
{
transform.GetComponentInChildren<ParticleSystem>().Stop();
}
enemyList.Remove(enemyList[0]);
Destroy(go);
}
}
}
}
网友评论