- Home /
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.
Answer by Bunny83 · Dec 27, 2019 at 12:51 PM
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.
of course the lerp approach is much more versatile. For example if you have variable a which you want to move from 10 to 2 and b which you want to move from 5 to 3 in the same time you just have to plug in the $$anonymous$$ / max values in the lerps and you get the desired result.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220623im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I smooth out my crouch camera movement 0 Answers
Is there a way to smoothly transition between two floats, with it slowing down towards the end 1 Answer
Mathf.Lerp not working as expected 2 Answers
How do I fix this lerp this float so it doesnt snap from animation to another 1 Answer
lerp with float problem!! 1 Answer