- Home /
Show laptime after each lap ?
Hi guys I made a checkpoint script see below :) BUT I cannot seem to work out how to show the laptime after each lap completed ? can anyone help please ,its probably very simple but as you can probably tell from my script ,I am a Javanoob :p
heres the code
var time : float; var lapTime : float; var ckp = 0; var lap = 0;
function OnGUI(){ GUI.Box (Rect (10,10,110,100), "Racetime:" + time); GUI.Box (Rect (120,120,110,100), "Checkpoint : " + ckp/2); GUI.Box (Rect (240,10,110,100), "Laps" + lap);
if (lap==1) GUI.Box (Rect (540,10,400,100), "Laptime:" + (lapTime - time)); // This Shows as Zero ?
if (lap==2) GUI.Box (Rect (540,10,200,100), "You WON !");
}
function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "Checkpoint1")
ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint2") ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint3") ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint4") ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint5") ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint6") ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint7") ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint8") ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint9") ckp=ckp +1; if(hit.gameObject.tag == "Checkpoint10") ckp=ckp +1;
if (ckp==20) lap=1; if (ckp==40) lap=2; if (ckp==60) lap=3; if (ckp==80) lap=4; if (ckp==100) lap=5;
}
Im asking how to get the value of the time when the car hits the last checkpoint a,d show it with ongui ,say 1 $$anonymous$$ 37 seconds and display THAT moment in time .A$$anonymous$$ I making sense lol ?
Try declaring laptime as a string ins$$anonymous$$d of a float.
Answer by Nightlithium · Aug 27, 2012 at 01:27 AM
You'll want to have timeStart only once, in the Awake() function, instead of in the Update() function. After that, you can find the current lap time by subtracting timeStart from the current time. Like so;
function Awake() {
var timeStart = Time.time; //Gets the time when the script awakes
}
And then where you have the check to increment laps, currently written as;
if (ckp==20) lap=1;
you just change it to;
if (ckp == 20) {
lap = 1;
lapTime = Time.time - startTime;
//Takes the current time, and subtracts the start time from it,
//leaving you with the amount of time in between as your lap time.
}
And just have your OnGUI display lapTime in the appropriate box constantly, with a null value at the beginning of the race.
I'm a newbie too though, only got Unity a day or two ago, so take my advice with a grain of salt.
If you only got Unity two days ago ,then I cannot wait to see what you will be able to do after a couple of months :)