- Home /
Accuracy of WaitForSeconds
I'm using WaitForSeconds to pause during a scripted animation. I'm finding that it is around .015 seconds slow. Is this really the accuracy of WaitForSeconds?
For example, this code consistently reports being off by .015 on my system.
using UnityEngine;
using System.Collections;
public class TestWaitForSeconds : MonoBehaviour {
private float lastTick;
void Start () {
StartCoroutine(Test());
}
private IEnumerator Test() {
while(true) {
lastTick = Time.time;
yield return new WaitForSeconds(.1f);
Debug.Log("Off by: " + Mathf.Abs((Time.time - lastTick) - .1f));
}
}
}
Answer by hexagonius · Aug 28, 2017 at 09:00 PM
Each frame takes a certain amount of time. WaitForSeconds doesn't continue execution the moment the time runs out, it simply checks the passed time during frames, which means that you won't get a spot on continuation, but a continuation as soon as the first frame past the time executes. If you wait 20 and a frame takes 40, you'd continue at 40.
Yeah, I didn't really think this through but .016 is one frame at 60fps..
Your answer
Follow this Question
Related Questions
Coroutine Countdown Timer being extremely slow 1 Answer
Using a unique time for yield WaitForSeconds 3 Answers
Time Manager Class Implementation Like Coroutine in Unity 1 Answer
Fog density over time. 1 Answer
Calculate in Seconds not Frames? 2 Answers