僅當場上的敵人少于 10 個時,我如何修改統一 c# 上的代碼以每 30 秒產生一次敵人。
目前我在一個列舉器中有一個while回圈,它將敵人產生到10個但只在開始時被呼叫,我需要這個函式來產生敵人,當敵人的數量達到1時。
謝謝。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateEnemies : MonoBehaviour
{
public GameObject theEnemy;
public int xPos;
public int zPos;
public int enemyCount;
void Start()
{
StartCoroutine(SpawnEnemy());
}
IEnumerator SpawnEnemy()
{
while (enemyCount < 10)
{
xPos = Random.Range(153, 203);
zPos = Random.Range(-76, 76);
Instantiate(theEnemy, new Vector3(xPos, 9, zPos), Quaternion.identity);
yield return new WaitForSeconds(0.9f);
enemyCount = 1;
}
}
}
uj5u.com熱心網友回復:
我根本沒有測驗過這段代碼
嗨 Obito,讓我向您展示這段代碼,看看您是否可以實作它:
[Header("Spawning Properties")]
public GameObject enemyPrefab;
public Vector3 spawnPoint;
public int maxEnemies = 10;
public int minEnemies = 1;
//This is the time between each spawn.
public float spawnDelay = 0.9f;
public spawnTimer = 30f;
//Every enemy *needs* an 'Enemy' script, or tag, to identify what is an enemy.
public int EnemyCount => GameObject.FindObjectsOfType<Enemy>();
private void Awake()
{
spawnTimer = 30f;
}
private void Update()
{
spawnTimer -= Time.deltaTime;
//"Do I need to start spawning enemies?"
if(EnemyCount <= minEnemies && spawnTimer <= 0) {
StartCoroutine(SpawnEnemies());
}
}
private IEnumerator SpawnEnemies()
{
while(EnemyCount <= maxEnemies)
{
Instantiate(enemyPrefab, spawnPoint, Quaternion.identity);
yield return new WaitForSeconds(spawnDelay);
Debug.Log("Enemy " EnemyCount "/" maxEnemies " spawned.");
}
spawnTimer = 30f;
}
提示:如果你想制作一個“每波敵人”系統,你應該嘗試這樣做:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Spawning Properties")]
public GameObject enemyPrefab;
public Vector3 spawnPoint;
public int[] wavesCount = { 10, 14, 23, 40 };
public int minEnemiesPerWave = 1;
public int currentWave = 0;
//This is the time between each spawn.
public float spawnDelay = 0.9f;
private float spawnTimer = 30f;
//Every enemy *needs* an 'Enemy' script, or tag, to identify what is an enemy.
public int EnemyCount => GameObject.FindObjectsOfType<Enemy>();
private void Start() {
spawnTimer = 30f;
}
private void Update()
{
spawnTimer -= Time.deltaTime;
//"Do I need to start spawning enemies?"
if(EnemyCount <= minEnemiesPerWave && spawnTimer <= 0) {
StartCoroutine(SpawnEnemies());
}
}
private IEnumerator SpawnEnemies()
{
while(EnemyCount <= wavesCount[currentWave])
{
Instantiate(enemyPrefab, spawnPoint, Quaternion.identity);
yield return new WaitForSeconds(spawnDelay);
Debug.Log("Enemy " EnemyCount "/" maxEnemies " spawned.");
}
spawnTimer = 30f;
currentWave ;
}
}
uj5u.com熱心網友回復:
只需將您的代碼包裝在一個無限回圈中并執行例如
public class GenerateEnemies : MonoBehaviour
{
public GameObject theEnemy;
public int xPos;
public int zPos;
public int enemyCount;
IEnumerator Start()
{
// As long as you yield within this is totally fine in an IEnumerator
while(true)
{
// If there is already 10 or more
if(enemyCount >= 10)
{
// wait a frame and check again
yield return null;
continue;
}
xPos = Random.Range(153, 203);
zPos = Random.Range(-76, 76);
Instantiate(theEnemy, new Vector3(xPos, 9, zPos), Quaternion.identity);
enemyCount ;
// waiting 30 seconds then check again
yield return new WaitForSeconds(30);
}
}
}
當然,假設這enemyCount是從外部減少的地方。
或者從你所說的你想要額外等到數量達到1然后再次開始產卵,這樣你就可以添加
...
enemyCount ;
if(enemyCount >= 10)
{
yield return new WaitUntil(() => enemyCount <= 1);
}
else
{
yield return new WaitForSeconds(30);
}
...
因此,您在生成敵人之間等待 30 秒,但是一旦達到 10 個敵人,您就等到數量再次達到1或更少
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416066.html
標籤:
