# Vector3.Lerp slow first and become faster

is there and alternative to vector3.Lerp that makes it slow at first and get faster, rather than the other way?

I REALIZE this is a Late-Post (years later), but NOBODY answered the man’s question!

I will apologize for the lengthy reply, but it is necessary to understand the math for “why” it works… Or you could just skip to the bottom and final code snipet

In case anyone else is Googling this exact same question, the solution is as follows:

First, we need to develop a self-iterative way for increasing value. In other words, we need to find a number which increases on its own. In this case Time.time

Let’s call this idea of starting slow and gradually moving fast as ExponentialLerp

As soon as we start the the ExponentialLerp, we need to set a timeStamp

``````float timeWeStarted = Time.time;
``````

So anytime, you want to ExponentialLerp, you must find out how much time has elapsed since we started ExponentialLerping…

``````float elapsedTime = Time.time - timeWeStarted;
float stepAmount = elapsedTime;

// FOR VECTOR3s
Vector3.Lerp (origin, destination, Mathf.MoveTowards (0f, 1f, stepAmount));
// FOR FLOATS
float variableToLerp = Mathf.MoveTowards (0f, 1f, stepAmount);
// FOR COLORS
Color colorToUse = Color.Lerp (originalColor, destinationColor, Mathf.Towards (0f, 1f, stepAmount));
``````

As you likely have guessed by now, so far every time this code is run it is additive. In other words, right now we are only moving in a positive step direction without actually going faster.

Let’s do that now!

I have a graphing calculator, but you could just rely on the famous Google to show you what it looks like. So go to google and search this “y = x^2”

Let’s pretend for a moment that X is the elapsedTime, and Y is the stepAmount. This means for each moment in time that passes, the step amount will get exponentially faster!

The code to achieve this would look like this…

``````float elapsedTime = Time.time - timeWeStarted;
float stepAmount = Mathf.Pow (elapsedTime, 2f);
// FOR VECTOR3s
Vector3.Lerp (origin, destination, Mathf.MoveTowards (0f, 1f, stepAmount));
// FOR FLOATS
float variableToLerp = Mathf.MoveTowards (0f, 1f, stepAmount);
// FOR COLORS
Color colorToUse = Color.Lerp (originalColor, destinationColor, Mathf.Towards (0f, 1f, stepAmount));
``````

As you can see, we simply define our stepAmount as taking the elapsedTime to a power of 2.

In my case, the speed at the tail end was not fast enough to end the ExponentialLerp. As such all we need to do is increase the power of 2 until it’s fast enough. Maybe a power of 10???

Again, go to google and look a the power curve by searching “y = x^10”

As you can see, now we are really going fast at the trail-end of this ExponentialLerp.

In my particular case though, I found that the time it took from from starting until it started ramping up at the end was too long!!! That flat-line in the last picture was too long…

All we have to do is take our stepAmount value (before raising it the power) and multiply it by some variable.

FINAL CODE and the ANSWER to the Man’s QUESTION

``````// This at the top of your script
public float speedOfChange = 2f;
public float exponentialModifier = 10f;
public float timeWeStarted;

// This code should be maybe in a function that you call which starts the ExponentialLerp
timeWeStarted = Time.time;

// This is somehwere in the update where you are trying to ExponentialLerp
float elapsedTime = Time.time - timeWeStarted;
float stepAmount = Mathf.Pow (elapsedTime * speedOfChange, exponentialModifier);
// FOR VECTOR3s
Vector3.Lerp (origin, destination, Mathf.MoveTowards (0f, 1f, stepAmount));
// FOR FLOATS
float variableToLerp = Mathf.MoveTowards (0f, 1f, stepAmount);
// FOR COLORS
Color colorToUse = Color.Lerp (originalColor, destinationColor, Mathf.Towards (0f, 1f, stepAmount));
``````

Lerp is not fast at first, slow later. It is linear (that’s what the ‘L’ stands for) with the delta supplied as the third parameter.

However, it is commonly misused. You might want to read the following to explain how:

To have a smoothing function that eases in, you need to provide an appropriate function as the third parameter (some sort of exponential function, perhaps)

@Lostrick I know that this is late, but you can spherically interpolate the the Vector using Vector3.Slerp. The Slerp method is more resource intensive, but it creates a smooth in and smooth out effect to both Quaternion rotations and Vector movements.