- Home /
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 it is 7 years later and you probably found a solution by now. But, for anyone else googling this problem...I have attached the solution below.
Answer by tanoshimi · Jan 25, 2015 at 07:06 AM
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)
thanks, i'm always having trouble using lerp, i didn't realize it's like this
Please note that in the first article you linked, the author writes percentage > 1.0f. This is called a ratio.
Also, he overcomplicates the lerping mechanic.
You didn't answer the guys question. You just told him how lerp works. I have attached code below which actually answers his question.
Answer by karma0413 · Feb 04 at 01:14 PM
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));
Answer by jojo_game_studios · Jul 27, 2019 at 04:28 PM
@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.
Huh? No it does not. Slerp is also a linear interpolation but for vectors and ensures a correct spherical interpolation. It does not ease in any way. Where did you get that form? Have you actually read the documentation? Slerp only makes sense for rotations or direction vectors. Slerp will ensure that the vector keeps its length while interpolating.
You may also want to read the wikipedia page on slerp:
It refers to constant-speed motion along a unit-radius great circle arc, given the ends and an interpolation parameter between 0 and 1.
If you want easing you can simply use one of the classic hermite spline polynomials and feed your t value through that function before using it in Lerp / Slerp.
@Bunny83 Spherically Interpolating does create an ease in/ease out effect because the Vector or Quaternion does not change equal amounts as compared to linear interpolating. Spherically interpolating changes slower at low and high percentages and faster at middle percentages.
No, it does not. You don't seem to understand what spherical means, It moves at constant angular speed. The motion represents an arc ins$$anonymous$$d of a straight line, that's the only difference between Lerp and Slerp. See this video for a visualization. Slerp will always rotate along the shortest arc.
edit
Try attaching this script to a sphere in your scene. It will move the object from (0,5,0) to (5,0,0) and back. $$anonymous$$eep in $$anonymous$$d that Slerp is for direction vectors and not positions as the arc will always be around (0,0,0). This script will actually measure the speed at which the object moves along the arc and it is constant throughout the movement.
using UnityEngine;
public class SlerpTest : $$anonymous$$onoBehaviour
{
Vector3 old;
float speed;
float t;
void Update ()
{
old = transform.position;
t = $$anonymous$$athf.PingPong(Time.time * 0.5f, 1f);
transform.position = 5f*Vector3.Slerp(Vector3.up, Vector3.right, t);
Vector3 diff = transform.position - old;
speed = diff.magnitude / Time.deltaTime;
}
private void OnGUI()
{
GUILayout.Label("Current speed:" + speed);
GUILayout.Label("Current t:" + t);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613181444im_/https://answers.unity.com/themes/thub/images/avi.jpg)