Recently I faced a little problem. In my game (and I’m pretty sure in yours too) I use this construction very often:
void Update()
{
sinceLastAction += Time.deltaTime;
if (sinceLastAction > actionTimeout)
{
DoAction();
sinceLastAction = 0;
}
}
But when actionTimeout
has low float value (like 0.5 or 0.1) you can stuck in situation when Time.deltaTime
has half or even greater value of your time counter (especially when you manipulate with Time.timeScale
), and it’s bring problem when part of your time just get deleted each Update()
. Other example, your game got lag or something and Time.deltaTime
pretty high, in some cases you can have more then one not executed actions. So is my question, should I use constructions like this:
void Update()
{
sinceLastAction += Time.deltaTime;
if (sinceLastAction > actionTimeout)
{
int timesActionHappend = Mathf.FloorToInt(sinceLastAction/actionTimeout);
for (int i = 0; i < timesActionHapped; i++)
DoAction();
sinceLastAction -= actionTimeout * timesActionHappend;
}
}
, or I just misunderstanding some basic principles of time based programming?