So I'm trying to make my melee attack script so that there are three different attack animations, and if you chain 3 attacks (left mouse clicks) within a certain amount of seconds between each, it will play the different animations for the different attacks.
I have it working so that an attackCounter is initialized, and every time the fire button is pressed it is incremented, will play a different animation depending on its value, and reset at 3. Also, every third swing I add another delay(comboDelay) to the delay between swings, so you can only do 3 attacks in a chain before a brief pause. However, I cannot figure out how to make it only increment and play a different animation if the next button press was within the correct timeframe (we'll call it timeAllowedBetweenCombo).
Here is the script in its current incarnation:
var swingRate = 0.25;
var comboDelay = 0.5;
private var nextSwing = 0.0;
private var swingCounter = 0;
function Update () {
if(Input.GetButtonDown("Fire1")){
if (Time.time > nextSwing){
switch(swingCounter){
case 0:
//This is the first swing in the chain
nextSwing = Time.time + swingRate;
break;
case 1:
//This is the second swing in the chain
nextSwing = Time.time + swingRate;
break;
case 2:
//This is the last swing in the chain
nextSwing = Time.time + swingRate + comboDelay;
break;
}
swingCounter++;
}
}
//reset combo counter every third swing
if(swingCounter == 3){
swingCounter = 0;
}
}
I've tried adding a variable called lastSwing and setting it equal to Time.time at the end of If(GetButtonDown("Fire1")){, and then checking if Time.time > lastSwing+timeAllowedBetweenCombo inside each of the switch cases, but to no avail :(