Lerp from A to B with time X [Help required]

hello fellow unity devs. i came across a little problem i for some reason cannot find out how to do.

i wish to do a lerp based on how long a specific action should take.

A = 0
B = 1

X = 5 seconds

so from A To B should take 5 seconds
and i wish to know how i can calculate based on the time how much it should lerp per second so to speak.

Well, if that was your question, ‘.2’ / s :wink:

Lerp takes a starting value, and ending value, and a clamped interpolation (0…1) between the two.

In a coroutine…

IEnumerator SimpleLerp(){
 int a = 0;  // start
 int b = 1;  // end
 int x = 5;  // time frame
 float n = 0;  // lerped value
for(float f = 0; f <= x; f += Time.deltaTime) {
  n = Mathf.Lerp(a, b, f / x); // passing in the start + end values, and using our elapsed time 'f' as a portion of the total time 'x'
  // use 'n' .. ?
  yield return null;
 }
}

Hope that helps. :slight_smile:

1 Like

this is the function i am using it for

public Color TurnObjectsVis()
    {
        for (int i = 0; i < GraphicsObject.Count; i++)
        {
            GraphicsObject[i].SetActive(true);

            if (GraphicsObject[i].GetComponent<SpriteRenderer>() != null)
            {
                sr = GraphicsObject[i].GetComponent<SpriteRenderer>();
                sr.color = Color.Lerp(new Color(sr.color.r, sr.color.g, sr.color.b, sr.color.a), new Color(sr.color.r, sr.color.g, sr.color.b, 255), 0.0000100f);

                if (GraphicsObject[i].gameObject.GetComponent<ParticleSystemRenderer>() != null)
                {
                    ParticleSystemRenderer psr = GraphicsObject[i].GetComponent<ParticleSystemRenderer>();
                    Material mr = psr.material;

                    mr.color = Color.Lerp(new Color(mr.color.r, mr.color.g, mr.color.b, mr.color.a), new Color(mr.color.r, mr.color.g, mr.color.b, 255), 0.0000100f); // i want to replace that horrible 0.0000100f with the value

                }
                if (sr.color.a >= 1f)
                {
                    this.gameObject.GetComponent<Collider>().enabled = true;
                }
           
            }
        }
        return sr.color;
    }

oh and everything is in a update loop

mmh i decided to move alot of stuff to Corutines as they actually do less checking and less work then update. i am prob going to need like 10 corutines but that shouldn’t be bad?

Have you considered a tweening engine like LeanTween or Dotween instead?

Actually it is very simple

float lerpingValue;
float A, B;
float time, duration;
bool isLerping;


public void MyLerp(float A, float B, float X)
{
    this.A = A;
    this.B = B;
    this.duration = X;
    this.time = 0;
    this.isLerping = true;

    // 'move per second' value can be calculated like this
    float movePerSecond = (B - A) / X;
}


void Update()
{
    if (isLerping)
    {
        // Accumulating time
        time += Time.deltaTime;

        // determining where are we on the time line
        // flow is our time line increasing from 0 to 1
        float flow = time / duration;

        if (flow < 1)
        {
            // lerping formula
            lerpingValue = A + (B - A) * flow;
        }
        else
        {
            // operation is done
            lerpingValue = B;
            isLerping = false;
        }
    }
}

You can call it like this:

MyLerp(0, 1, 5);

Tell me if I understood something wrong.
Hope it helps

its somewhat what i tried to do. and i ended up trying to use corutines but i quickly discovered with my system i build atm i can’t really do that without doing to much extra code.

i would diff like to move my code out of a constant update loop. so i can get more control over the code.

However you write your code, it is gonna run in something like an update loop. Either coroutines or actual update methods run every frame. So there is not much of a choice here.
But about having control over your code, what do you want to do exactly? We can modify this code to fulfill your needs.

well i just wrote a new thread. about my somewhat problem. https://discussions.unity.com/t/678585

I typically do my interpelations in coroutines, like this one…

private IEnumerator InterpelateRoutine()
{
    float lerp = 0;
    while(duration>0 && lerp<1)
    {
        lerp = Mathf.MoveTowards(lerp,1,Time.deltaTime / duration);
        current = Mathf.Lerp(start,end,lerp);

        yield return null;
    }
    current = end;
}

I actually use my own interpolation framework instead of directly using Lerp,Slerp, SmoothStep, etc… but the concept is the same, and the code simple.

doing it in Update isn’t much different…

private void Update()
{
    lerp = (duration>0 && lerp<1)
        ?Mathf.MoveTowards(lerp,1,Time.deltaTime / duration)
        :1;

    current = Mathf.Lerp(start,end,lerp);
}