Slow down the speed value of lerp from a spesific distance

Hi

I am moving a certain object to the target with a certain speed using Coroutine.

What I want is for the object to slow down when it comes to a certain distance from the target, but still reach the target.
I just need to implement “current speed” and “remaning distance” paremeters or something else to here:

float distance2 = Vector3.Distance(go.transform.position, TargetPosition.Value);

                    if (distance2 < 5.0f)
                    {
                       speed = Mathf.MoveTowards(remainingDistance, 0.0f, Time.deltaTime);
                    }

For an example, when i added this line (* 2.5f) to this code, it works great and it stops smooth. (if my projectile speed is 15)

float distance2 = Vector3.Distance(go.transform.position, TargetPosition.Value);

                    if (distance2 < 5.0f)
                    {
                        speed = Mathf.MoveTowards(remainingDistance * 2.5f, 0.0f, Time.deltaTime);
                    }

with *2.5f: ( speed is 15)
8367192--1102356--Animation.gif
without *2.5f: ( speed is 15)
8359842--1100535--Animation2.gif

However, When i change the projectile speed, it doesn’t stop smooth again.

with *2.5f: ( speed is 5)
8359842--1100538--Animation3.gif

So I need to change this *2.5f value dynamically. But I don’t know how…

My whole Coroutine:

 public IEnumerator GotoPosition()
        {
            var go = Fsm.GetOwnerDefaultTarget(gameObject);
            Vector3 startPosition = go.transform.position;
            Vector2 Noise = new Vector2(Random.Range(MinNoise.x, MaxNoise.x), Random.Range(MinNoise.y, MaxNoise.y));
            Vector3 BulletDirectionVector = new Vector3(TargetPosition.Value.x, TargetPosition.Value.y + yOffset, TargetPosition.Value.z) - startPosition;
            Vector3 HorizontalNoiseVector = Vector3.Cross(BulletDirectionVector, Vector3.up).normalized;
            float distance = Vector3.Distance(go.transform.position, TargetPosition.Value);
            float remainingDistance = distance;

      

            while (remainingDistance > disableFollowDistance)
            {
                if (smoothEnd.Value)
                {

                    float distance2 = Vector3.Distance(go.transform.position, TargetPosition.Value);

                    if (distance2 < 5.0f)
                    {
                        speed = Mathf.MoveTowards(remainingDistance * 2.5f, 0.0f, Time.deltaTime);
                    }
                    else
                    {
                        speed = MoveSpeed.Value;
                    }
                }
              
                NoisePosition = NoiseCurve.curve.Evaluate(1 - (remainingDistance / distance));

                if (followTargetBool)
                {
                    if (useCurve)
                    {
                        go.transform.position = Vector3.Lerp(startPosition, TargetPosition.Value + new Vector3(0, yOffset, 0), 1 - (remainingDistance / distance)) + new Vector3(HorizontalNoiseVector.x * NoisePosition * Noise.x, NoisePosition * Noise.y, NoisePosition * HorizontalNoiseVector.z * Noise.x);
                    }
                    else
                    {
                        go.transform.position = Vector3.Lerp(startPosition, TargetPosition.Value + new Vector3(0, yOffset, 0), 1 - (remainingDistance / distance));
                    }
          

                    lookVector = (lastPosition - go.transform.position).normalized / Time.fixedDeltaTime;
                    lastPosition = go.transform.position;
                    go.transform.rotation = Quaternion.LookRotation(-lookVector);
                }
                else
                {
                    if (useCurve)
                    {
                        go.transform.position = Vector3.Lerp(startPosition, reachPos + new Vector3(0, yOffset, 0), 1 - (remainingDistance / distance)) + new Vector3(HorizontalNoiseVector.x * NoisePosition * Noise.x, NoisePosition * Noise.y, NoisePosition * HorizontalNoiseVector.z * Noise.x);
                    }
                    else
                    {
                        go.transform.position = Vector3.Lerp(startPosition, reachPos + new Vector3(0, yOffset, 0), 1 - (remainingDistance / distance));
                    }
              

                    lookVector = (lastPosition - go.transform.position).normalized / Time.fixedDeltaTime;
                    lastPosition = go.transform.position;
                    go.transform.rotation = Quaternion.LookRotation(-lookVector);

                }

                remainingDistance -= speed * Time.deltaTime;
                yield return null;


            }


            stopCurrent();

        }

Please don’t suggest any 3rd party asset.
Thanks for help.

I don’t fully understand your question but I think you should take a look to easing functions:

hover each graph to see the
behavior

Lerp is a linear interpolation, meaning the object travels the same amount of distance each delta time.

If you want to slow down the object reaching the target, you could use a easeoutquad, for example.

You can see the math expression at the bottom

You can implement it this way (high level, didn’t test)
Lerp uses a interpolation value t between 0-1.
You can apply the easing function to t and use this t’ in the lerp function

float EaseOutQuad(float a, float b, float t)
{
t = 1.0f - (1.0f - t) * (1.0f - t);
return Math.Lerp(float a, float b, float t);
}

When you have some experience you can create your own easing functions