x


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 :(

more ▼

asked Apr 28 '10 at 07:10 PM

PrimeDerektive gravatar image

PrimeDerektive
3.1k 57 64 84

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
        }
more ▼

answered Apr 28 '10 at 08:57 PM

Jason_DB gravatar image

Jason_DB
1.9k 4 14 36

What is the significance of the Math.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? :(

Apr 29 '10 at 12:15 AM PrimeDerektive
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5276
x3935
x110

asked: Apr 28 '10 at 07:10 PM

Seen: 1604 times

Last Updated: Apr 28 '10 at 07:10 PM