SoBiT
1
What is the best way to check if the function has already been called in the last X seconds?
Is increasing a Variable each second good for performance? Or are there better possibilities?
Here’s a simple example of one way to do this. You track elapsed time by adding time.deltaTime to a float variable every frame. Once the float variable exceeds your target time (I’m using 10 seconds here, you should make it a constant or store in another variable so it’s easily configurable), execute the timed code, and reset the timer back to zero.
float timeSinceLastCall;
void Update()
{
timeSinceLastCall += time.deltaTime;
if (timeSinceLastCall >= 10)
{
// TODO : do timed stuff
timeSinceLastCall = 0; // reset timer back to 0
}
}
If you just want to execute something repeatedly every x seconds, you might also take a look at InvokeRepeating.