- Home /
yield WaitForSeconds waits for too long.
When I run an empty project with this script attached to the main camera, the lag counter increments. The fixed one does not. I have a quadcore, so I'd say the processing power should be sufficient.
var currTime : float; var lag : int;
var currFixedTime : float; var fixedLag : int; function Start() { Time.fixedDeltaTime=0.05; while(true) { if (Time.time-currTime>0.1) lag +=1; currTime = Time.time; yield WaitForSeconds(0.05); } }
function FixedUpdate() { if (Time.time - currFixedTime > 0.1) fixedLag +=1; currFixedTime = Time.time; }
function OnGUI() { GUILayout.Label("Time: " + currTime); GUILayout.Label("Lag: " + lag); GUILayout.Label("Fixed: " + fixedLag); }
I assume I get the lags because WaitForSeconds is somehow tied into the framerate. How can I get the behaviour I'm getting from FixedUpdate, without using FixedUpdate? (Still want my physics to be calculated 50 times per second)
-Bonus Question:
Are NetworkViews synchronization lagging in the same way as the WaitForSeconds?
Answer by Mike 3 · Nov 13, 2010 at 10:53 AM
It could well be that you're using a large amount of memory, and every 20 seconds or so it's hitting a Garbage Collection.
Alternatively, it could be caused by an already low frame rate if you're trying to render too much.
Hard to tell without more information though, but it's probably a mixture of both.
The project is just the script I posted, and running on the default main camera. So not allocating any memory (other than OnGUI). The framerate is fine, because the project doesn't contain anything. The lags also occur in the standalone player, and somewhat in the editor (I set them to run in background and just let them run). What more info would you like?
In that case, really not sure. Try make a second lag counter, which you increment if Time.deltaTime (in Update) is greater than 0.1, see how the actual delta stacks up against your calculated one
The second lag counter got me thinking. I set my fixed update rate to 0,05, and in the fixed update loop tried doing the same thing as in the while(true) loop. That doesn't lag. Also, the counter you suggested gives approx the same as my counter. I also found the max deltaTime to be ~0.2 so apparently there are some lags. Edited my question, to include this.
FixedUpdate is called multiple times a frame to catch up, so isn't actually being called every 0.05s per se. What you could do is use InvokeRepeating("FunctionName", 0.05, 0.05); to have a constantly called function, which should give the same results as FixedUpdate
Of the three methods InvokeRepeating, WaitForSeconds and WaitForFixedUpdate I find WaitForSeconds to give the most consistent ti$$anonymous$$gs.
Answer by boymeetsrobot · Nov 13, 2010 at 08:35 AM
Try removing the if statement in the while loop and just run the yield.
The result is the same when I put the if statement into Update.
Try using WaitForFixedUpdate. Regarding your if statement, I would not use it at all if possible. Doing my own tests with Time.time with a similar method was less consistent for me.