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!");
}
}
}