How can I use time to reset a switch statement?

Hello, Im trying to setup a combo system for my game where the player will be able to chain attacks if the attack button is pressed in rapid succession.

I’ve been experimenting with switch statements and it seems like it’ll be the way to go, here’s a simple test code I made:

var comboCount: int = 0;

function Update () 
{
	if (Input.GetKeyDown(KeyCode.Space))
		comboFunction();
}

function comboFunction ()
{
	switch (comboCount)
	{
		case 0:
			print ("Idle");
			comboCount += 1;
			break;
		case 1:
			print ("Attack1");
			comboCount += 1;
			break;
		case 2:
			print ("Attack2");
			comboCount += 1;
			break;
		case 3:
			print ("Attack3");
			comboCount = 0;
			break;
	}
}

So, what Im doing here is printing a different message each time the space key is pressed.
The first time it’ll return “Idle”, then “Attack1”, “Attack2” and finally “Attack3” then go back to idle.

So what I want to do now is implement a timer so that if there are no key presses during X amount of time (let’s call it “var reFireTime”) then the “comboCount” variable will be reset to zero.

Could anyone explain how to do this? I’ve been reading up on Time.time but Im struggling in figuring out how to implement this to my example.

Thank you for your time.

This method I show you here is a basic and simple way to do it. It’s btw not tested at any level. You can call a timer function right after your combo function, but then your program will probably register several clicks at “the same time”. In other words, you will do all of the states Idle, Attack1, Attack2 and Attack3 in matter of milliseconds. You are statistically not quick enough to release your finger off the button. You have to lock the button for an amount of time, lets say for about 0.5s. This is the minimum time you have to wait before the player can do another attack. So lets implement this somehow, by first changing the update function to:

var isSpaceLocked : boolean = false;

function Update ()  {
    if (Input.GetKeyDown(KeyCode.Space) && !isSpaceLocked) {
       comboFunction();
       StartCoroutine(LockSpaceForTime(0.5f));
       StartCoroutine(TimeCombo());
    } }

Lets implement our LockSpaceForTime(), which takes care of the minimum time you have to wait before a click is registered as a new click:

function LockSpaceForTime(time : float) {
    isSpaceLocked = true;
    yield WaitForSeconds(time);
    isSpaceLocked = false;
}

Then implement the maximum time before the combo gets outdated, TimeCombo():

var timeBeforeComboEnds : float = 1.5f;
var comboCoroutinesRunning : int = 0;

function TimeCombo() {
    comboCoroutinesRunning += 1;
    yield WaitForSeconds(timeBeforeComboEnds);
    comboCoroutinesRunning -= 1;
    if (comboCoroutinesRunning == 0)
        comboCount = 0;
}

So the total script would be:

var comboCount: int = 0;
var comboCoroutinesRunning : int = 0;
var isSpaceLocked : boolean = false;
var timeBeforeComboEnds : float = 1.5f;

function Update () 
{
    if (Input.GetKeyDown(KeyCode.Space) && !isSpaceLocked) {
       comboFunction();
       StartCoroutine(LockSpaceForTime(0.5f));
       StartCoroutine(TimeCombo());
    }
}

function TimeCombo() {
    comboCoroutinesRunning += 1;
    yield WaitForSeconds(timeBeforeComboEnds);
    comboCoroutinesRunning -= 1;
    if (comboCoroutinesRunning == 0)
        comboCount = 0;
}

function LockSpaceForTime(time : float) {
    isSpaceLocked = true;
    yield WaitForSeconds(time);
    isSpaceLocked = false;
}

function comboFunction ()
{
    switch (comboCount)
    {
       case 0:
         print ("Idle");
         comboCount += 1;
         break;
       case 1:
         print ("Attack1");
         comboCount += 1;
         break;
       case 2:
         print ("Attack2");
         comboCount += 1;
         break;
       case 3:
         print ("Attack3");
         comboCount = 0;
         break;
    }
}

You can change the timing values to whatever that fits your needs, I just chose some almost arbitrary numbers.