I have a float that is constantly being updated in Update(), and I need to store what that float was one second ago, but I need to do this constantly. So like a variable that is the exact same as another variable but it is lagging behind by one second. If I constantly start a coroutine that saves the original float in var1, waits for 1 second, and then sets var1 equal to var2, so that var2 is constantly lagging behind the original float by one second, will this be performance-intensive to constantly be starting a coroutine? But would this work, as var1 would be constanyl updated by the coroutines starting after, so it wouldn’t lag behind by one second?
Starting a new coroutine every single frame is definitely going to be a performance hit, especially with all the garbage generation and collection. I’d say your best bet is to have a reasonable sample rate, say 1/Nth of a second, and a corresponding List with N elements. Every 1/Nth of a second, move every element in the list towards 0 and push the current value of your float to the last entry. The value at list[0] is the value roughly one second ago. 1/10th or 1/20th of a second should be more than fine for gameplay.
yes as above but instead of moving the elements of the list around just update the current position
float X[60]; // 60 updates a second
int current_pos=0;
FixedUpdate()
{
a second ago = X[current_pos];
X[current_pos] = new float value
current_pos++;
}
Don’t forget your wrap!! ![]()
1 Like
Oh yes of course ![]()
![]()
1 Like