我想在 0.1 秒之間生成一個 100 個物件。我有這個,但它們仍然會同時生成。
private float timer = 0.0f;
bool goSpawn = false;
private void Update()
{
if (goSpawn)
{
timer = Time.deltaTime;
}
}
public void spawnAnimals()
{
goSpawn = true;
for (int i = 0; i < 100;)
{
if (timer > 2f)
{
RndSpawnPos = new Vector3(Random.Range(3.30f, 5.70f), 0.78f, Random.Range(-3.00f, 3.01f));
Instantiate(animal, RndSpawnPos, Quaternion.identity);
i ;
timer = 0.0f;
}
}
}
uj5u.com熱心網友回復:
使用IEnumerator
. 我舉了一個例子來解決這個問題:
public void Start() => StartCoroutine(SpawnObject(100, .1f));
public IEnumerator SpawnObject(int count, float intervalTime)
{
for (var i = 0; i < count; i )
{
Instantiate(" something in here...");
yield return new WaitForSeconds(intervalTime);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477589.html
上一篇:復制組件的正確方法是什么?