- Home /
Resetting a combo attack chain if next attack did not occur in time
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 :(
Answer by Jason_DB · Apr 28, 2010 at 08:57 PM
What I would do is change the if from Time.time > nextSwing to being within a certain difference from the target time.
//comboTime is how close you have to be to the correct time in order to combo
if(Mathf.Abs(Time.time - nextSwing) < comboTime)
So what you could do is put what you have now in that if, but add an }else{ which resets the combo.
if (Mathf.Abs(Time.time - nextSwing) < comboTime){
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;
}
} else {
swingCounter = 0;
}
What is the significance of the $$anonymous$$ath.Abs part? Also, I can't tell but it seems that in your example, if the swing was not within the time of the last combo swing, there will be no swing at all (rather than resetting and starting over?) I can't really tell, thrown off by the time calculation. Why is time so confusing? :(
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animation script with is.grounded help 0 Answers
change animation name in inspector 2 Answers
Animation component in Unity 4.3 doesn't work? 2 Answers
Character controller problem? 2 Answers