using UnityEngine; using System.Collections.Generic; // Added for List public class TerrainObjectSpawner : MonoBehaviour { public GameObject orbPrefab; public int orbCount = 20; public LayerMask terrainLayer; public float meshMaxHeight = 25f; public GameObject enemyPrefab; public int enemyCount = 5; public float minDistanceBetweenOrbs = 15f; public void SpawnOrbs() { Debug.Log("Spawner: Starting process..."); if (orbPrefab == null) return; int spawned = 0; int attempts = 0; List spawnedPositions = new List(); while (spawned < orbCount && attempts < 1000) // Increased attempts for better scattering { attempts++; float x = Random.Range(-100f, 100f); float z = Random.Range(-100f, 100f); Vector3 rayStart = new Vector3(x, 500f, z); if (Physics.Raycast(rayStart, Vector3.down, out RaycastHit hit, 1000f, terrainLayer)) { // Check distance from previously spawned orbs bool isTooClose = false; foreach (Vector3 pos in spawnedPositions) { if (Vector3.Distance(hit.point, pos) < minDistanceBetweenOrbs) { isTooClose = true; break; } } if (!isTooClose) { Instantiate(orbPrefab, hit.point + Vector3.up * 1.5f, Quaternion.identity); spawnedPositions.Add(hit.point); spawned++; } } } Debug.Log("Spawner: Successfully spawned " + spawned + " scattered orbs!"); } public void SpawnEnemies() { int spawned = 0; int attempts = 0; while (spawned < enemyCount && attempts < 500) { attempts++; float x = Random.Range(-100f, 100f); float z = Random.Range(-100f, 100f); Vector3 rayStart = new Vector3(x, 500f, z); if (Physics.Raycast(rayStart, Vector3.down, out RaycastHit hit, 1000f, terrainLayer)) { if (hit.point.y > meshMaxHeight * 0.5f) { Instantiate(enemyPrefab, hit.point + Vector3.up * 2f, Quaternion.identity); spawned++; } } } } public void SpawnExtraEnemies(int amount) { int extraSpawned = 0; while (extraSpawned < amount) { float x = Random.Range(-100f, 100f); float z = Random.Range(-100f, 100f); Vector3 rayStart = new Vector3(x, 500f, z); if (Physics.Raycast(rayStart, Vector3.down, out RaycastHit hit, 1000f, terrainLayer)) { // Spawn them on hills as usual if (hit.point.y > meshMaxHeight * 0.4f) { Instantiate(enemyPrefab, hit.point + Vector3.up * 2f, Quaternion.identity); extraSpawned++; } } } } }