- Home /
Input triggering action multiple times intermittently
Having trouble figuring out what's causing an intermittent bug. The issue is that the "explosionA" animation will sometimes trigger multiple times when either holding the A key or pressing it rapidly. I can't accurately reproduce it since it happens so sporadically, so I'm having trouble figuring out the issue.
void Start() {
aUsable = true;
aCooldown = 0.5f;
aTimer = 0;
}
void Update () {
CooldownManager();
Cast();
}
void Cast() {
if (Input.GetKey(KeyCode.A) && aUsable){
spell.SetTrigger("explosionA");
aUsable = false;
}
}
void CooldownManager() {
if (Time.time > aTimer){
aUsable = true;
aTimer = Time.time + aCooldown;
}
}
On a side note, adding in a Time.time check into the if statement of the keypress check fixes the issue, but I need to be able to handle several different cooldowns of varying lengths and I'd rather not bundle them in with the keypress checks.
Moving Cast() to FixedUpdate() does not resolve the isse.
Any help would be appreciated.
Answer by Kiwasi · Jul 17, 2014 at 10:48 AM
Your cool down timer rests every 0.5 seconds, not after 0.5 seconds. Your bug occurs on the random frame where you manage to hit A just before, then just after the update. Here is a fix.
void Cast() {
if (Input.GetKey(KeyCode.A) && aUsable){
spell.SetTrigger("explosionA");
aUsable = false;
aTimer = Time.time + aCooldown;
}
}
void CooldownManager() {
if (Time.time > aTimer){
aUsable = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613154307im_/https://answers.unity.com/themes/thub/images/avi.jpg)