- Home /
Day And Night Clock issue
I am working with a day and night and it works great but when set to a 24 hour day the clock counts up to 24 hours. Instead I want the clock to work like a normal clock would and at 12 noon and 12 mindnight change over to 1am and 1pm. I am not sure how to go about fixing this in.
using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections;
public class DayNightController : MonoBehaviour
{
/// number of seconds in a day
public float dayCycleLength = 1440;
/// current time in game time (0 - dayCycleLength).
public float currentCycleTime = 0;
/// number of hours per day.
public float hoursPerDay;
/// The rotation pivot of Sun
public Transform rotation;
/// current day phase
public DayPhase currentPhase;
/// Dawn occurs at currentCycleTime = 0.0f, so this offsets the WorldHour time to make
/// dawn occur at a specified hour. A value of 3 results in a 5am dawn for a 24 hour world clock.
public float dawnTimeOffset;
/// calculated hour of the day, based on the hoursPerDay setting.
public int worldTimeHour;
/// calculated minutes of the day, based on the hoursPerDay setting.
public int minutes;
private float timePerHour ;
/// The scene ambient color used for full daylight.
public Color fullLight = new Color(253.0f / 255.0f, 248.0f / 255.0f, 223.0f / 255.0f);
/// The scene ambient color used for full night.
public Color fullDark = new Color(32.0f / 255.0f, 28.0f / 255.0f, 46.0f / 255.0f);
/// The scene fog color to use at dawn and dusk.
public Color dawnDuskFog = new Color(133.0f / 255.0f, 124.0f / 255.0f, 102.0f / 255.0f);
/// The scene fog color to use during the day.
public Color dayFog = new Color(180.0f / 255.0f, 208.0f / 255.0f, 209.0f / 255.0f);
/// The scene fog color to use at night.
public Color nightFog = new Color(12.0f / 255.0f, 15.0f / 255.0f, 91.0f / 255.0f);
/// The calculated time at which dawn occurs based on 1/4 of dayCycleLength.
private float dawnTime;
/// The calculated time at which day occurs based on 1/4 of dayCycleLength.
private float dayTime;
/// The calculated time at which dusk occurs based on 1/4 of dayCycleLength.
private float duskTime;
/// The calculated time at which night occurs based on 1/4 of dayCycleLength.
private float nightTime;
/// One quarter the value of dayCycleLength.
private float quarterDay;
private float halfquarterDay;
/// The specified intensity of the directional light, if one exists. This value will be
/// faded to 0 during dusk, and faded from 0 back to this value during dawn.
private float lightIntensity;
/// Initializes working variables and performs starting calculations.
void Initialize()
{
quarterDay = dayCycleLength * 0.25f;
halfquarterDay = dayCycleLength * 0.125f;
dawnTime = 0.0f;
dayTime = dawnTime + halfquarterDay;
duskTime = dayTime + quarterDay + halfquarterDay;
nightTime = duskTime + halfquarterDay;
timePerHour = dayCycleLength/hoursPerDay;
if (light != null)
{ lightIntensity = light.intensity; }
}
/// Sets the script control fields to reasonable default values for an acceptable day/night cycle effect.
void Reset()
{
dayCycleLength = 120.0f;
hoursPerDay = 24.0f;
dawnTimeOffset = 3.0f;
fullDark = new Color(32.0f / 255.0f, 28.0f / 255.0f, 46.0f / 255.0f);
fullLight = new Color(253.0f / 255.0f, 248.0f / 255.0f, 223.0f / 255.0f);
dawnDuskFog = new Color(133.0f / 255.0f, 124.0f / 255.0f, 102.0f / 255.0f);
dayFog = new Color(180.0f / 255.0f, 208.0f / 255.0f, 209.0f / 255.0f);
nightFog = new Color(12.0f / 255.0f, 15.0f / 255.0f, 91.0f / 255.0f);
}
// Use this for initialization
void Start()
{
Initialize();
}
void OnGUI(){
string jam = worldTimeHour.ToString();
string menit = minutes.ToString();
if (worldTimeHour < 10){
jam = "0" + worldTimeHour;
}
if (minutes < 10){
menit = "0"+minutes;
}
GUI.Button(new Rect(500, 20, 100, 26), currentPhase.ToString() +" : "+jam+":"+menit);
}
// Update is called once per frame
void Update()
{
// Rudementary phase-check algorithm:
if (currentCycleTime > nightTime && currentPhase == DayPhase.Dusk)
{
SetNight();
}
else if (currentCycleTime > duskTime && currentPhase == DayPhase.Day)
{
SetDusk();
}
else if (currentCycleTime > dayTime && currentPhase == DayPhase.Dawn)
{
SetDay();
}
else if (currentCycleTime > dawnTime && currentCycleTime < dayTime && currentPhase == DayPhase.Night)
{
SetDawn();
}
// Perform standard updates:
UpdateWorldTime();
UpdateDaylight();
UpdateFog();
// Update the current cycle time:
currentCycleTime += Time.deltaTime;
currentCycleTime = currentCycleTime % dayCycleLength;
}
/// Sets the currentPhase to Dawn, turning on the directional light, if any.
public void SetDawn()
{
if (light != null)
{ light.enabled = true; }
currentPhase = DayPhase.Dawn;
}
/// Sets the currentPhase to Day, ensuring full day color ambient light, and full
/// directional light intensity, if any.
public void SetDay()
{
RenderSettings.ambientLight = fullLight;
if (light != null)
{ light.intensity = lightIntensity; }
currentPhase = DayPhase.Day;
}
/// Sets the currentPhase to Dusk.
public void SetDusk()
{
currentPhase = DayPhase.Dusk;
}
/// Sets the currentPhase to Night, ensuring full night color ambient light, and
/// turning off the directional light, if any.
public void SetNight()
{
RenderSettings.ambientLight = fullDark;
if (light != null)
{ light.enabled = false; }
currentPhase = DayPhase.Night;
}
/// If the currentPhase is dawn or dusk, this method adjusts the ambient light color and direcitonal
/// light intensity (if any) to a percentage of full dark or full light as appropriate. Regardless
/// of currentPhase, the method also rotates the transform of this component, thereby rotating the
/// directional light, if any.
private void UpdateDaylight()
{
if (currentPhase == DayPhase.Dawn)
{
float relativeTime = currentCycleTime - dawnTime;
RenderSettings.ambientLight = Color.Lerp(fullDark, fullLight, relativeTime / halfquarterDay);
if (light != null)
{ light.intensity = lightIntensity * (relativeTime / halfquarterDay); }
}
else if (currentPhase == DayPhase.Dusk)
{
float relativeTime = currentCycleTime - duskTime;
RenderSettings.ambientLight = Color.Lerp(fullLight, fullDark, relativeTime / halfquarterDay);
if (light != null)
{ light.intensity = lightIntensity * ((halfquarterDay - relativeTime) / halfquarterDay); }
}
//transform.Rotate(Vector3.up * ((Time.deltaTime / dayCycleLength) * 360.0f), Space.Self);
transform.RotateAround (rotation.position, Vector3.forward, ((Time.deltaTime / dayCycleLength) * 360.0f));
}
/// Interpolates the fog color between the specified phase colors during each phase's transition.
/// eg. From DawnDusk to Day, Day to DawnDusk, DawnDusk to Night, and Night to DawnDusk
private void UpdateFog()
{
if (currentPhase == DayPhase.Dawn)
{
float relativeTime = currentCycleTime - dawnTime;
RenderSettings.fogColor = Color.Lerp(dawnDuskFog, dayFog, relativeTime / halfquarterDay);
}
else if (currentPhase == DayPhase.Day)
{
float relativeTime = currentCycleTime - dayTime;
RenderSettings.fogColor = Color.Lerp(dayFog, dawnDuskFog, relativeTime / (quarterDay+halfquarterDay));
}
else if (currentPhase == DayPhase.Dusk)
{
float relativeTime = currentCycleTime - duskTime;
RenderSettings.fogColor = Color.Lerp(dawnDuskFog, nightFog, relativeTime / halfquarterDay);
}
else if (currentPhase == DayPhase.Night)
{
float relativeTime = currentCycleTime - nightTime;
RenderSettings.fogColor = Color.Lerp(nightFog, dawnDuskFog, relativeTime / (quarterDay+halfquarterDay));
}
}
/// Updates the World-time hour based on the current time of day.
private void UpdateWorldTime()
{
worldTimeHour = (int)((Mathf.Ceil((currentCycleTime / dayCycleLength) * hoursPerDay) + dawnTimeOffset) % hoursPerDay) + 1;
minutes = (int)(Mathf.Ceil((currentCycleTime* (60/timePerHour))% 60));
}
public enum DayPhase
{
Night = 0,
Dawn = 1,
Day = 2,
Dusk = 3
}
}
Did you write this code by yourself?
Anyway, why do you want your time to be two times twelve hours? If it's just for communicating the time to the user, all you need to do is to convert the 24 hour time to am and pm. That should be doable, no?
This was a freebee script from a website I found. I have lots of experience coding projects up to this point just non with time used in them. I understand what you are saying but still not sure how I would go about doing something like that. Using time in coding is a bit confusing to me for some reason.
I added this into the OnGUI but it just causes the timer to stay at 1.
if (worldTimeHour >= 13)
{
worldTimeHour = 1;
}
Awsome thanks so much for your help. If you change that over to an answer I will accept it as the solution to my problem. You were a big help thanks again.
Answer by christoph_r · Aug 14, 2014 at 03:13 PM
You should probably keep a separate int for the display hour and a bool 'pm'. If that display hour int is called displayTimeHour, try this:
if (worldTimeHour >= 13)
{
displayTimeHour = worldTimeHour - 12;
pm = true;
}
else {
displayTimeHour = worldTimeHour;
pm = false;
}
This might need some adjustments for noon/midnight. Now when you want to display your time in am/pm, use the pm
bool, displayTimeHour
and worldTimeMinute
.
if you want less code (who doesn't, right?) you can replace the above with these 2 lines.
pm = (worldTimeHour > 12);
displayTimeHour = pm ? (worldTimeHour - 12) : worldTimeHour;
you might also want to consider using some sort of helper function for the color settings to make the code a bit more readable.
public Color NewColor(float r, float g, float b, float a = 255.0f)
{
return new Color(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
}
then using it looks something like this:
fullDark = NewColor(32.0f, 28.0f, 46.0f);
which, for me at least (it's all about me), is much more readable.
you should also separate out the color setups from Reset()
so that they can be called from Initialize()
too (since you can't do it as part of the class constructor using the helper).
that way, you're also setting them from a single place - so any changes are done once, leading to less potential errors. google: "DRY"
i realize that you didn't originally write that script, but once you take it and start changing things, it's yours and you need to own it! ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
TextMesh isn't updating itself. 0 Answers
(C#) GUI Buttons not working 1 Answer
[Answered]Rigid body Movement C# 0 Answers