OrbManager.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. public class OrbManager : MonoBehaviour
  5. {
  6. public GameObject prefab;
  7. public int orbsToSpawn;
  8. private int orbsSpawned;
  9. public int spawnRadius;
  10. private GameObject player;
  11. public IEnumerator StartSpawn()
  12. {
  13. player = GameObject.Find("Player");
  14. orbsSpawned = 0;
  15. yield return new WaitForSeconds(5);
  16. while (orbsSpawned < orbsToSpawn)
  17. {
  18. TrySpawnOrb();
  19. }
  20. }
  21. void TrySpawnOrb()
  22. {
  23. float xRadius = UnityEngine.Random.Range(-spawnRadius, spawnRadius);
  24. float zRadius = UnityEngine.Random.Range(-spawnRadius, spawnRadius);
  25. Vector3 randomRadiusVector = new Vector3(xRadius, 0, zRadius);
  26. Vector3 rayStart = player.transform.position + Vector3.up * 300 + randomRadiusVector;
  27. if (Physics.Raycast(rayStart, Vector3.down, out RaycastHit hit, 600f))
  28. {
  29. if (hit.point.y < OrbData.maxHeight && hit.point.y > OrbData.minHeight)
  30. {
  31. UnityEngine.Object.Instantiate(prefab, hit.point + new Vector3(0f, 0.75f, 0f), Quaternion.identity);
  32. orbsSpawned++;
  33. }
  34. }
  35. }
  36. }