0

I have a list of harvested crops. There's a capacity of let's say 100 crops(100 items in a list). Now I want to empty the list within X seconds when max full (2 sec in my case). This should be relative to the max cap. If the list is 100 it should take 2 sec if 50 then 1 sec accordingly. What should be the approach for achieving that?

The reason for this is that I want to iterate over each crop item and animate them towards some point for selling. I have tried for loop but it's not quite giving me the right time as my max cap changes over time when the user upgrades but I want the animation time to be constant to 2 sec when the list is maxed up.

This is what I tried:

 private IEnumerator MoveHarvestToStore()
{
    float startTime = Time.time;
    animationTimeForSelling = 2.0f * trunkCapacityFilled / trunkTotalCapacity;
    Debug.Log("Anim Time = " + animationTimeForSelling);
for (int i = pickablesInHarvester.Count - 1; i >= 0; i--)
{
    float animTime = animationTimeForSelling / pickablesInHarvester.Count;
    yield return new WaitForSeconds(animTime);
}
float endTime = Time.time;
Debug.Log("Time took = " + (endTime - startTime));

}

The problem with the above code is that this is not consistent in terms of time. Following are the logs with the same trunkCapacityFilled and trunkTotalCapacity every time:

Anim Time = 2, Time took = 2.17

Anim Time = 2, Time took = 2.15

Anim Time = 2, Time took = 2.16

Anim Time = 2, Time took = 2.213

Anim Time = 2, Time took = 2.13

Syed Munim Raza
  • 467
  • 6
  • 14
  • "I have tried for loop" Show us. – DMGregory Nov 23 '21 at 12:22
  • @DMGregory updated – Syed Munim Raza Nov 23 '21 at 12:43
  • @DMGregory I have updated my findings and logs as well. I need anim time and actual time is taken to be the same. These logs are with trunkTotalCapacity = 150 and trunkCapacityFilled =150 – Syed Munim Raza Nov 23 '21 at 13:32
  • This looks like what we'd expect, no? Your code asks for the operation to finish in 2*150/150 = 2 seconds, and we find that the end time is 2 seconds plus a small margin due to rounding to the next frame on each iteration. So we've landed quite close to the requested time. You can land closer by adjusting for the rounding on each frame. How does this differ from what you want/expect? – DMGregory Nov 23 '21 at 13:47
  • If the capacity goes to 350 then it starts to fluctuate even larger like 2.3s I can accept bit lower fluctuations like 2.05s or something like that but larger values are not what I want. I didn't get what you meant by saying about "rounding to the next frame" – Syed Munim Raza Nov 23 '21 at 13:59
  • I've tried debugging time passed which is WaitForSeconds(0.13333f) as animationTimeForSelling(2.0f) / pickablesInHarvester.Count(15). now 0.13333*15(loop count) = 1.99995. Now the extra few mili seconds are coming from unity's WaitForSeconds as far as I believe. @DMGregory – Syed Munim Raza Nov 23 '21 at 14:15

2 Answers2

1

Divide your current amount by max amount and you'll get the percentage amount of items that you have, then multiply this number by you full time and you'll end with the seconds to empty the current bag.

currentAmount / maxAmount * maxTime = x

Taking your example

50 / 100 = 0.5 0.5 * 2 (sec) = 1 sec

Nefisto
  • 111
  • 1
0

Remember that WaitForSeconds waits at least that long. It's only ticked once per frame, so if a frame lands slightly before the waiting deadline, you don't resume until the next frame later - which might be nearly 16 ms later than planned if you're running at 60 FPS. So if you have an average of 8 ms extra delay per item, times 350 items, that could slow down your full iteration by nearly 3 seconds in total!

What you can do is keep track of how long you've actually waited at each step. So if the next frame lands a little later than your desired wake-up time, you subtract the time you overslept from your next alarm, to correct for the error. That way the error does not have an opportunity to accumulate.

float accumulator = 0f;
for (int i = pickablesInHarvester.Count - 1; i >= 0; i--)
{
float animTime = animationTimeForSelling / pickablesInHarvester.Count;
accumulator += animTime;
while (accumulator > 0) {
    yield return null;
    accumulator -= Time.deltaTime;
}

}

DMGregory
  • 134,153
  • 22
  • 242
  • 357