下面,我們?yōu)橛螒蚣右稽c(diǎn)變數(shù)——天氣
最初的設(shè)想為晴天時(shí)轮听,火焰·炮彈防御塔正常運(yùn)行骗露,弩箭·激光武器威力加1/2,冰塔減速效果削弱血巍;雨天時(shí)萧锉,火焰·炮彈傷害減半,減速效果提升述寡,雪天火焰·炮彈傷害減3/4柿隙,除減速的冰塔,其余防御塔攻速減半鲫凶。
然后我們下載一個(gè)雨雪的插件禀崖,或者自己寫(xiě)也行。
unity3d 游戲插件 移動(dòng)平臺(tái)天氣系統(tǒng) UniStorm Mobile
鏈接: http://pan.baidu.com/s/1sjIEY41
密碼: n23b
unity3d 游戲插件 天氣 氣候 模擬 Nuaj' v1.2 天氣制作系統(tǒng)
鏈接: http://pan.baidu.com/s/1pJvIyKz
密碼: r289
unity3d 游戲插件 Storm Effects 天氣特效支持IOS
鏈接: http://pan.baidu.com/s/1qW00hqc
密碼: p6qn
我使用的是第三個(gè)螟炫。
那么開(kāi)始動(dòng)手吧波附!
首先創(chuàng)建一個(gè)空物體,并放在在合適的位置昼钻,并創(chuàng)建WeatherController腳本控制天氣轉(zhuǎn)變掸屡,代碼如下:
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;
}
}
}
我暫時(shí)打算分波次改變天氣(隨機(jī)也行,固定也可然评,隨你喜歡)折晦,weather記錄當(dāng)前天氣編號(hào),oldweather記錄上一次天氣編號(hào)沾瓦,如果前后不同才更新满着,否則不變(減少實(shí)例化和銷(xiāo)毀次數(shù))谦炒,然后在EnemyCreatePos 腳本的Test02協(xié)程中,隨機(jī)weather值风喇,達(dá)到改變天氣從而改變防御塔的作用(防止玩家過(guò)分依賴(lài)某一種防御塔)宁改。
然后更改CheckEnemy腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckEnemy : MonoBehaviour
{
private WeatherController wc;
/// <summary>
/// 是否能打到飛行怪
/// </summary>
public bool IsHitFly;
/// <summary>
/// 開(kāi)火頻率
/// </summary>
public float firewaittime = 2;
private float fw;
private float dv;
/// <summary>
/// 炮塔價(jià)格
/// </summary>
public float value = 100;
/// <summary>
/// 臨時(shí)ID,測(cè)試
/// </summary>
public int idi = 0;
/// <summary>
/// 是否是特效炮塔魂莫,比如激光还蹲,火焰槍
/// </summary>
public bool IsEffectTow;
/// <summary>
/// 炮塔轉(zhuǎn)動(dòng)速度
/// </summary>
public float turnSpeed = 10;
/// <summary>
/// 最遠(yuǎn)攻擊距離
/// </summary>
public float disFire = 12;
/// <summary>
/// 是否有正在攻擊的目標(biāo)
/// </summary>
private bool isAlarm;
/// <summary>
/// 子彈
/// </summary>
public GameObject ziDan;
/// <summary>
/// 子彈生成位置
/// </summary>
public Transform firePos;
/// <summary>
/// 攻擊目標(biāo)
/// </summary>
private Transform targetEnemy;
/// <summary>
/// 存儲(chǔ)所有檢測(cè)到的敵人
/// </summary>
private List<Transform> enemyList;
/// <summary>
/// 開(kāi)火的協(xié)程是否開(kāi)啟
/// </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;
//}
//如果是特效炮塔,關(guān)閉特效
if (IsEffectTow) {
if (transform.GetComponentInChildren<ParticleSystem>() != null) {
transform.GetComponentInChildren<ParticleSystem>().Stop();
}
}
}
void Update () {
//攻擊隊(duì)列里有敵人
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");
}
//瞄準(zhǔn)敵人
//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>
/// 檢測(cè)觸發(fā)物是否為敵人,是的話(huà)放入攻擊列表
/// </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)
{
//生成子彈并指定發(fā)射的目標(biāo)
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);
}
}
}
}