Unity_塔防游戏按波产生怪_063
生活随笔
收集整理的這篇文章主要介紹了
Unity_塔防游戏按波产生怪_063
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
怪物預(yù)制體上綁定的腳本:
using UnityEngine; using System.Collections; using System; public class NavigationFixedPath : MonoBehaviour {//保存所有的路點信息GameObject[] pathPoints;//記錄下一個路點int nextPathPointIndex = 1;// Use this for initializationvoid Start () {//通過tag值來獲取所有的路點pathPoints = GameObject.FindGameObjectsWithTag("Path01");//對獲取的路點排序//Array.Reverse(pathPoints);Array.Sort(pathPoints, (x, y) => { return x.gameObject.name.CompareTo(y.gameObject.name); });//產(chǎn)生的怪物放在第一個節(jié)點上transform.position = pathPoints[0].transform.position;//在第一個節(jié)點時,怪物的朝向transform.forward = pathPoints[nextPathPointIndex].transform.position - transform.position;}// Update is called once per framevoid Update () {if (Vector3.Distance(pathPoints[nextPathPointIndex].transform.position, transform.position) < 0.1f){//如果還有下一個節(jié)點就讓nextPathPointIndex自加if (nextPathPointIndex != pathPoints.Length - 1){nextPathPointIndex++;}//如果怪物自身的位置信息和最后一個節(jié)點的距離小于0.1m的時候if (Vector3.Distance(pathPoints[pathPoints.Length - 1].transform.position, transform.position) < 0.1f){//把最后一個節(jié)點的位置信息賦值給怪物自身的位置信息transform.position = pathPoints[pathPoints.Length - 1].transform.position;return;}//每次更換節(jié)點的時候改變怪物的位置信息transform.forward = pathPoints[nextPathPointIndex].transform.position - transform.position;}//怪物移動的邏輯transform.Translate(Vector3.forward * 5 * Time.deltaTime, Space.Self);} }生成預(yù)制體腳本:
using UnityEngine; using System.Collections;public class CreateEnemeies : MonoBehaviour {//每一產(chǎn)生一個的計時器float timer = 0;//每一波產(chǎn)生的計時器float timer2 = 0;//產(chǎn)生敵人的速率public float rate;//敵人的預(yù)制體public GameObject enemeyPrefab;//每一波的時間public float timesofEachWave = 30;//每一波的已經(jīng)產(chǎn)生的數(shù)量private int count;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {timer2 += Time.deltaTime;//每隔一定時間產(chǎn)生一波敵人if (timer2 < timesofEachWave && count!=10){timer += Time.deltaTime;//每隔一定的時間產(chǎn)生一個波敵人if (timer > rate){Instantiate(enemeyPrefab, Vector3.zero, Quaternion.identity);count++;timer -= rate;}}//如果每一波產(chǎn)生的時間大于timesofEachWave秒 if (timer2>timesofEachWave){timer2 -= timesofEachWave;//當前的數(shù)量清零count = 0;}} }總結(jié)
以上是生活随笔為你收集整理的Unity_塔防游戏按波产生怪_063的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vim基本指令(一)
- 下一篇: cpuz测试分数天梯图_联想Z5跑分实测