gpt4 book ai didi

c# - Unity3D:如何控制生成器,等待波完成时不会停止实例化

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:30 26 4
gpt4 key购买 nike

Unity3D 2018.2.4 C#

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

我正在检查

if (destroyedEnemyCounter == enemyStartAmount)

因为我正在计算所有被摧毁的敌人以及 wave spawner 乘以 enemyStartAmount 的数量,但是即使我将 wave 转发放在 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;

}
}

// Main: Spawn Waves
IEnumerator SpawnWaves()
{
// Start when First Countdown isFinished
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < enemyStartAmount; i++)
{
// Each Wave is a different way
switch (waveCurrentCount)
{
case 1:
Transform spawnPoint = innerLevelSpawns[(int)Random.Range(0, innerLevelSpawns.Length)].transform;
Quaternion spawnRotation = Quaternion.identity;
Instantiate(enemyTypes[(int)Random.Range(0, 0)], spawnPoint.position, spawnRotation);
break;

...

case 8:
Transform spawnPoint8 = outerLevelSpawns[(int)Random.Range(0, outerLevelSpawns.Length)].transform;
Quaternion spawnRotation8 = Quaternion.identity;
Instantiate(enemyTypes[(int)Random.Range(0, 0)], spawnPoint8.position, spawnRotation8);
break;
default:
Transform spawnPoint9 = outerLevelSpawns[(int)Random.Range(0, outerLevelSpawns.Length)].transform;
Quaternion spawnRotation9 = Quaternion.identity;
Instantiate(enemyTypes[(int)Random.Range(0, enemyTypes.Length)], spawnPoint9.position, spawnRotation9);
break;
}
yield return new WaitForSeconds(spawnWait);
}

// Check if enemyCount has been destroyed -> then send next wave
enemiesInScene = GameObject.FindGameObjectsWithTag("Enemy");
enemiesInSceneText.text = "Total Spawned: " + enemiesInScene.Length.ToString();

// Next Wave -----------Hold Spawn when Wave Amount Destroyed--------------------------

if (destroyedEnemyCounter == enemyStartAmount)
{
Debug.Log("Next Wave Incoming");

// Multiply Enemy Count
enemyStartAmount = enemyStartAmount * multiply;

// Count Waves
waveCurrentCount = ++waveCurrentCount;

yield return new WaitForSeconds(waveWait);
}
}
}

最佳答案

替换

if (destroyedEnemyCounter == enemyStartAmount)

yield return new WaitUntil(() => destroyedEnemyCounter == enemyStartAmount);

关于c# - Unity3D:如何控制生成器,等待波完成时不会停止实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51938270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com