Reduce 2 numbers at the same rate

Hi, I am having trouble creating a equation that does the following;

takes a float = 10;

and another float = 5;

and reduces both down to 0 over a desired period of time, so they both reach 0 at the same time.

I am thinking a Lerp function but not sure how to make them reach 0 at the same time.

Well, your question is a bit abstract. If you really just want both values to be linked together to reach 0 at the same time there are several possibilities. Though it depends on if the start values are fix / constant known values (like always 10 and 5) and if that time period is also a known time period.

First you can use an independent timer variable “t” which you let go from 0 to 1 in your desired time period. Now you can simply use Lerp with that reference time to get your desired intermediate values

float t = 0;

void Update()
{
    t += Time.deltaTime / timePeriodInSeconds;
    a = Mathf.Lerp(10,0,t);
    b = Mathf.Lerp(5,0,t);
}

This assumes you know exactly the time in seconds the process should take. However if instead of the time you have a certain speed for one of the variables, you can directly decrease one of your two variables with your desired speed and have the other variable just depend on the first.

float a = 10;

void Update()
{
    a -= Time.deltaTime * speed;
    b = a * (5f / 10f);
}

Here 5f / 10f is the ratio in speed between the two variables. So since the value a is twice as big as b, b is always half of a.

Finally of course you can just reduce both variables independent from each other but calculate a dedicated speed for each variable so the reach 0 at the same time.

float a = 10;
float b = 5;
void Update()
{
    a -= Time.deltaTime * speed;
    b -= Time.deltaTime * speed * (5f / 10f);
}

Of course decreasing them independently might cause slight variations due to rounding errors. But at max you get a one frame difference.

Note that you have to take care of when you reach your desired target. Mathf.Lerp is clamped. So when “t” goes beyond 1 it just behaves as if it stays at 1. However all the other solutions without lerp would continue decreasing if you do not clamp the value or take care of when you reach your target.