# How to lerp with a changing "from" value?

It’s possible I’m not even using this right but basically I’m making a hunger system and I’m calling this in fixed update:

``````   public void Starve()
{
if(CurrentHunger > 0)
{
CurrentHunger = Mathf.Lerp(CurrentHunger, 0, HungerRate * Time.deltaTime);
}

}
``````

In this example, CurrentHunger starts as 100. Hunger rate is 15. My time.deltaTime is 0.02. With this use of lerp, basically, my from value is always changing.

So:

• 1 second it is 70
• 2 seconds it is 49
• 3 seconds it is 34.3
• 4 seconds its is 24.01

See? It’s not decrementing linearly. Am I even using lerp right? And what do I need to do to fix this?

Lerp won’t work in that case. Both values need to be constant.

Instead of Lerp you might want to use Mathf.MoveTowards which does exactly what you want.

Lerp is implemented like this:

``````public static float Lerp(float a, float b, float t)
{
return a + (b - a) * Mathf.Clamp01(t);
}
``````

while MoveTowards is implemented like this:

``````public static float MoveTowards(float current, float target, float maxDelta)
{
if (Mathf.Abs(target - current) <= maxDelta)
{
return target;
}
return current + Mathf.Sign(target - current) * maxDelta;
}
``````

You can also use the following formula to lerp a changing value to a target, such as in this case a transform.position during Update:

``````transform.position = Vector3.Lerp(transform.position, targetPos, 1f / (1f + dampen * Time.deltaTime));
``````

Here, `dampen` is a value above zero, where zero will apply no dampening, and anything above zero will make the transition to the target value progressively slower