将另一个敌人添加到协程?

时间:2014-06-30 16:07:12

标签: c# unity3d

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;
    int nextEnemy = 1;
    GameObject cachedEnemy;
    GameObject cachedEnemyTwo;
    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));
        //Somewhere here 
    }
}


private IEnumerator Kill(float wait, GameObject enemy){
    yield return new WaitForSeconds(wait);
    Destroy(enemy);

    //or here i need to include the "int nextEnemy". I need it to come after the cached enemy so it needs to have the same random.range value!
    // I need it to have the same value so it spawns exactly 1 second after the cached enemy, like a pair so they spawn from the same
    // random value (very important). So as soon as enemy 1 dieTime runs out, "int nextEnemy" spawns and  runs for 1 second after.
}
}

计划是这样的:int thisEnemy使用spawndelay生成,它运行3秒钟(坚持示例,dieTime)。然后,当这个死亡后,下一个敌人产生1秒钟后,这些必须像对!然后循环。

感谢您的帮助,这真的让我的笼子嘎嘎作响! 最大

1 个答案:

答案 0 :(得分:0)

StartCoroutine(Kill(dieTime, cachedEnemy, spawnDelay, cachedEnemyTwo, nextEnemy))// <-- pass needed values to Kill

并更改杀戮:

private IEnumerator Kill(float wait, GameObject enemy, float spawnDelay, GameObject enemy2, int enemyIndex){
    yield return new WaitForSeconds(wait);
    Destroy(enemy);
    yield return new WaitForSeconds(spawnDelay or 1 second what you need...);
    cachedEnemyTwo = (GameObject)Instantiate(enemies[enemyIndex], transform.position, transform.rotation);
}

对不起,这就是我理解你的问题的方式,我对IEnumerator很陌生,希望这会有所帮助。