Can't seem to measure time between key presses

I’m working on a walking game where a game mat emulates the key board. I need the controller to move along as long as there is continual walking on the keys. If the user stands on the keys or leaves the game mat completely, the controller should stop. I’ve decided to use a timer to measure the amount of time between key presses, but, it seems to just keep increasing. Pace shouold vary between .5 - 1 second, not increase> What am I doing wrong?

function WalkTester(){
	//pacestart = 0 ;
    if (Input.GetKeyDown ("t")) {
        steps += 1 ;
        pacestart = Time.time ;
        Debug.Log ( pacestart);

    if (Input.GetKeyDown ("y")) {
        steps += 1 ;
        paceEnd = Time.time ;
        pace = (paceEnd - pacestart) ;
        Debug.Log ( pace );

If you’d like to have pace never increase beyond 1, you’d need to put a limit on it like:

pace = (paceEnd - paceStart);
if(pace > 1)
    pace = 1;

Otherwise, your code as you have written will result in huge pace numbers if you hit the “t” key, but say, wait for 10 seconds before hitting the y key. You may want to write a timeout script which says that if there is too much time between the two key presses, pace returns to zero.

In fact, you may do something like always lerp pace to a target value which is being reset to zero.

something like:

targetPace = 1/(paceEnd - paceStart); // 1 divided by subtraction will result in lower numbers for higher time value difference, therefore higher pace for less time between presses
if(targetPace > 1)
    targetPace = 1;

pace = Mathf.Lerp(pace, targetPace, .1f)

targetPace = 0;

this way, if no presses are detected, your “target pace” will move back to zero. otherwise, you can increase the pace slowly as you press more rapidly

I tested your code out, and it seems to work fine. Note your pasestart is going to go up, because it is set to the current running time of the application. Comment out the Debug.Log() on line 7, and test your code. For me, the numbers were consistent with my fingers walking between the two keys.

Note if you get repeated ‘y’ hits without a ‘t’ hit, the time will go up since ‘pacestart’ is not being set. Not sure how you want to handle it. You could reset pacestart to Time.time in the ‘y’ code, but then a user could repeatedly hit the ‘y’ key without ever hitting the ‘t’ key. Or you could add a boolean value so that the ‘y’ key is ignored if not preceeded by a ‘t’ key.