Hey all,
Please can anyone provide me with some direction or tips? I would appreciate it.
I am building a racing game in Unity which is effectively a WRC Hill Climb, so from point A to B.
I want a timer that ticks up so the user can see how long it took them.
Now the tough part is, that I want minutes, seconds and milliseconds.
I have read that DeltaTime does seconds only which is not enough for me.
Can anyone think of what else I can use and how I can make a nice digital GUI to display it?
Thanks
its pretty simple. Time.time is the time in seconds from the start of the game.
So you capture a StartTime at whatever Time.time is. Then you get something like this:
RaceTime = Time.time - StartTime
In order to get minutes:
Minutes = Mathf.Floor(RaceTime / 60)
In order to get seconds:
Seconds = Mathf.Floor(RaceTime - Minutes)
In order to get Milliseconds:
Milliseconds = RaceTime - Mathf.Floor(RaceTime)
In order to convert the Milliseconds to ###:
sMilliseconds = (Milliseconds.ToString() + “000”).Substring(0,3)
Same for Seconds and Minutes
Digital display:
sDigital = sMinutes + “:” + sSeconds + “:” + sMilliseconds
Oh ok I think I understand that. I will take this onboard and see what I can do! Thank you!
Hey, Sorry I did some testing with that but I dont think thats what I wanted. Might be my fault for not explaining it properly.
Basically from first entering the game, the counter will go up from 00:00:000 continuously so the user can see the total time they are spending.
I am trying to get my code so far to go up from the formated way 00:00:000, but I can only get it to work as 0:0:0 format. Its the FormatTime method that Im trying to change 
Any help please?
private var timerText:GUIText;
private var startTime:int = 0;
function Update () {
timerText = gameObject.GetComponent(GUIText);
timeTaken = startTime + Time.time;
timerText.text = FormatTime (timeTaken);
}
function FormatTime (time)
{
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
var fraction : int = time * 10;
fraction = fraction % 10;
timeText = minutes.ToString () + ":";
timeText = timeText + seconds.ToString();
timeText += ":" + fraction.ToString ();
return timeText;
}
Close. Your using an int to do all of your time calculations, by that, you are rounding the int to get it to a whole number, thus destroying the milliseconds part of your equation. Also the rounding does not help you. The minutes are also rounded so that is bad.
Your cycling the fraction which is not good either. Also you are limiting it to one digit (10 * 0.123 = 1) And your even rounding that.
So the use of Mathf.Floor is very crucial here.
Next, your building your strings as minutes.ToString(). this returns the number as an actual string. You need to use something like “00” + seconds. Then to get it down to two digits you could use something like Substring(string.Length - 2) Which would give you the last two letters.
private var timerText:GUIText;
private var startTime:float = 0;
function Update () {
timerText = gameObject.GetComponent(GUIText);
timeTaken = startTime + Time.time;
timerText.text = FormatTime (timeTaken);
}
function FormatTime (time : float){
var minutes : int = Mathf.Floor(time / 60.0);
var seconds : int = Mathf.Floor(time - minutes * 60.0);
var milliseconds = time - Mathf.Floor(time);
milliseconds = Mathf.Floor(milliseconds * 1000.0);
var sMinutes = "00" + minutes.ToString();
sMinutes = sMinutes.Substring(sMinutes.Length-2);
var sSeconds = "00" + seconds.ToString();
sSeconds = sSeconds.Substring(sSeconds.Length-2);
var sMilliseconds = "000" + milliseconds.ToString();
sMilliseconds = sMilliseconds.Substring(sMilliseconds.Length-3);
timeText = sMinutes + ":" + sSeconds + ":" + sMilliseconds;
return timeText;
}
1 Like
BigMisterB, thank you so much!!
The explanation going with the code was perfect to help me understand. Sorry, I am new to this!!
I will now double check I know everything that is happening in the method to make sure I get it.
Thank you for the help!