- Home /
Issue with timer in combo attack
Hello,
I am trying to create a simple combo system for my game. I have a script that almost works but I have an issue with combo attack timer. So the problem is that inside my AttackAnimation() function I start the timer by setting activateTimerToReset to true and then I increment currentComboState to perform next combo attack. However inside the ResetComboState () my currentComboState is also incremented so instead of easyCombo [0] I have easyCombo [1] and so on, which means that function starts the timer of the wrong combo hit and also it doesn't reset the timer. Is there any way to solve this issue?
Here's my script so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackSystem : MonoBehaviour {
public AttackCharacteristics[] easyCombo = new AttackCharacteristics[3];
private float originalTimer;
private int currentComboState = 0;
bool activateTimerToReset = false;
Animator anim;
void Start ()
{
originalTimer = easyCombo [currentComboState].comboMaxTime;
anim = GetComponentInChildren <Animator> ();
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.A))
AttackAnimation();
ResetComboState (activateTimerToReset);
}
void AttackAnimation()
{
switch (currentComboState)
{
case 0:
activateTimerToReset = true;
anim.SetTrigger(easyCombo[currentComboState].animTriger);
currentComboState++;
Debug.Log ("1 hit.");
break;
case 1:
anim.SetTrigger(easyCombo[currentComboState].animTriger);
currentComboState++;
Debug.Log ("2 hit");
break;
case 2:
anim.SetTrigger(easyCombo[currentComboState].animTriger);
Debug.Log ("3 hit!");
currentComboState = 0;
break;
}
}
void ResetComboState (bool _timerToReset)
{
if (_timerToReset)
{
easyCombo [currentComboState].comboMaxTime -= Time.deltaTime;
if(easyCombo [currentComboState].comboMaxTime <=0)
{
currentComboState = 0;
activateTimerToReset = false;
easyCombo [currentComboState].comboMaxTime = originalTimer;
anim.SetTrigger("BreakCombo");
}
}
}
}
[System.Serializable]
public class AttackCharacteristics
{
public string animTriger;
public float comboMaxTime;
}
Two things from reading your code:
I see you resetting the current easyCombo timer in ResetComboState, but only if the timer runs out. I don't see where you reset it if you press the key in time. $$anonymous$$aybe you should reset the timer every time you switch to a new combo state?
I also noticed that ResetComboState only takes one argument, and does nothing when the argument is false. Rather than check for this in the function itself, I would check this condition during the update loop, and only call the function if activateTimerToReset == true. It avoids the overhead of loading a function every frame if it doesn't have to. Won't make a serious performance difference, but I thought I'd point it out.
Let me know if I'm misunderstanding your code.
First of all thanks for your reply, I will rewrite my code according to your advises. However the main issue is that ResetComboState starts wrong timer for each case. For example I press the A button for the first time, so the case 0 should be executed, and it should looks like this:
case 0: activateTimerToReset = true; anim.SetTrigger(easyCombo[0].animTriger); currentComboState++; Debug.Log ("1 hit."); break;
But then, because case 0 increments currentComboState++, ResetComboState function executes this code:
void ResetComboState (bool _timerToReset) { if (_timerToReset) { easyCombo [1].combo$$anonymous$$axTime -= Time.deltaTime; if(easyCombo [1].combo$$anonymous$$axTime <=0) { currentComboState = 0; activateTimerToReset = false; easyCombo [1].combo$$anonymous$$axTime = originalTimer; anim.SetTrigger("BreakCombo"); } } }
Here's screenshot from the inspector to make it more clearer:
Answer by madks13 · Jun 29, 2018 at 10:05 AM
Sorry, i fail to see where your currentComboState is incremented a second time. You set it to 0 in 2 places, but only 1 place (the switch) increments it.
Edit : below an exemple on autocycling :
public class AttackSystem : MonoBehaviour
{
public AttackCharacteristics[] easyCombo = new AttackCharacteristics[3];
public AttackCharacteristics currentCombo;
private float timer;
private int currentComboState;
private bool activateTimerToReset = false;
private Animator anim;
void Start()
{
ChangeCombo(0);
anim = GetComponentInChildren<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
AttackAnimation();
ResetComboState(activateTimerToReset);
}
void AttackAnimation()
{
if (currentComboState == 0)
{
activateTimerToReset = true;
}
anim.SetTrigger(currentCombo.animTriger);
Debug.Log((currentComboState + 1) + "hit.");
//Cycle trough elements of easyCombo
ChangeCombo((currentComboState + 1) % easyCombo.Length);
}
void ResetComboState(bool _timerToReset)
{
if (_timerToReset)
{
timer -= Time.deltaTime;
if (timer <= 0)
{
ChangeCombo(0);
activateTimerToReset = false;
anim.SetTrigger("BreakCombo");
}
}
}
void ChangeCombo(int index)
{
currentComboState = index;
currentCombo = easyCombo[index];
timer = currentCombo.comboMaxTime;
}
}
I increment currentComboState in each case. The problem occures in ResetComboState function.
For example I press the A button for the first time, so the case 0 should be executed, and it should looks like this:
case 0: activateTimerToReset = true;
anim.SetTrigger(easyCombo[0].animTriger);
currentComboState++;
Debug.Log ("1 hit."); break;
But then, because case 0 increments currentComboState++, ResetComboState function executes this code:
void ResetComboState (bool _timerToReset)
{ if (_timerToReset)
{ easyCombo [1].combo$$anonymous$$axTime -= Time.deltaTime;
if(easyCombo [1].combo$$anonymous$$axTime <=0)
{ currentComboState = 0;
activateTimerToReset = false;
easyCombo [1].combo$$anonymous$$axTime = originalTimer;
anim.SetTrigger("BreakCombo"); }
} }
Here's screenshot from the inspector to make it more clearer:
I see,
then, why not simply get the ref to current combo state object and use that ins$$anonymous$$d of the index?
Thanks for your reply and the code that you've posted above. However, the issue still persists - when I press A button 1 time, I see "2hit." in the console, and also Attack2 trigger activates, ins$$anonymous$$d of Attack1.
Your answer
![](https://koobas.hobune.stream/wayback/20220612165703im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
mecanim blend tree attack combo 2 Answers
Animations Combo 0 Answers
Resetting Combos 1 Answer
How do you script combo attacks? 2 Answers
Combo Attack Chain Script Help 0 Answers