GenerateOrbs.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro.Examples;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. public class GenerateOrbs : MonoBehaviour
  7. {
  8. public GameObject orb;
  9. [SerializeField] int spawnRadius;
  10. [SerializeField] int maximumOrbs;
  11. //public GameObject[] orbs;
  12. private void Start()
  13. {
  14. //orbs = new GameObject[maximumOrbs];
  15. }
  16. public void StartGenerating(Transform playerTransform)
  17. {
  18. StartCoroutine(GenerateOrb(playerTransform));
  19. }
  20. IEnumerator GenerateOrb(Transform playerTransform)
  21. {
  22. int spawnedOrbCounter = 0;
  23. while (spawnedOrbCounter < maximumOrbs)
  24. {
  25. float spawnX = Random.Range(-spawnRadius, spawnRadius) + playerTransform.position.x;
  26. float spawnZ = Random.Range(-spawnRadius, spawnRadius) + playerTransform.position.z;
  27. Vector3 rayOrigin = new Vector3(spawnX, 500f, spawnZ);
  28. if(Physics.Raycast(rayOrigin, Vector3.down, out RaycastHit hit, 1000f)){
  29. if (hit.collider.CompareTag("Floor"))
  30. {
  31. Vector3 spawnPosition = hit.point + new Vector3(0, 1f, 0);
  32. //orbs[spawnedOrbCounter] =
  33. Instantiate(orb, spawnPosition, Quaternion.identity);
  34. spawnedOrbCounter++;
  35. yield return null;
  36. }
  37. if (spawnedOrbCounter < maximumOrbs)
  38. {
  39. yield return null;
  40. }
  41. //Debug.Log("All orbs successfully spawned!");
  42. }
  43. /* else
  44. {
  45. Debug.DrawRay(rayOrigin, Vector3.down * 1000f, Color.red, 5f);
  46. Debug.Log($"No hit at {spawnX}, {spawnZ}");
  47. }*/
  48. }
  49. }
  50. }