- Home /
How to transfer time script to 24 hours and not decimals
Hello so I've found a script online for Unity that is a Day and Night cycle however it works in decimals, 0.1 to 1, however I want it to be 1 to 24.
The code I am using is below, I have tried fiddling around with the decimal values however I can't get it correct.
using UnityEngine;
using System.Collections;
public class DayNightCycle : MonoBehaviour {
public Light sun;
public float secondsInFullDay = 120f;
[Range(0,24)]
public float currentTimeOfDay = 0;
[HideInInspector]
public float timeMultiplier = 1f;
float sunInitialIntensity;
void Start() {
sunInitialIntensity = sun.intensity;
}
void Update() {
UpdateSun();
currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
if (currentTimeOfDay >= 24) {
currentTimeOfDay = 0;
}
}
void UpdateSun() {
sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
float intensityMultiplier = 1;
if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f) {
intensityMultiplier = 0;
}
else if (currentTimeOfDay <= 0.25f) {
intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
}
else if (currentTimeOfDay >= 0.73f) {
intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
}
sun.intensity = sunInitialIntensity * intensityMultiplier;
}
}
The result I want is for the day and night cycle to work from 1 to 24 and not 0 to 1, so that there's 24 hours in the game, and that it's easier to modify when using a sleep script I've made.
Answer by bpaynom · Jul 18, 2019 at 01:17 PM
Add this property
public float currentTime24HoursBased {
get {
return Mathf.Lerp( 0 , 24 , currentTimeOfDay );
}
}
And in the Update()
callback change currentTimeOfDay >= 24 for >= 1, like this:
void Update() {
UpdateSun();
currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
if (currentTimeOfDay >= 1f) {
currentTimeOfDay = 0;
}
}
Your answer
Follow this Question
Related Questions
Big Numbers - How to convert (k, M, a ) 4 Answers
Increase text size over time c# 2 Answers
Checking for start of second in waitforseconds 0 Answers
How to work a real life timer? 2 Answers
Cronómetro 1 Answer