Calculating reaction time

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");
}

Just on a quick look, using Time.time as your system clock, you would need to set the new start time after each button press. Something like this seems to work:

function OnGUI () {
if (GUI.Button (Rect (60,420,152,22), “PUSHME!”)) {
DisplayReactionTime();
m_timeSinceStartup = Time.time;
print("reaction time: " + reactionTime );
}
}

function DisplayReactionTime() {
reactionTime = Time.time - m_timeSinceStartup;
print("reactionTime : " + reactionTime);
}

Regards,

Peter

Thanks for the reply. That’s exactly the clue I was looking for. Here’s a final version of the sample script which includes a random delay before displaying the button to increase the challenge of the reaction test.

var reactionTime : float;
var m_timeSinceStartup : float;
var buttonActive : boolean = false;
var m_minWaitTime : float = 1.0;
var m_maxWaitTime : float = 2.5;

function Start() {
	ButtonRest();
}

function OnGUI () {
	if(buttonActive) {
		if (GUI.Button (Rect (60,420,152,22), "PUSHME!")) {
			DisplayReactionTime();
		}
	}
}

function DisplayReactionTime() {
	reactionTime = Time.time - m_timeSinceStartup;
	print("reactionTime : " + reactionTime);
} 

function ButtonRest() {
	m_randomStartTime = Random.Range(m_minWaitTime, m_maxWaitTime);
	yield WaitForSeconds(m_randomStartTime);
	m_timeSinceStartup = Time.time;
	buttonActive = true;
}

Hey guys, i am very new to Unity and i am trying to do the exact same thing. I already implemented a button. My question is, how do i bind the script you posted to my button? I get compile errors and dont know why. I can only find tutorials for SceneChanges and stuff. Would appreciate an answer even if the topic is 8 years old :stuck_out_tongue: Thanks!