Syncing Sunrise & Countdown Timer

Hi, I’m developing a videogame which starts at sunset and restarts the scene at sunrise. I’m using two scripts currently, AutoIntensity.cs (sunset/sunrise) and Timer.cs which do the job but they are not in sync (I have to manually insert the time it takes for the sun to rise). I would like to have some suggestions how I could sync the time between these two.

PS: This is my first thread, so please bear with me :slight_smile:

  • AutoIntensity.cs
using UnityEngine;
using System.Collections;

public class AutoIntensity : MonoBehaviour {

    public Gradient nightDayColor;

    public float maxIntensity = 3f;
    public float minIntensity = 0f;
    public float minPoint = -0.2f;

    public float maxAmbient = 1f;
    public float minAmbient = 0f;
    public float minAmbientPoint = -0.2f;


    public Gradient nightDayFogColor;
    public AnimationCurve fogDensityCurve;
    public float fogScale = 1f;

    public float dayAtmosphereThickness = 0.4f;
    public float nightAtmosphereThickness = 0.87f;

    public Vector3 dayRotateSpeed;
    public Vector3 nightRotateSpeed;

    float skySpeed = 1;


    Light mainLight;
    Skybox sky;
    Material skyMat;

    void Start ()
    {

        mainLight = GetComponent<Light>();
        skyMat = RenderSettings.skybox;

    }

    void Update ()
    {

        float tRange = 1 - minPoint;
        float dot = Mathf.Clamp01 ((Vector3.Dot (mainLight.transform.forward, Vector3.down) - minPoint) / tRange);
        float i = ((maxIntensity - minIntensity) * dot) + minIntensity;

        mainLight.intensity = i;

        tRange = 1 - minAmbientPoint;
        dot = Mathf.Clamp01 ((Vector3.Dot (mainLight.transform.forward, Vector3.down) - minAmbientPoint) / tRange);
        i = ((maxAmbient - minAmbient) * dot) + minAmbient;
        RenderSettings.ambientIntensity = i;

        mainLight.color = nightDayColor.Evaluate(dot);
        RenderSettings.ambientLight = mainLight.color;

        RenderSettings.fogColor = nightDayFogColor.Evaluate(dot);
        RenderSettings.fogDensity = fogDensityCurve.Evaluate(dot) * fogScale;

        i = ((dayAtmosphereThickness - nightAtmosphereThickness) * dot) + nightAtmosphereThickness;
        skyMat.SetFloat ("_AtmosphereThickness", i);

        if (dot > 0)
            transform.Rotate (dayRotateSpeed * Time.deltaTime * skySpeed);
        else
            transform.Rotate (nightRotateSpeed * Time.deltaTime * skySpeed);

        if (Input.GetKeyDown (KeyCode.K)) skySpeed *= 1f;
        if (Input.GetKeyDown (KeyCode.L)) skySpeed *= 2f;


    }
}
  • Timer.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public int timeLeft = 5;
    public Text countdownText;

    // Use this for initialization
    void Start ()
    {
        StartCoroutine ("LoseTime");
   
    }
   
    // Update is called once per frame
    void Update ()
    {
        countdownText.text = ("TIME UNTIL SUNRISE: " + timeLeft);
   
        if (timeLeft<= 0)
        {
            StopCoroutine ("LoseTime");
            countdownText.text = "THIS IS THE END.";
            Application.LoadLevel("RaveFiction_empty2");
        }
    }

    IEnumerator LoseTime()
    {
        while (true) {
            yield return new WaitForSeconds (1);
            timeLeft--;
        }
    }
}

Instead of tracking time, why not just use Time.time and scale it appropriately so you have whatever day length cycle you want? Or if that is problematic because you can’t control initial time of day, then just track a single float value, and every frame update it by Time.deltaTime.

If you want a 24-second day, you would just modulo that master time value by 24 and use the value as the hour of the day. You can choose what you want to be the start of day or night, just have less-than-greater-than checks.

That way you can also use AnimationCurve objects to change the values of other things, like light intensity, or colors of light (with the color-ramp equivalent of AnimationCurve; I forget the object name but ParticleSystems use them), and then index it off the 0 to 1 scaled value of where you are in the day/night cycle.