Adding trick spawn and movement to Experiment scene
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Spawner : MonoBehaviour
|
||||
@@ -7,6 +8,9 @@ public class Spawner : MonoBehaviour
|
||||
[SerializeField] private GameObject objectToSpawn;
|
||||
// Time interval between spawns
|
||||
public float spawnInterval = 1f;
|
||||
[SerializeField] private int maxAlive = 0;
|
||||
|
||||
private readonly List<GameObject> spawned = new List<GameObject>();
|
||||
|
||||
void Start()
|
||||
{
|
||||
@@ -21,8 +25,32 @@ public class Spawner : MonoBehaviour
|
||||
{
|
||||
// Wait for the specified interval
|
||||
yield return new WaitForSeconds(spawnInterval);
|
||||
if (objectToSpawn == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (maxAlive > 0 && CountAlive() >= maxAlive)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Spawn the object at the spawner's position and rotation
|
||||
Instantiate(objectToSpawn, transform.position, Quaternion.identity);
|
||||
var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity);
|
||||
spawned.Add(instance);
|
||||
}
|
||||
}
|
||||
|
||||
private int CountAlive()
|
||||
{
|
||||
for (var i = spawned.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (spawned[i] == null)
|
||||
{
|
||||
spawned.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
return spawned.Count;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user