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
- 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--;
}
}
}