- Home /
WaitForSecond Corountine Sleep Time Varies
Code:
public float shooting_wait_time = 0.2f;
public int bullets;
private bool waiting = false;
void Update() {
if (Input.GetButtonDown("Fire1") && bullets != 0) {
if (!waiting) {
// Do shooting stuff
StartCoroutine(shoot_wait());
}
}
}
private IEnumerator shoot_wait() {
waiting = true;
yield return new WaitForSecondsRealtime(shooting_wait_time);
waiting = false;
}
As you can see I have a Coroutine that waits for 0.2 seconds... The problem is that sometimes it waits the right duration, but sometimes it does a short bursts of no sleep time! I've been looking it up for a long time but I have found no solutions for me.
I don't see what I'm doing wrong. Please help. Thank you in advance.
Comment
You were right. Some poorly tied animator was the culprit. Thank you!
Best Answer
Answer by Coulomb1 · Oct 17, 2021 at 08:17 PM
The problem wasn't in the code, it was because of a poorly tied to together animator.
Answer by Parokonnyy · Oct 15, 2021 at 08:29 AM
Try to use default WaitForSeconds()
I always use it and it works fine for me.