Why Is My Combo Script Not Working?

My first animation keeps playing, but it never moves on to the second one. What I’m trying to do is play the second animation after the key is hit within a set amount of time. I’ve tried using a random method before which works great(it plays the animations I’ve set randomly), but it doesn’t play in the order I want(obviously since it’s random :P).

using UnityEngine;
using System.Collections;

public class Player_ATK : MonoBehaviour {
    Animator animate;
    private const float coolDown = 0.7f;
    private float startTime;

    // Use this for initialization
    void Start() {
        animate = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update() {
        {
            if (Time.time <= startTime + coolDown)
                return; 
            if (Input.GetKeyDown("1"))
            {
                startTime = Time.time;
                animate.Play("Sword_Atk1", -1, 0f);
                Debug.Log("Hit1!");
            }
            else if (Input.GetKeyDown("1") && coolDown <= 0.7)
            {
                startTime = Time.time;
                animate.Play("Sword_Atk2", -1, 0f);
                Debug.Log("Hit2!");
            }
//From earlier testing.
            //else if (Input.GetKeyDown("1"))
            //{
            //    CancelInvoke("Attack1");
            //    CancelInvoke("Attack2");
            //    Invoke("Attack3", 1f);
            //}
        }
   
    }
// These are here in the case of Invoking.
    void Attack1()
    {
        animate.Play("Sword_Atk1", -1, 0);
    }
    void Attack2()
    {
        animate.Play("Sword_Atk2", -1, 0);
    }
    void Attack3()
    {
        animate.Play("Sword_Atk3", -1, 0);
    }
}

Been trying to figure this out for about 3 days now. Any help would be greatly appreciated. I’m also trying to avoid the use of Coroutines.

The second part of the ifelse statement will never get called. You need more constraints on line 19. Also since you never change the cooldown time then both statements are effectively calling the same thing.

1 Like

Interesting. So if I’m understanding correctly, would something like this work(not able to use unity at the moment)?

 if (Time.time <= startTime + coolDown && Time.Time == 0f && Time.Time <= 0.1f)
                return;
            if (Input.GetKeyDown("1"))
            {
                startTime = Time.time;
                animate.Play("Sword_Atk1", -1, 0f);
                Debug.Log("Hit1!");
            }
            else if (Input.GetKeyDown("1") && coolDown <= 0.7 && Time.time <= 0.4f)

No, you need a constraint on the first if. The else will never get called as it has the same restraints as the first and more.

1 Like

Alright then I’m a bit confused on what you mean by “constraint”. When you say constraint, I’m thinking of adding a second coolDown(coolDown2, coolDown3 and so on) to the if statement in order to differentiate the multiple if statements.

1 Like

Your code is getting a bit messy, this should be easier to understand.

Heres the code (this is untested I just wrote from my head):

public int count; //the current count of the combo
    public int resetTime; //the time it takes the combo to reset

    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            CancelInvoke("Reset");
            Invoke("Reset", resetTime);
            switch (count)
            {
                case 1:
                    anim.Play("Attack1");
                    break;
                case 2:
                    anim.Play("Attack2");
                    break;
            }
            count++;
        }
    }
    void Reset ()
    {
        count = 1;
    }

This code is pretty easy to understand, if you have any questions about it feel free to ask! :slight_smile:

1 Like

Thanks for leading me in the right direction. Very simple one button combo. There are a lot of methods out there that are way to complicated to achieve something so simple. Here’s the code.

using UnityEngine;
using System.Collections;

public class Player_Attack : MonoBehaviour {
    Animator animate;
    public int count; // Combo Count.
    public int countTimer = 1;
    public bool coolDown = false; // Cool down timer between attacks. Defined in Invoke. When set to true, attack counter starts.
   

    // Use this for initialization
    void Start () {
        animate = GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update () {
        if (coolDown == false)
        if (Input.GetKeyDown("m"))
        {
                CancelInvoke("counterCount");
                Invoke("counterCount", countTimer);
                switch (count)
            {
                case 1:
                    animate.Play("Sword_Atk1", -1, 0f);
                    Invoke("pauseCombo", 0.5f);
                    coolDown = true;
                    break;
                case 2:
                    animate.Play("Sword_Atk2", -1, 0f);
                    Invoke("pauseCombo", 0.5f);
                    coolDown = true;
                    break;
                case 3:
                    animate.Play("Sword_Atk3", -1, 0f);
                    Invoke("pauseCombo", 1f);
                    coolDown = true;
                    break;
                default:
                    count = 0; // Resets the combo counter to zero.
                    break;

            }
            count++;
        }
     
    }
    void pauseCombo() // This pauses the attacks.
    {
        coolDown = false;
    }
    void counterCount() // Time between attacks. If this isn't met, it returns to first attack in sequence.
    {
        count = 1;
    }
}
1 Like