DayNightCycle.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using UnityEngine;
  2. public class DayNightCycle : MonoBehaviour
  3. {
  4. [Header("Lights")]
  5. public Transform sunLight;
  6. public Transform moonLight;
  7. [Header("Skybox Materials")]
  8. public Material daySkybox;
  9. public Material nightSkybox;
  10. [Header("Rotation Settings")]
  11. public float startRotationX = 10f;
  12. public float endRotationX = 350f;
  13. [Range(0, 1)]
  14. public float duskPercentage = 0.5f;
  15. private bool hasTriggeredDusk = false;
  16. void Update()
  17. {
  18. if (GameManager.Instance != null)
  19. {
  20. float totalDayTime = 300f;
  21. float timePassed = totalDayTime - GameManager.Instance.timeRemaining;
  22. float timePercentage = timePassed / totalDayTime;
  23. // 1. Handle Light Rotation
  24. float currentSunX = Mathf.Lerp(startRotationX, endRotationX, timePercentage);
  25. sunLight.rotation = Quaternion.Euler(currentSunX, -30f, 0f);
  26. moonLight.rotation = Quaternion.Euler(currentSunX + 180f, -30f, 0f);
  27. // 2. Handle Skybox Swap
  28. // We swap to the night skybox a bit before the dusk enemies spawn
  29. if (timePercentage > 0.45f)
  30. {
  31. RenderSettings.skybox = nightSkybox;
  32. }
  33. else
  34. {
  35. RenderSettings.skybox = daySkybox;
  36. }
  37. // 3. Dusk Enemies Trigger
  38. if (timePercentage >= duskPercentage && !hasTriggeredDusk)
  39. {
  40. hasTriggeredDusk = true;
  41. TerrainObjectSpawner spawner = FindObjectOfType<TerrainObjectSpawner>();
  42. if (spawner != null) spawner.SpawnExtraEnemies(2);
  43. }
  44. }
  45. }
  46. }