# Mathf.Lerp float between 0 and 1 based on boolean input

I’m trying to lerp a float value from the current value to 1 if the boolean is true and from the current value to 0 if the boolean is false.

The function should result in a simulation of a getAxis(horizontal) keypress when the horizontal value gradually increases till 1 when the key is pressed and decreases down to 0 when released.

I tried this:

``````public float leftNumber;
public bool leftBool;
float lerpSpeed = 0.3f;

private void Update()
{
if (leftBool)
{
leftNumber = Mathf.Lerp(leftNumber, 1, lerpSpeed);
}
else
{
leftNumber = Mathf.Lerp(leftNumber, 0, lerpSpeed);
}
}
``````

The result:

• leftbool = true

• leftNumber value increases very fast to 0.9999999.

• leftbool = false

• leftNumber value decreases very slowly to 1.401298e-45

I also tried:

``````    void Update()
{
if (rightBool)
{
if (rightNumber < 1f) rightNumber += 0.01f;
}
else
{
if(rightNumber > 0f) rightNumber -= 0.01f;
}
}
``````

The result:

• rightbool = true

• rightNumber value increases to 1.009999

• rightbool = false

• rightNumber value decreases to -0.009999925

What I want to achieve

• leftbool = true

• leftNumber value increases to 1

• leftbool = false

• leftNumber value decreases to 0

Both should be at the same “speed”

public float leftNumber;
public bool leftBool;
public float lerpSpeed = 0.3f;
private float timer;

``````private void Update()
{
if (leftBool)
timer = Mathf.Clamp01(timer + Time.deltaTime * lerpSpeed);
else
timer = Mathf.Clamp01(timer - Time.deltaTime * lerpSpeed);
// OR
// timer = Mathf.Clamp01(leftBool
//    ? timer + Time.deltaTime * lerpSpeed
//    : timer - Time.deltaTime * lerpSpeed);
leftNumber = Mathf.Lerp(0, 1, timer);
}
``````

I was able to get back and forwards movement with tweened values using Math.MoveTowards, as @Bunny83 pointed out! You don’t even need `if` statements!

``````// input is 1 or 0 or -1
// increase or decrease from current value to new input
currentValue = Mathf.MoveTowards(inputValue, currentValue, 0.25f * Time.deltaTime);
// apply to transform
transform.Translate(Vector3.forward * Time.deltaTime * currentValue * speed);
``````

Lerp is not working how you think it is. The third parameter is the ‘current’ position between the start and end value. This obviously wants to be updated or it will never move, which is why we use Time.deltatime to update it between 0 and 1 (or whatever start/end value you have).

``````private float LerpTime = 1;
private float m_CurrentTime;

private void Update()
{
while (leftBool & m_CurrentTime < m_LerpTime)
{
m_CurrentTime += Time.deltaTime;
var percent = m_CurrentTime / m_LerpTime;
leftNumber = Mathf.Lerp(startNumber, endNumber, percent);
}
}
``````

If leftBool is true, then it should lerp to the end value instead of getting stuck at 0.999999. There are a couple of issues with this though…

If you introduce your “else” here, then it will constantly try to lerp to 0 until leftBool becomes true. Which means you might want to introduce some condition to trigger the whole thing.

You will also need to reset the timer once the lerp has completed its cycle if you want it to be able to run more than once that is (presumably you do). But one thing to consider is if it is currently lerping towards 1, and leftBool becomes false, do you want it to cancel its lerp towards 1 and start lerping back to 0?

It might be worth checking out coroutines, where you can use it to lerp, but also cancel the current coroutine running if it gets called again. Then you ensure only one lerp is happening at once.