I tried to make some combo attacks in my game based on this answer but it don’t works for me, the character do only the first slash. Can someone help me make that code work? and with 3 slashs?
This is what I did so far (only 2 slashs now):
if (Input.GetKeyDown("u"))
{
if (!fired)
{
fired = true;
timer = 0.0;
comboCount = 0.0;
Debug.Log("I served a punch!");
//Do something awesome to deliver a punch!
animation.Play("Combo1");
}
else
{
comboCount++;
if (comboCount == comboNum)
{
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Combo3");
}
}
if (fired)
{
timer += Time.deltaTime;
if (timer > fireRate)
{
fired = false;
}
}
}
Your if(fired) block is only executed if you press ‘u’. It needs to be called each update, whether or not ‘u’ is pressed. Also, you’re checking to see if comboCount == comboNum, and if comboNum isn’t 2, you won’t see a combo. Make sure comboCount is an integer.
var fired : boolean = false;
var comboCount : int = 0;
var fireRate : float = 3;
var timer : float = 0;
function Update()
{
if (Input.GetKeyDown("u"))
{
if (!fired)
{
fired = true;
timer = 0.0;
comboCount = 1;
Debug.Log("I served a punch!");
//Do something awesome to deliver a punch!
animation.Play("Combo1");
}
else
{
comboCount++;
if (comboCount == 2)
{
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Combo2");
}
}
}
if (fired)
{
timer += Time.deltaTime;
if (timer > fireRate)
{
comboCount = 0;
fired = false;
}
}
}