我如何重复生成它们之间 0.1 秒的对象?
How do I spawn repeatedly object whot 0.1 seconds between them?
我想在 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;
}
}
}
使用 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);
}
}
我想在 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;
}
}
}
使用 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);
}
}