This has to be one of those completely easy/obvious things, but I’ve been struggling with this for hours and I just can’t figure out how to work with time.
At some point after the game starts (eg. after 2 seconds), the player is prompted to press a button. I want to determine the amount of time that passed between the time of the prompt and the time of the actual button press.
Really I guess my question boils down to: how can I reset the time to zero (or start a new timer at zero) so that I can track a new interval independently of how long the game has been running? This same problem would apply to tracking individual lap times in a racing game.
Here’s a simplified mockup of how I tried to set a timer (reactionClock) to zero after the game has already been running for 2 seconds. When I subtract m_timeSinceStartup from reactionClock, the reactionClock doesn’t go back to zero. How come?
var m_timeSinceStartup : float;
var reactionClock : float;
private var resetReactionClock : boolean = false;
function Start() {
ShowPrompt();
}
function Update() {
m_timeSinceStartup = Time.time;
if(resetReactionClock) {
resetReactionClock = false;
reactionClock = Time.time - m_timeSinceStartup; //reset this timer back to zero
//reactionClock -= m_timeSinceStartup; // this won't reset the time to zero either
}
else {
reactionClock = Time.time;
}
}
function ShowPrompt() {
yield WaitForSeconds(2);
resetReactionClock = true;
}
function OnGUI () {
if (GUI.Button (Rect (260,420,52,22), "PUSHME!")) {
DisplayReactionTime();
}
}
function DisplayReactionTime() {
reactionTime = reactionClock;
rtText.text = reactionTime.ToString("0.#.00");
}