TerrainObjectSpawner.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. public class TerrainObjectSpawner : MonoBehaviour
  3. {
  4. public GameObject orbPrefab;
  5. public int orbCount = 20;
  6. public LayerMask terrainLayer;
  7. public float meshMaxHeight = 25f;
  8. public void SpawnOrbs()
  9. {
  10. Debug.Log("Spawner: Starting process...");
  11. if (orbPrefab == null)
  12. {
  13. Debug.LogError("Spawner: ORB PREFAB IS MISSING IN INSPECTOR!");
  14. return;
  15. }
  16. int spawned = 0;
  17. int attempts = 0;
  18. while (spawned < orbCount && attempts < 500)
  19. {
  20. attempts++;
  21. // Lague's map is centered, so we use a wide range
  22. float x = Random.Range(-100f, 100f);
  23. float z = Random.Range(-100f, 100f);
  24. Vector3 rayStart = new Vector3(x, 500f, z); // Start very high up
  25. // TEST RAYCAST
  26. if (Physics.Raycast(rayStart, Vector3.down, out RaycastHit hit, 1000f, terrainLayer))
  27. {
  28. // If it hits, spawn the orb immediately for now (ignoring height logic)
  29. Instantiate(orbPrefab, hit.point + Vector3.up * 1.5f, Quaternion.identity);
  30. spawned++;
  31. }
  32. }
  33. if (spawned == 0)
  34. {
  35. Debug.LogWarning("Spawner: 0 Orbs spawned. Raycasts hit NOTHING. Check your Layer settings!");
  36. }
  37. else
  38. {
  39. Debug.Log("Spawner: Successfully spawned " + spawned + " orbs!");
  40. }
  41. }
  42. }