| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using UnityEngine;
- public class TerrainObjectSpawner : MonoBehaviour
- {
- public GameObject orbPrefab;
- public int orbCount = 20;
- public LayerMask terrainLayer;
- public float meshMaxHeight = 25f;
- public void SpawnOrbs()
- {
- Debug.Log("Spawner: Starting process...");
- if (orbPrefab == null)
- {
- Debug.LogError("Spawner: ORB PREFAB IS MISSING IN INSPECTOR!");
- return;
- }
- int spawned = 0;
- int attempts = 0;
- while (spawned < orbCount && attempts < 500)
- {
- attempts++;
- // Lague's map is centered, so we use a wide range
- float x = Random.Range(-100f, 100f);
- float z = Random.Range(-100f, 100f);
- Vector3 rayStart = new Vector3(x, 500f, z); // Start very high up
- // TEST RAYCAST
- if (Physics.Raycast(rayStart, Vector3.down, out RaycastHit hit, 1000f, terrainLayer))
- {
- // If it hits, spawn the orb immediately for now (ignoring height logic)
- Instantiate(orbPrefab, hit.point + Vector3.up * 1.5f, Quaternion.identity);
- spawned++;
- }
- }
- if (spawned == 0)
- {
- Debug.LogWarning("Spawner: 0 Orbs spawned. Raycasts hit NOTHING. Check your Layer settings!");
- }
- else
- {
- Debug.Log("Spawner: Successfully spawned " + spawned + " orbs!");
- }
- }
- }
|