Finish One Combo

Hello Unity3D.I have a problem with my combo script.The problem is that i can’t use more than one button in order to do another animation.For example i want to a 4 hit combo using “p”,“p”,“p”,“k” buttons.But the combo script only allows me to use “p”.Whenever i trying using “k” button my 5 hit combo starts off with the first animation to the “k” button combo.I don’t want that.I want to be able to do the “p”,“p”,“p”,“k” combo without it triggering my “k”,k",k",k",k" button combo.If anyone knows how i can do this.Can you please tell me how?

Here’s the script that i am having trouble with

var fired : boolean = false;
 var comboCount : int = 0;
 var fireRate : float = 3;
 var timer : float = 0;

 
     function Update()
     {
         if (Input.GetKeyDown("p"))
         {
             if (!fired)
             {
                 fired = true;
                 timer = 0.0;
                 comboCount = 1;
                 Debug.Log("I served a punch!");
                 //Do something awesome to deliver a punch!
                 animation.Play("Overhead_slash");
                  audio.Play(); //Finally play the audio
    				audio.volume = 50;
    		
             }
             else
             {
                 comboCount++;
                 if (comboCount == 2)
              
                 {
                     Debug.Log("I did a combo!");                
                     //Do something awesome for the combo!
                     animation.Play("Overhead_slash2");
                     
             }
              else
             {
                 comboCount++;
              
                 if (comboCount == 4)
                 if (Input.GetKeyDown("p"))
                 {
                     Debug.Log("I did a combo!");                
                     //Do something awesome for the combo!
                     animation.Play("Overhead_slash");
                   
             }         
              else
             {
                 comboCount++;
              
            	if (comboCount == 6)
            	 if (Input.GetKeyDown("p")) 
                 
                     Debug.Log("I did a combo!");                
                     //Do something awesome for the combo!
                     animation.Play("Spinning_Slashes");
                     animation["Spinning_Slashes"].speed= 2;
             }     
             
                            
                                           
                                                          
                                                                         
                                                                                        
              else
             {
                 comboCount++;
              
            	if (comboCount == 8)
            	 if (Input.GetKeyDown("k")) 
                 
                     Debug.Log("I did a combo!");                
                     //Do something awesome for the combo!
                     animation.Play("Overhead_slash4");
                     animation["Overhead_slash4"].speed= 2;
             }                   
                                     
                                                 
                 }
             }
         }
         
         
         if (fired)
         {
             timer += Time.deltaTime;
             if (timer > fireRate)
             {
                 comboCount = 0;
                 fired = false;
             }        
         }
     }

Hi, stop killing kenny, Cartmans getting mad :stuck_out_tongue:

instead of having a legion of else blocks and conditions based on the number of buttons pressed, i suggest you try making a button recording system using strings.

string recordInput = "";

basically you have a recording string, this string basically keeps track of your inputs… and if there is no input for a giving amount for time the string will clear itself. this is very important so you character will not continue acting out combos and you can proceed with new combos.

 string recordInput = "";
float waitClearSeconds = 1f //wait 1 second adter the last input before clear, you will have to adjust this setting later

void Update() {

RecordInput();

waitClearSeconds -= Time.deltatime; 

if(waitClearSeconds < 0f) {

Clear();
}
}

void RecordInput() {

Input.GetKeyDown("p") {

recordInput += "p"
waitClearSeconds = 1f //reset the wait time to avoid clearing too early
}
Input.GetKeyDown("k") {

recordInput += "k"
waitClearSeconds = 1f //reset the wait time to avoid clearing too early
}

}

void Clear() {
recordInput = "";

}

to keep track, every time you press “p” or “k” you recording string will add “p” or “k” to it. again after a certain time the string must clear itself. in order to maintain character control. lets say you do a 3 punch combo, you will press “p” 3 times. your recording string will add “p” to it 3 times becoming “ppp”.

    string recordInput = "";
    float waitClearSeconds = 1f //wait 1 second adter the last input before clear, you will have to adjust this setting later
    
    void Update() {
    
    RecordInput();
    
    waitClearSeconds -= Time.deltatime; 
    
    if(waitClearSeconds < 0f) {
    
    GetCombo();
    Clear();
    }
    }

    void GetCombo() {

if(recordInput == "ppp") {

//do my combo code

} 
 }

after that your script will wait until just before clear, this way if you have a combo that has a “pppkkp”, it won’t be interred with the “ppp” combo. just before clearing the recording string search for a combo that matches “ppp”., from there you can finally trigger that actions that involve your combo. now finally after all that you can clear the string ready for the next combo :slight_smile:

simplified:

  1. Get recording string, to record inputs
  2. run a timer that resets after each input
  3. when timer reaches zero, use recording string to invoke your desired animation
  4. Clear the string ready for the characters next combo sequence

WARNING! : CODE REMAINS UNTESTED!

Hope it helps