I’m making a racing game for a school project and I need to have a timer that counts upward from start until the player crosses the finish line. I also need the timer to go back a certain increment when the player hits an object (i.e. +Time) and then go forward when the player collides with an object like a barricade. Please help!
I figured out the timer code:
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;
}
Now I just have to figure out how to add or subtract time depending on which object the player collides with. Thanks
So, this would be a new script attached to my game objects that the racer could collide with?
– TheDefibulatorThat could be using the GUI you have on the example. Only the logic is somehow modified. You would have to use some trigger boxes that represent checkpoint and a final trigger for the end race. And this is attached to the racing object not the boxes
– fafaseWhere is it happening?
– fafaseIt happens on startup.
– TheDefibulatorNo, the error, where is it? The line?
– fafase