using System.Collections; using System.Collections.Generic; using UnityEngine; public enum RotationAxis { X, Y } public class DayTimeManager : MonoBehaviour { public Light sunlight; public float secondsPerDay; public RotationAxis rotAxis = RotationAxis.X; public float maxAngle = 50.0f; private float _elapsedTime; public float timeFactor { get { return _elapsedTime / secondsPerDay; } } public DayTimeLabel dayTimeLabel { get { if (timeFactor >= 6.0f / 24.0f && timeFactor < 11.0f / 24.0f) { return DayTimeLabel.Morning; } if (timeFactor >= 11.0f / 24.0f && timeFactor < 17.0f / 24.0f) { return DayTimeLabel.Noon; } if (timeFactor >= 17.0f / 24.0f && timeFactor < 22.0f / 24.0f) { return DayTimeLabel.Evening; } if (timeFactor >= 23.0f / 24.0f && timeFactor < 24.0f / 24.0f) { return DayTimeLabel.Night; } if (timeFactor >= 0.0f / 24.0f && timeFactor < 6.0f / 24.0f) { return DayTimeLabel.Night; } return DayTimeLabel.Morning; } } private float _timeDirection = 1.0f; private Vector3 _position; private Vector3 _rotation; void Awake () { } // Use this for initialization void Start () { _position = sunlight.gameObject.transform.position; _rotation = sunlight.gameObject.transform.rotation.ToEulerAngles(); Debug.Log("Got Rotation " + _rotation); } // Update is called once per frame void Update () { _elapsedTime += Time.deltaTime; if (_elapsedTime >= secondsPerDay) { _elapsedTime = 0; } float intensityAngle = this.timeFactor * Mathf.PI; sunlight.intensity = Mathf.Sin(intensityAngle); float rotationDirection = Mathf.Sign(this.timeFactor - 0.5f); Debug.Log("DayTimeLabel: " + dayTimeLabel.ToString()); switch (rotAxis) { case RotationAxis.X: { sunlight.gameObject.transform.rotation.SetEulerAngles(_rotation.x + rotationDirection * maxAngle, _rotation.y, _rotation.z); } break; case RotationAxis.Y: { sunlight.gameObject.transform.rotation.SetEulerAngles(_rotation.x, _rotation.y + rotationDirection * maxAngle, _rotation.z); } break; } } float FactorToDayTime(float factor) { return factor * 24.0f; } float DayTimeToFactor(float dayTime) { return dayTime / 24.0f; } }