Adding trick spawn and movement to Experiment scene

This commit is contained in:
hz
2026-01-31 18:35:14 -06:00
parent 0c031b558e
commit c24846d568
7 changed files with 298 additions and 75 deletions
+29 -1
View File
@@ -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;
}
}