2025-11-25 09:25:11 +01:00

33 lines
1.2 KiB
C#

using System.Collections;
using UnityEngine;
public class SwarmSpawn : MonoBehaviour {
public int count = 1;
public GameObject boidPrefab;
public Vector3 spawnAreaSize = new(0.5f, 0.5f, 0.5f); // Size of the area to spawn the prefab
public float minDelay = 1f; // Minimum delay between spawns
public float maxDelay = 5f; // Maximum delay between spawns
void Start() {
StartCoroutine(SpawnPrefab());
}
IEnumerator SpawnPrefab() {
for (int i = 0; i < count; i++) {
float delay = Random.Range(minDelay, maxDelay);
yield return new WaitForSeconds(delay);
// Generate a random local position within the specified area
Vector3 randomPosition = new Vector3(
Random.Range(-spawnAreaSize.x / 2, spawnAreaSize.x / 2),
Random.Range(-spawnAreaSize.y / 2, spawnAreaSize.y / 2),
Random.Range(-spawnAreaSize.z / 2, spawnAreaSize.z / 2)
);
// Instantiate the prefab at the random position relative to the spawner
Instantiate(boidPrefab, transform.position + randomPosition, Random.rotation);
}
}
}