使用下面的代码我将如何对其进行编码,而不是从阵列中随机产生敌人,它会产生敌人1,然后在说出10秒延迟之后产生敌人2,但仍使用随机产生的延迟和死亡时间?
可能很简单,但我无法看到它..
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public GameObject[] enemies;
private void Start()
{
StartCoroutine("SpawnHandler");
}
private IEnumerator SpawnHandler()
{
float spawnDelay;
int thisEnemy = 0;
GameObject cachedEnemy;
float dieTime;
while (thisEnemy < 2)
{
spawnDelay = Random.Range(3f,6f); //random time, from 1-3
dieTime = Random.Range(3f,3f);
yield return new WaitForSeconds(spawnDelay); //wait that time
cachedEnemy = (GameObject)Instantiate(enemies[thisEnemy],
transform.position, transform.rotation);//spawn enemy, cache him
StartCoroutine(Kill(dieTime, cachedEnemy));
}
}
private IEnumerator Kill(float wait, GameObject enemy)
{
yield return new WaitForSeconds(wait);
Destroy(enemy);
}
}
重申我需要完成的事情: - &gt; randoms产生延迟 - &gt;产生敌人1 - &gt;延迟(比如10秒) - &gt;产生敌人2 - &gt;回路;
希望清楚,任何帮助都非常感谢, 提前致谢, 最大
答案 0 :(得分:1)
只需更改while
循环的条件:
private IEnumerator SpawnHandler(){
float spawnDelay;
int thisEnemy = 0;
GameObject cachedEnemy;
float dieTime;
while (thisEnemy < 2)
{
spawnDelay = Random.Range(3f,6f); //random time, from 1-3
dieTime = Random.Range(3f,3f);
yield return new WaitForSeconds(spawnDelay); //wait that time
cachedEnemy = (GameObject)Instantiate(enemies[thisEnemy], transform.position, transform.rotation);//spawn enemy, cache him
StartCoroutine(Kill(dieTime, cachedEnemy));
thisEnemy++;
}
}