- Home /
Resetting a combo chain
I'm trying to use mecanim to chain combo animations together. My trouble is the counter that I assign in anim will always be 0, because Update() runs much faster than you can click the mouse to increase the counter. Is there a simple way to execute this every fifth update or so, or delay it so the counter can increase properly?
void Update(){
if(Input.GetButtonDown("Fire1")){
if (Mathf.Abs(Time.time - nextPunch) < 2){
switch(punch){
case 0:
//This is the first swing in the chain
punch = punch + 1;
nextPunch = Time.time + punchRate;
anim.SetFloat("Punch", punch);
break;
case 1:
//This is the second swing in the chain
punch = punch + 1;
nextPunch = Time.time + punchRate;
anim.SetFloat("Punch", punch);
break;
case 2:
//This is the last swing in the chain
punch = punch + 1;
nextPunch = Time.time + punchRate + punchDelay;
anim.SetFloat("Punch", punch);
break;
}
}
}
else {
punch = 0;
nextPunch = 0.0f;
}
}
Answer by amphoterik · Jul 15, 2013 at 11:55 AM
A counter system is pretty easy. Just create a counter, threshold, and test:
int counter = 0;
int threshold = 5;
void Update()
{
if(++counter <= threshold) return;
counter = 0;
//rest of your code
}
This will only run the update every 5 times.
Now, while that answers your question, it is probably not great to delay the update method. You might be doing other important things in there. Instead, you would want to delay only the resetting of the combo. You would also want to do it based on time and not a set number of updates. For example, if you wanted to reset the counter every half second:
float counter = 0;
float threshold = .5f;
void Update()
{
counter += Time.deltaTime;
if(counter <= threshold) return;
counter -= threshold;
//rest of your code
}
Thanks so much, the second one worked perfectly! It should be noted for future searchers that my update loop is now structured as such:
void Update()
{
counter += Time.deltaTime;
if(counter <= threshold) //notice no semicolon
//combo code
return;
counter -= threshold;
//continue updating
}
This allows my update to run while there isn't a combo going on. The original code as posted will cut update short every frame, which didn't work for my purpose.
Glad it worked. Please select my answer as the answer to this question. It helps future readers and gives you karma as well.
Your answer
Follow this Question
Related Questions
How to update 3D character 1 Answer
Does anybody have best practices on creating an action-adventurer character (Mecanim)? 0 Answers
Mecanim Trigger Stays too long true 1 Answer
mecanim blend tree attack combo 2 Answers
Mecanim 1 frame delay [solved] 2 Answers