Hello,
I have a mechanic in my project where the player can set a digital alarm clock to a time, go to sleep, and wake up at that time, allowing time to pass and for stamina to restore and such. A day/night cycle occurs between a value of 0 and 1, where 0 is 12AM, 0.5 is 12PM, 1 is a new day at 12AM, and everything in between.
Right now as day and night cycles occur, the digital clock takes that value and converts that into an AM/PM time. I’m trying to figure out how to make it go the opposite way, to take a user-inputed time and make that a value between 0 and 1 so that I can set the current time to that.
This is the code for converting from the value to the time, where controller.currentTime is the value between 0 and 1.
public class ClockDigital : MonoBehaviour
{
public Text clockText;
public Text ampmText;
public double minute, hour;
DayAndNightControl controller;
private void Start()
{
controller = GameObject.Find("Day and Night Controller").GetComponent<DayAndNightControl>();
}
void Update()
{
CalculateTime();
}
void CalculateTime()
{
float currHour = 24 * controller.currentTime;
float currMin = 60 * (currHour - Mathf.Floor(currHour));
minute = Mathf.Floor(Mathf.Repeat(currMin, 60));
hour = Mathf.Repeat(Mathf.Floor(currHour), 12);
Then I use a couple if statements to create 0’s for minutes and hours less than nine for the clock text, which has a generic form of clockText.text = hour + “:” + minute. But I figure I’m looking at these last 4 lines to reverse the equation…