Ali Iftikhar
Ali Iftikhar

Reputation: 23

UNITY - Attack Hit Combo Code Error. Help needed

There's something wrong with my code. When I press the assigned keycode "Z", the CurrentState does not change to 1 and proceed with the whole combo. I tried putting a debug.log and found that the box collider does activate on pressing Z but the number does not increment by 1. Can anyone please help me out? Here's the code.

public class Fighter : MonoBehaviour {

public Collider[] attackhitboxes;
public Animator art;
public int CurrentState = 0;
bool activateTimertoreset = false;
public float currentcombotimer;
float origTimer = 50f;
private void Start()
{
    art = gameObject.GetComponent<Animator>();
    origTimer = currentcombotimer;
}
void Update()
{
    art.SetInteger("currentstate", CurrentState);
    NewComboSystem();
    ResetComboState(activateTimertoreset);

}

void ResetComboState(bool resettimer)
{
    if(resettimer)
    {
        currentcombotimer -= Time.deltaTime;

        if(currentcombotimer <= 0)
        {
            CurrentState = 0;
            activateTimertoreset = false;
            currentcombotimer = origTimer;
        }
    }
}
    private void LaunchAttack(Collider col)
{
    Collider[] cols = Physics.OverlapBox(col.bounds.center, col.bounds.extents, col.transform.rotation, LayerMask.GetMask("Hitbox"));
    foreach(Collider c in cols)
    {
        if(c.transform.parent.parent == transform)
        {
            continue;
        }
    }
}

void NewComboSystem()
{
    if (Input.GetKeyDown(KeyCode.Z))
    {
        activateTimertoreset = true;
        CurrentState++;
        if (CurrentState == 1)
        {
            LaunchAttack(attackhitboxes[0]); 
        }

        if (CurrentState == 2)
        {
            LaunchAttack(attackhitboxes[0]);
        }

        if (CurrentState >= 3)
        {
            LaunchAttack(attackhitboxes[0]);
        }
    }
}

}

Upvotes: 0

Views: 290

Answers (1)

Colby
Colby

Reputation: 31

Try putting your if (input.keydown) in the update function and have it call your attack. With the way you have it I believe it's calling your attack function many times a second because update runs every frame. Also try debugging the value of currentstate .

Edit: Are you assigning currentcombotimer a value in the editor since it's public? In the script it has no starting value. In the start function you assign it to the origTimer. If this value is zero then you have zero combo time.

Upvotes: 0

Related Questions