Hi there. I am making day/night cycle. I am rotating the directional light and then i draw halo and trying to move it with sin and cos. It works just one problem i have is when it’s night i want the sun to move faster or to be on exact position and then update that position with cos and sin. But when i do that. It only start working again old position not updated position.
[Header("Object reference")]
public Transform stars;
[Header("Color changers")]
public Gradient nightDayColor;
public Gradient nightDayFogColor;
public AnimationCurve fogDensityCurve;
[Header("Current time of day")]
public float maxIntensity = 3f;
public float minIntensity = 0f;
public float minPoint = -0.2f;
public Vector3 dayRotateSpeed;
public Vector3 nightRotateSpeed;
private Light mainLight;
private Skybox sky;
private Material _sky;
public float fogScale = 1f;
[Header("Sun parameters")]
[SerializeField]
private float TimeofDay = 1;
[SerializeField]
private float amplitudeX = 2.0f;
[SerializeField]
private float amplitudeZ = 3.0f;
[SerializeField]
private float periodInSec = 120;
[SerializeField]
private float frequency = 2.0f;
private float twoPi = Mathf.PI * 2f;
[SerializeField]
private bool night = false;
[SerializeField]
private bool day = false;
public Vector3 oldPosition = new Vector3();
public Vector3 sunposition;
void Start()
{
mainLight = GetComponent<Light>();
_sky = RenderSettings.skybox;
frequency = 1 / periodInSec;
sunposition = transform.localPosition;
}
void Update()
{
stars.transform.rotation = transform.rotation;//rotate starts with sun
float tRange = 1 - minPoint; //total day time
float dot = Mathf.Clamp01((Vector3.Dot(mainLight.transform.forward, Vector3.down) - minPoint) / tRange);// from -1 to 1
float i = ((maxIntensity - minIntensity) * dot) + minIntensity;
float speed = Time.deltaTime*TimeofDay;
mainLight.intensity = i;
mainLight.color = nightDayColor.Evaluate(dot);//at particular point give excatcly that color :)
RenderSettings.ambientLight = mainLight.color;
RenderSettings.fogColor = nightDayFogColor.Evaluate(dot);
RenderSettings.fogDensity = fogDensityCurve.Evaluate(dot) * fogScale;
float x = amplitudeX*Mathf.Cos(twoPi*Time.time*frequency);
float z = amplitudeZ*Mathf.Sin(twoPi*Time.time*frequency);
//day
if (dot > 0)
{
day = true;
night = false;
transform.Rotate(dayRotateSpeed*speed);
}
//night
else
{
transform.Rotate(nightRotateSpeed * speed);
day = false;
night = true;
}
// transform.localPosition = new Vector3(Mathf.Cos(speed), 0, Mathf.Sin(speed));
if (day && !night)
{
sunposition = new Vector3(-x, z, 0);
}
else if (night && !day)
{
x = -60;
z = -20;
oldPosition = transform.localPosition = new Vector3(-60,-20,0);
}
}