- Home /
Javascript Basic Lap Timer with GUI
I have been searching the forums for literally days, and can't find a working answer. I have tried dozens of possible solutions but nothing is working for me. Basically, I have a basic racing game. I have a nice little welcome screen, with a GUI that loads the map with the track. I got a countdown timer to say 3, 2, 1, GO! But now I'm trying to figure out how to make a lap timer. I only need the car to do one lap, and then it uploads the timer to a server I have. I have tried using triggers to start and stop the time, but nothing seems to work. I would post code, but there are just so many versions I have tried. If anyone knows of any good scripts in the wiki, or tutorials on the internet, literally anything, it would be greatly appreciated.
Answer by POLYGAMe · Feb 06, 2012 at 11:33 PM
You're welcome to use mine. It's a little more complex and probably a little rough as I'm a bit of a noob but it does the job. It takes into account multiple laps and displays the last lap etc etc and can also detect if the lap was legitimate (though that needs tweaking). Take from it what you want ;-)
// Public Variables
var ValidLap : GameObject;
var LapCounter : GameObject;
var LapCounterText : GUIText;
var LapCounterTextShadow : GUIText;
var iStartSequenceTime : int = 9;
var LapTimeText : GUIText;
var LapTimeTextShadow : GUIText;
var LastLapText : GUIText; // Displays the previous lap time in the centre of the screen for the player
var LastLapTextShadow : GUIText;
var iNumberOfLaps : int = 5;
var fAllLapTimes : float[] = new float[iNumberOfLaps]; // An array to store all laptimes.
// Private Variables
private var bValidLapIsTrue = false;
private var iCurrentLap : int = 1;
private var iMinutes : int;
private var iSeconds : int;
private var iHundredths : int;
private var iMinutes2 : int;
private var iSeconds2 : int;
private var iHundredths2 : int;
private var fCurrentLapTime : float;
private var fPreviousLap : float; // Float to store the previous completed lap
private var fLapTimeTotal : float; // Container to store all previous laptimes together.
private var bSetLapTime = false;
private var bCurrentLapReset = false;
function OnTriggerEnter(other : Collider)
{
if(other == ValidLap.collider)
{
if(bValidLapIsTrue)
{
bValidLapIsTrue = false;
}
else
{
bValidLapIsTrue = true;
}
}
if(other == LapCounter.collider && bValidLapIsTrue)
{
++iCurrentLap;
bValidLapIsTrue = false;
bSetLapTime = true;
}
}
function Update()
{
// Update the onscreen lap counter
LapCounterText.text = ("LAP " + (iCurrentLap));
LapCounterTextShadow.text = ("LAP " + (iCurrentLap));
if (LevelSetup.g_bGameInPlay)
{
if (!bCurrentLapReset)
{
fCurrentLapTime = Time.time - iStartSequenceTime; // Account for the starting sequence delay
}
else
{
// Reset the current lap timer, accounting for original start seq delay
fCurrentLapTime = ((Time.time - fLapTimeTotal) - iStartSequenceTime);
}
}
else
{
// Keeps timer at zero until game underway
fCurrentLapTime = 0.0f;
}
// Lap timer
iMinutes = (fCurrentLapTime /60f);
iSeconds = (fCurrentLapTime % 60f);
iHundredths = ((fCurrentLapTime * 10) %10);
LapTimeText.text = String.Format("{0:00}:{1:00}:{2:00}",iMinutes,iSeconds,iHundredths);
LapTimeTextShadow.text = String.Format("{0:00}:{1:00}:{2:00}",iMinutes,iSeconds,iHundredths);
// Set the previous lap time
while (bSetLapTime)
{
fPreviousLap = fCurrentLapTime;
print(fPreviousLap);
bSetLapTime = false;
fCurrentLapTime = 0.0f; // Reset the current Laptime
bCurrentLapReset = true;
fLapTimeTotal += fPreviousLap; // Adds to the total race time
DisplayLapTime();
if (iCurrentLap <= fAllLapTimes.Length) // So array doesn't go out of bounds
{
for (var i = 0; i < iCurrentLap; ++i)
{
fAllLapTimes[i] = fPreviousLap; // Populate the array with lap times
print("Lap: " + fAllLapTimes[i]);
}
}
}
}
function DisplayLapTime()
{
var LastLapTextCopy = Instantiate(LastLapText);
var LastLapTextShadowCopy = Instantiate(LastLapTextShadow);
iMinutes2 = (fPreviousLap /60f);
iSeconds2 = (fPreviousLap % 60f);
iHundredths2 = ((fPreviousLap * 10) %10);
for (var i = 0; i < 5; ++i)
{
LastLapTextCopy.text = ("LAST LAP: \n" + String.Format("{0:00}:{1:00}:{2:00}",iMinutes2,iSeconds2,iHundredths2));
LastLapTextShadowCopy.text = ("LAST LAP: \n" + String.Format("{0:00}:{1:00}:{2:00}",iMinutes2,iSeconds2,iHundredths2));
yield WaitForSeconds(0.5);
LastLapTextCopy.text = (" ");
LastLapTextShadowCopy.text = (" ");
yield WaitForSeconds(0.5);
}
yield WaitForSeconds(3.0f);
Destroy(LastLapTextCopy);
Destroy(LastLapTextShadowCopy);
}
Thanks! I'm having a bit of trouble implementing this.. can you explain how you have it setup in your game? Its giving me a compiler error "(61,9): BCE0005: $$anonymous$$ identifier: 'LevelSetup'. " The code says: if (LevelSetup.g_bGameInPlay)
I appreciate your help!
Ah, sorry, that part of the script is referencing another script (LevelSetup is a script and bGameInPlay is a bool in that script). You should be able to read through this though and take the bits you need... it's reasonably well commented ;-)
Ok Thanks. I suppose that part should just be removed altogether.
also, when I try to assign the GUI Texts using the editor, it returns errors. Ideas?
What are the errors? Have you created the GUIText gameobjects and linked to them?
Im not near my computer now, but it gave me some error along the lines of "LapCountText is not a defined variable" or something like thay. I will let you know the exact message when I get home... Thanks again
Answer by Kryptos · Feb 06, 2012 at 11:02 PM
At the beginning of the race (just after the countdown):
this.startTime = Time.time; // startTime is a float member of your script
At the end:
float fTime = Time.time - startTime; // absolute value in seconds
int nHundredths = (int)(Mathf.Floor(fTime*100))%100;
int nSecond = (int)Mathf.Floor(fTime);
int nMin = (int) nSecond/60;
nSecond = nSecond%60;
string sText = string.Format("{0:00}:{1:00}.{2:00}", nMin, nSecond, nHundredths);
Your answer
Follow this Question
Related Questions
Timer Between Labels 2 Answers
Need some help an object collider that stops a timer then displays it on another scene 1 Answer
Why my button in for loop isn't working? 1 Answer
Race Start Triggers 1 Answer
Check which waypoint is closer 1 Answer