Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Time scaled from 0 to 1 in HH:MM

I have a simple question (I am bad at math…).
So i made a day/night cycle in my game and to change the time of the day i have slider ranging from 0 to 1. Everything is working correctly, but i can’t convert this time in minutes.

Here is a bit of the code I am using:

Where time is my slider (float) and timeRate (float) a time multiplier.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    private void Update(){
        time += timeRate * Time.deltaTime;
        ConvertTime();
    }


    private void ConvertTime()
    {
        hours = 24 * time;
        minutes = ?;

        if(minutes >= 60)
            minutes = 0;

        displayRealTime = LeadingZero(hours.ToString("0")) + ":" + "00";
    }

The "LeadingZero" function just add a 0 if the number < 10.

The hours are good but i don’t know how to get the minutes.
Thanks for reading.

>Solution :

well your hours already contain the minutes after the decimal.

E.g.

12.23 hours

=> minutes is

0.23 * 60

So all you need is Get only decimals of a floating point number and multiply it by 60 and then round to int again

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading