- Home /
InvokeRepeating OR Update OR FIxedUpdate?
I'm basically thinking to avoid modifying every calculation with Time.deltaTime, could I just do an InvokeRepeating and haveit run at 30 or 60 fps (or whatever works) instead of putting time sensitive stuff in Update? ALternatively, while UT recommends using FixedUpdate for physics, could/should I use FixedUpdate to handle every other time sensitive calculation as well?
Just to confuse the issue more you could also use coroutines.
In response to OboShape, B$$anonymous$$ayne, and Bored$$anonymous$$ormon: I suppose I should clarify that while execution efficiency is important, I'm mostly looking for coding efficiency in this case. It would be very easy to forget to add a time.deltaTime call in a calculation and neeeeeever know until the user reports some strange occurances. I know this is all on the programmer's shoulders, so to speak, but if I could do away with the call altogether for fixed interval calculations that sure would be nice. I'm interested in InvokeRepeating via coroutine, now that you mention it, haha.
Answer by BMayne · Aug 31, 2014 at 12:31 AM
Hey JTSmith,
Don't be afraid of your friend Time.deltatime. Computer are super fast and doing that very simple math will not in that slightest have an effect your framerate. Using Invoke repeating would have more overhead then using your own timer system.
One thing that sucks about InvokeRepeating is that you have no control when it is really called (ie. [After/before/during] Update, [After/before/during] animations, [After/before/during] FixedUpdate). This does not sound like an issue but you have you other classes that depending on the function being called before they update on the same frame you might be out of luck.
What I suggest is making a simple timer class that you can use over and over again to call your functions. If you know how to use Delegates this is super easy to do.
Let me know if you have any other questions.
Regards,
Also use Update with Time.delta time and not FixedUpdate for non-physics stuff.
Fixed update can run faster or slower then update (this depends on your framerate). Say you had the class below
int fixedValue = 0;
int Value = 0;
void Update()
{
Value++;
}
void FixedUpdate()
{
fixedValue++;
}
Now if your game was running at 60 frames after a while both Value and FixedValue will be the same.
If your game was running slow FixedValue would be greater then Value (how much is based on how slow)
If your game was running faster then 60 FixedValue would be less the Value.
(For my quick math I just guess that 60 frames is what fixed update is set to do)
This can lead to broken logic in your game that will be very hard to find.
Answer by OboShape · Aug 31, 2014 at 12:11 AM
Hope im getting the gist of this JTSmith,
time.deltatime can be used in update or fixed update. deltaTime is the time difference between calls be it fixedupdate or update.
as far as I understand InvokeRepeating() uses a time difference in seconds, so therefore using deltatime behind the scenes based on your current target platform framerate to give you a call every x seconds.
certainly use fixedupdate for any physics movement etc, as update will not run in sequence with fixedupdate and you could get some shonky movement etc.
have a little look at the tutorial in the learn section, for a bit more info http://unity3d.com/learn/tutorials/modules/beginner/scripting/delta-time
im by no means any kind of expert but this is just what i interpret it to mean, if i understand it wrong then someone please correct me please :)
dont want to lead you up the preverbial garden path.
DaZ