- Home /
Timer stopped on gameover in car racing game unity
Hello everyone,
I have made a simple car racing game using keyboard input..In my game when game is over my car is stopped but timer is still working..I want to stop the timer when car stop..but I don't want to use Time.timeScale function bcoz it stopped whole game..please help me ...i will really appreciate your time taken to preview this post..:)
thanks in advanced.. Enjoy..:) :)
Answer by fafase · Oct 08, 2012 at 10:37 AM
Enclosed your timer into an if statement:
if(running)//timer logic
if(carAtLastCheckpoint)running=false;
Considering your timer goes like this:
var running:boolean=true;
var timer:float;
function Update(){
if(running)timer += Time.deltaTime;
if(Input.GetKeyDown(KeyCode.Space))running = !running;
print(timer);
}
The program above will print the time as it goes and if you press space the timer stops. Press again the timer goes again.
In your game you would have a endOfRace boolean with this below attach to the last checkpoint with a trigger box:
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag == "Car")endOfRace = true;
}
The little example program uses Input for you to try, what you would need instead in your game is:
if(endOfRace){
running = false;
endOfRace=false;
}
Any questions, go ahead as a comment.
Hi fafase,
Thanx a lot for helping me..i am very new to this technology. here by i have enclosed my script plzz give it few $$anonymous$$utes. Here i am unable to understand where i have to do startGame=false so that my timer will stop..
function GameStart() { print("Timer Game Start Called"); // Startplay.text = "3"; yield WaitForSeconds (1.0); // Startplay.text = "2"; yield WaitForSeconds (1.0); // Startplay.text = "1"; yield WaitForSeconds (1.0); // Startplay.text = "Go"; yield WaitForSeconds (1.0); // Startplay.text = ""; startGame = true; }
function TimerOn(){ if(!startGame) { startTime = Time.time; } }
var TimerStop : boolean = false; function OnGUI () {
if(startGame==true) { //wait(); var guiTime = Time.time - startTime;
var $$anonymous$$utes : int = guiTime / 60; var seconds : int = guiTime % 60; var fraction : int = (guiTime * 100) % 100;
text = String.Format ("{0:00}:{1:00}:{2:000}", $$anonymous$$utes, seconds, fraction); GUI.Label (Rect (1220, 50, 100, 30), text);
}
Hey Fafase,
I tried your code but in that when i presed space thn only my timer stoped but i want to stop the timer at the end of the game..
thank u so much for help..:)
Pragya
The idea is that what I amdoing pressing the space bar you do the same with your car passing the final line. You would have a trigger box collider with the function OnTriggerEnter part given in the answer.
so:
-1 add a collider box at the finish line and tick IsTrigger.
-2 Add the code with OnTriggerEnter to the collider
-3 Look there http://answers.unity3d.com/questions/246211/Having-scripts-interact.html to see how to get your script interacting.
-4 Get the collider script to find the boolean in the timer script.
Hopefully you get my idea, might not be so clear...
Simple, truncate the value by assigning the float to an integer:
var fl:float = 2.2582;
var in:int;
in = fl;
print(in);
This will print 2
hey i want to convert float to string..And its showing like we can't convert float to String.
Answer by k_pragya · Oct 08, 2012 at 01:08 PM
Hi,
I used startTime= Time.time; but what should i do to stop my timer..and i did like this only but timer is not stopped on game over..thanx for revert me back..:) like below:-
if(startGame==true) { var guiTime = Time.time - startTime;
var minutes : int = guiTime / 60; var seconds : int = guiTime % 60; var fraction : int = (guiTime * 100) % 100;
text = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction); GUI.Label (Rect (1220, 50, 100, 30), text);
} when i do startgame==false then also my timer is working..
Pragya
Ok when you say "when i do startgame==false then also my timer is working.." do you mean you are using startgame==false to revert the value? because this is a logical operation. If you want startGame to become false you need to use only 1 = like this
startGame=false;
Your answer
![](https://koobas.hobune.stream/wayback/20220613082035im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotation along the x-axis to that player can turn around? 1 Answer
Racing game basic javascript question 2 Answers
Let the Car-Wheels rotating, problem 1 Answer
My car dont move 1 Answer
Flip over car after tips over? 1 Answer