Unity3D:如何控制 spawner,在等待 wave 完成时不会停止实例化

Unity3D 2018.2.4 C#


在这个 spawner 代码中,它一直在实例化。我如何阻止生成从实例化直到所有生成的敌人都被摧毁。


我正在检查


if (destroyedEnemyCounter == enemyStartAmount)

因为我正在计算所有被摧毁的敌人以及波生成器乘以敌人开始数量的多少但是即使我将波转发放在 if 语句中,代码也会继续实例化。


在该波/轮的计数被破坏并能够移动到下一个波/轮之前,我将如何添加阻止生成器实例化的限制?


这是我到目前为止的代码


private int enemyStartAmount = 2;

private int multiply = 2;

private float startWait = 30.0f;

private float spawnWait = 2.0f;

private float waveWait = 28.0f;


//  Start

private void OnEnable()

{

    startCountDownTimer = startWait;

    waveCurrentCount = 1;


    StartCoroutine(SpawnWaves());

}


//  Update

private void FixedUpdate()

{

    //  For Text: StartTimer

    if (isStartTimerFinished == false)

    {

        startCountDownTimer -= Time.deltaTime;

    }


    //  For Text: When StartTimer isFinished

    if (isStartTimerFinished == false && startCountDownTimer <= 0f)

    {

        isStartTimerFinished = true;

    }


    //  For Text: When startTimer ends and Spawner Starts

    if (isStartTimerFinished == true)

    {

        //  Game Timer

        stopWatchTimer += Time.deltaTime;


    }

}


慕姐8265434
浏览 393回答 2
2回答

慕的地10843

代替if&nbsp;(destroyedEnemyCounter&nbsp;==&nbsp;enemyStartAmount)和yield&nbsp;return&nbsp;new&nbsp;WaitUntil(()&nbsp;=>&nbsp;destroyedEnemyCounter&nbsp;==&nbsp;enemyStartAmount);

德玛西亚99

检查后if (destroyedEnemyCounter == enemyStartAmount)循环将再次开始,再次生成enemyStartAmount。您可以做的是在生成逻辑周围添加一个条件,例如bool readyForNextWave将您设置为trueinsideif (destroyedEnemyCounter == enemyStartAmount)if(readyForNextWave){&nbsp; &nbsp; readyForNextWave = false;&nbsp; &nbsp; for (int i = 0; i < enemyStartAmount; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; Each Wave is a different way&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; yield return new WaitForSeconds(spawnWait);&nbsp; &nbsp; }}// ...if (destroyedEnemyCounter == enemyStartAmount){&nbsp; &nbsp; readyForNextWave = true;&nbsp; &nbsp; Debug.Log("Next Wave Incoming");&nbsp; &nbsp; // ...}
打开App,查看更多内容
随时随地看视频慕课网APP