- Home /
Problem with combo attacks
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;
}
}
}
It in the Update function.
Sorry for poor english.
Answer by almo · May 30, 2011 at 07:23 PM
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;
}
}
}
What's fireRate? If it's too small, you won't be able to hit the button fast enough.
Either I'm missing something in my answer, or you have a problem elsewhere. I don't see anything wrong in my code's logic. I'll check it again later today when I have time.
I have just copied the code above into a unity project, and I get the desired behavior.
I served a punch! I did a combo! I served a punch! I served a punch! I did a combo!
Answer by lzt120 · Jun 12, 2012 at 08:14 AM
public class Combat : MonoBehaviour {
float fireRate = 2;
int comboNum = 3;
private bool fired = false;
private float timer = 0.0f;
private int comboCount = 0;
void Update()
{
if (Input.GetKeyDown(KeyCode.J))
{
if (!fired)
{
fired = true;
timer = 0.0f;
comboCount = 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 answer
Follow this Question
Related Questions
Kill Streak Help 3 Answers
How to do combos? 1 Answer
Saving script variations 1 Answer