- Home /
how to create race lap timer, start to finish, display gui?
ive been trying to create a timer for something like the race car tutorial..still confused as ever, here's hoping again i can get clearer help and guidance. again, im new to anything-scripting, im a game artist, and i've been learning to script in unity with c#, not javascript. the example answers others here have been contributing has helped me learn alot in a somewhat short time, so thanks and i hope i can get more help and continue learning with this question.
i already have a script, to start the object moving on pressing a key.
my scene is setup with the player car set just before a Trigger "Startpoint".
i have a OnGui() function applied to the Startpoint trigger.
using UnityEngine; using System.Collections;
public class StartingLine : MonoBehaviour {
private float startTime;
private float ellapsedTime;
void Awake(){
startTime = Time.time;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(){
TimerStart();
}
void TimerStart(){
ellapsedTime = Time.time - startTime;
}
void OnGUI(){
GUI.Label(new Rect(300, 100, 100, 20), (ellapsedTime.ToString()));
}
}
this makes it display a time in the game. it shows "0.00", and when my player object activates/crosses the trigger, the time immediately jumps to a different number, displays "2.31", so unity is counting the time between the moment i "click Play scene" and the moment my object hits the trigger.
thats wrong..so how should i fix my script? i want unity to not count time until the player "starts" a lap, my trigger is this Startpoint collider. (display the time as 0:00, until the player hits the Startpoint trigger, then start the timer, and display in realtime the time counting up, 0:00..0:10.. 0:30.. 1:00.. 1:30.. etc). with my current script, the numbers dont "tick" on screen, they just update and "jump" to the new value.
and which part of my script should i split, to apply to my Endpoint trigger? i need unity to stop the timer (call the time variable from the Start script?) when the player object hits this 2nd trigger.
hopefully this long post makes it clear enough what ive been trying to do..i thought a timer like this would be a really small task to do, apparently i have a lot more to learn and research..hope you guys can guide me along further and faster, thanks many!
EDIT: im using OnGui() because i will later be using a custom font (i believe a guiskin?)to display the timer. i would appreciate it sooo much if whoever answers and helps, can add comments "//" to like, everything in the script, i really want to learn and understand what you're writing, the other answers here i've spent hours reading and trying to apply to my script is not very useful, i dont know what an array is, or how to use the Math.<> thingy either..looking up the Unity Scripting documentation is only confusing me further too.
Duplicate of http://answers.unity3d.com/questions/39221/how-to-start-a-timer-display-as-gui/39223#39223 Please post latest code.
Answer by kennypu · Feb 07, 2011 at 09:31 PM
easy way would be when the race starts, just have a variable that stores that, for eg. startTime = Time.time. now for the ellapsed time, you just check for Time.time - startTime;
in other words you're doing it right, however your TimerStart() would be used wrong because Time.time is always going. So whenever you set ellapsedTime it will just give you the difference of Time.tim - startTime. what you really want to do at TimerStart() is what you do in awake, and you will set ellapsedTime in update() so that you see your currentTime at all times.
sorry if that was confusing. to simplify things, change your code to:
void TimerStart()
{
startTime = Time.time;
}
void Update()
{
ellapsedTime = Time.time - startTime;
}
now ellapsedTime will show the correct time.