2D combo script - why it doesn't work

Hi

I wrote a script for Combo attack. It supposed to just have different animations for each attack, also Attack 1 and 3 are made by right arm, Attack 2 by left.
The problem is the character ionly permorms Attack 3. Am I just too naive thinking that playing with attack = true/false would work, lol?

public class PlayerCombat : MonoBehaviour
{
    public Transform attackPoint;
    public Transform attackPoint2;
    public float attackRange = 0.5f;
    public LayerMask enemyLayers;
    public int attackDamage = 40;
    public float attackRate = 2f;
    public Animator animator;
    bool Attack1;
    bool Attack2;
    bool Attack3;

    void Start()
    {
        Attack1 = true;
        Attack2 = false;
        Attack3 = false;
    }

    void Update()
    {
        if (Attack1 == true && (Input.GetKeyDown(KeyCode.P)))
        {
                Attack_1();
                Attack1 = false;
                Attack2 = true;
        }

        if (Attack2 == true && (Input.GetKeyDown(KeyCode.P)))
        {
                Attack_2();
                Attack2 = false;
                Attack3 = true;
        }

        if (Attack3 == true && (Input.GetKeyDown(KeyCode.P)))
        {
            Attack_3();
            Attack3 = false;
            Attack1 = true;
        }
    }

    void Attack_1()
    {
        animator.SetTrigger("Attack1");

        //Detect enemies
        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

        //Damage them
        foreach (Collider2D enemy in hitEnemies)
        {
            enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
        }
    }

    void Attack_2()
    {
        animator.SetTrigger("Attack2");

        //Detect enemies
        Collider2D[] hitEnemies2 = Physics2D.OverlapCircleAll(attackPoint2.position, attackRange, enemyLayers);

        //Damage them
        foreach (Collider2D enemy in hitEnemies2)
        {
            enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
        }
    }

    void Attack_3()
    {
        animator.SetTrigger("Attack3");

        //Detect enemies
        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

        //Damage them
        foreach (Collider2D enemy in hitEnemies)
        {
            enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
        }
    }

    private void OnDrawGizmosSelected()
    {
        if (attackPoint == null)
            return;
        
        Gizmos.DrawWireSphere(attackPoint.position, attackRange);

        if (attackPoint2 == null)
            return;

        Gizmos.DrawWireSphere(attackPoint2.position, attackRange);
    }
}

Animation settings:

1 Like

Sounds like you wrote a bug… and that means… time to start debugging!

Anything with Animations / Animators / Mechanim:

Only consider the code AFTER you have done this critical step:

Always start with the Animator state machine and prove it works in isolation, no code at all.

Here’s more reading:

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

lets think about this code

So we enter in at the top, and lets say attack1 is true the other 2 are not

is attack1 true and P pressed if yes, do stuff and unset attack1 but set attack2

so now attack2 is true

next bit of code
is attack2 true and P pressed if yes, do stuff and unset attack12 but set attack3

well yes, attack2 IS true, we just set it. so yes, this is now true… lets do this… attack3 becomes true

next bit of code
is attack3 true and P pressed if yes, do stuff and unset attack13but set attack1

see the problem?

Thank you for information! I’ll check it all :slight_smile: