Combo script for action games [solved]

Edit: GetButtonDown() is ok, script was not ok.See the answer for correct one.
I checked and cross-checked. I am trying to use the “keyCombo” script from wiki. I modified the script to suit my need. But the class is not working.

I examined and cross-examined.

On line 31-32 where there is “if(Input.GetButtonDown(keys[index]))”, problems probably lies on there.

control flow do not enter into, here. I do not know why!? By this line, it should check if the button corresponding to “keys[index]” is pressed on this frame or not. If the button pressed, then Boolean true will be out and code block below will start executing. The real situation is, it is not happening!! But if I add a “!” on if statement line 31-32 , then it executes even if I do not press any button. This is really freaky and unusual!!! The class code is below:

using UnityEngine;
using System.Collections;
namespace kaiyum
{
	namespace control
	{
		public class comboSystem
		{
			public string[] keys;
			private int index=0;
			public float inBetweenTime=2.0f;
			private float lastKeyPressTime=0.0f;
			public string msg= "ini";
			public comboSystem (string[] k)
			{
				keys=k;
			}

			public bool check()
			{
				if(Time.time>lastKeyPressTime+inBetweenTime)
				{

					index=0;
					return false;
				}
				else
				{
					if(index<keys.Length)
					{
						if(!Input.GetButtonDown(keys[index]))
						{
							lastKeyPressTime=Time.time;
							index++;
							if(index>=keys.Length)
							{
								index=0;
								return true;
							}
							else
							{
								return false;
							}

						}
						else
						{
							return false;
						}
					}
					else
					{
						return false;
					}
				}
			}
		}
	}
}

And implementation is like:

    using UnityEngine;
    using System.Collections;
    using kaiyum.control;
    
    public class doCombo : MonoBehaviour {
    
    	private comboSystem combo1= new comboSystem(new string[] {"right","right","left"});
    	void Update () 
    	{
    		if(combo1.check())
    		{
    			print ("Do combo1!");
    		}
    	
    	}
    }

You’re checking if the button is not down.

!Input.GetButtonDown(keys[index])

Shouldn’t you be checking if it is down.

Input.GetButtonDown(keys[index])

Also, you are using Input.GetButtonDown(), which requires you to setup button aliases in the Input Manager. I think you want to use Input.GetKeyDown().

You would use Input.GetButtonDown() if you want to do something like this.

Input.GetButtonDown("Light Punch");

And in the input manager, you would assign a key to “Light Punch”.

at last I did it. The main culprit was here,

if(Time.time>lastKeyPressTime+inBetweenTime)
				{
					index=0;
					lastKeyPressTime=Time.time;
					return false;
				}

The overall correct class is:

using UnityEngine;
using System.Collections;
namespace kaiyum
{
	namespace control
	{
		public class comboSystem
		{
			public KeyCode[]keys;
			public int index;
			public float inBetweenTime;
			public float lastKeyPressTime;
			public comboSystem (KeyCode[] k)
			{
				index=0;
				inBetweenTime=1.5f;
			    lastKeyPressTime=0.0f;
				keys=k;
			}

			public bool check()
			{
				if(Time.time>lastKeyPressTime+inBetweenTime)
				{
					index=0;
					lastKeyPressTime=Time.time;
					return false;
				}
				else
				{
					if(index<keys.Length)
					{
						if(Input.GetKeyDown(keys[index]))
						{
							lastKeyPressTime=Time.time;
							index++;
							if(index>=keys.Length)
							{
								index=0;
								return true;
							}
							else
							{
								return false;
							}

						}
						else
						{
							return false;
						}
					}
					else
					{
						return false;
					}
				}
			}
		}
	}
}

And implementation is:

using UnityEngine;
using System.Collections;
using kaiyum.control;

public class doCombo : MonoBehaviour {

	private comboSystem plumbOfPrometheus= new comboSystem(new KeyCode[] {KeyCode.Keypad0,KeyCode.Keypad0,KeyCode.Keypad1});
	private comboSystem rageOfSparta= new comboSystem(new KeyCode[] {KeyCode.Keypad0,KeyCode.Keypad0,KeyCode.Keypad0});
	private comboSystem inferno=new comboSystem(new KeyCode[]{KeyCode.KeypadEnter,KeyCode.KeypadPlus,KeyCode.Keypad0});
	void Update () 
	{
		if(plumbOfPrometheus.check())
		{
			print ("Do Plumb of Prometheus!");
		}
		if(rageOfSparta.check())
		{
			print ("Do Rage of Sparta!");
		}
		if(inferno.check())
		{
			print ("Infernal pain has gone!!");
		}
	
	}
}

Al though I used getkeydown(), I am pretty confident it will work with getbuttondown() too! Now lets see if I can finish this combo system for my beat’em up next.