- Home /
show times at the end of the game
Hello everyone!... Im working on this race game, What im having trouble with is basically showing the time on the top of the screen, and then when the game finnishes(when player gets to the goal) display a sign saying your time is:__(and show how long it took t to complete it)... Any ideas?? Thanks a lot!
Answer by Sebastian 2 · May 04, 2011 at 03:48 PM
Hey Colombian,
you have to add a new Cube and disable the mesh renderer. Then place it on your Finish line. Now make a new tag like "FinishLine".
Now you do this with a script on youre car:
var time : float; var finished : boolean = false;
function Update{ //Add the time every Frame if race isnt finished if(finished == false){ time += Time.deltaTime; } }
function OnTriggerEnter(hit : Collider){ //if hit Collider is the Finish Cube if(hit.tag == "FinishLine"){ //Race is finished finished = true; } }
function OnGUI(){ if(finished){ //if Race finished GUI.Label(Rect(Screen.width / 2 - 100, 20, 200, 25), "Your Time: " + time); } else{ //if race isnt finished (only draw time) GUI.Label(Rect(Screen.width / 2 - 100, 20, 200, 25), "" + time); } }
Sounds pretty cool!... but its says: expecting (, found { any ideas why this is?
Hi, thank you for commend Sebastian 2. But do you know if there script with c#? because my car movement script is c#, so I can't use javascript which is you posted. Please I need help. Thank you
Answer by Byterunner · May 04, 2011 at 03:40 PM
Store the time at the beginning of the race, then in your update function print out the current time minus the start time to display how long they've been racing on the top of the screen. When they reach the goal, save the current time minus the start time, and that's the time the race took.
Answer by Jesse Anders · May 04, 2011 at 03:41 PM
Although there may be more suitable solutions (depending on the circumstances), Unity's built-in GUI system is probably a good place to start. Using the GUI system, you can easily display text and graphics onscreen, which is what it sounds like you need here.
Displaying the total time at the end of the race is more of a program logic problem, and there are many ways it can be done (a simple state machine, loading a completely separate scene, etc.).
Those are fairly general answers, but the question itself is fairly broad. If you need further help with this, I'd recommend posting some more specific questions and specifying in more detail what exactly it is that you need help with.
Your answer
Follow this Question
Related Questions
Time Based Shooter 0 Answers
I need help for delete old highscore 0 Answers
How to call a function only once in Update 1 Answer