Files

34 lines
1.0 KiB
C#

using System;
using UnityEngine;
public class SunRotation : MonoBehaviour
{
Light directionalLight;
public float offset = 270f; // Adjust for sunrise/sunset timing
void Start()
{
directionalLight = GetComponent<Light>();
}
void Update()
{
DateTime dateTime = new DateTime(
DateTime.Today.Year,
DateTime.Today.Month,
DateTime.Today.Day,
GameManager.Instance.TimeManager.GetCurrentHour(),
GameManager.Instance.TimeManager.GetCurrentMinute(), 0);
// Get current time and convert to a fraction of a day (0 to 1)
float timeOfDay = (float)dateTime.TimeOfDay.TotalDays;
float sunRotation = timeOfDay * 360f + offset;
transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(sunRotation, 0, 0), 10);
// adjust light intensity based on sun height
float sunElevation = Mathf.Sin(sunRotation * Mathf.Deg2Rad);
directionalLight.intensity = Mathf.Max(0, sunElevation);
}
}