- Home /
Lerp Gameobjects..
I know this topic has been plastered all over, but it is unbaringly horrid to try and traverse through many different peoples methods to find what I am looking for.
All I am after is something the moves from A to B within a time frame thats it. no special features, no smoothing, just a basic, in and out within a time.
My current code, which obviously doesnt work (works okay. but doesnt have a 'smooth' movement, it just goes from a to b in an instant, how can I make it so the 'speed' actually is taken into accordance?
public void openBottom()
{
bottomDraw.transform.localPosition = new Vector3(bTempX, bTempY, Mathf.Lerp(closePos, openPos, speed));
bottomOpen = true;
}
public void closeBottom()
{
bottomDraw.transform.localPosition = new Vector3(bTempX, bTempY, Mathf.Lerp(openPos, closePos, speed));
bottomOpen = false;
}
Answer by Namey5 · Mar 14, 2020 at 09:36 AM
If you want something that moves linearly between A and B over a set period of time, then you need to keep track of time itself;
//Lerp start
public Vector3 start;
//Lerp end
public Vector3 end;
//Total time of the lerp in seconds
public float seconds = 2f;
private float startTime = 0f;
private bool runFunction = false;
void Update ()
{
//Start the coroutine once when the space key is pressed
if (Input.GetKeyDown (KeyCode.Space))
StartCoroutine (LerpPosition (start, end, seconds));
//Alternatively, run the function and set the current time when the mouse is clicked
if (Input.GetKeyDown (KeyCode.Mouse0) && !runFunction)
{
startTime = Time.time;
runFunction = true;
}
if (runFunction)
{
LerpPosition2 (start, end, seconds);
}
}
private bool isRunning = false;
//I'm using an enumerator to make things a bit easier, but there's also an example with a normal function below
IEnumerator LerpPosition (Vector3 a, Vector3 b, float s)
{
if (isRunning)
yield break;
isRunning = true;
float startTime = 0f;
float time = startTime;
while (time < s)
{
transform.position = Vector3.Lerp (a, b, (time - startTime) / s);
time += Time.deltaTime;
yield return null;
}
isRunning = false;
}
void LerpPosition2 (Vector3 a, Vector3 b, float s)
{
float time = (Time.time - startTime) / s;
transform.position = Vector3.Lerp (a, b, time);
if (time >= 1f)
runFunction = false;
}
Thanks so much for the answer, I had a quick read over this lastnight as I was just heading to bed, but put it into effect this morning and it works great.. Again thanks! Has this recently been modified the way this works? the only way I knew how to lerp an object was using a similar way I had shown in my code above, unless Im thinking of something different.
The way you did it is definitely the most common answer, but it isn't really correct. The idea of 'pos = Lerp (pos, target, speed * Time.deltaTime)' is basically a smoothing function - with every new frame the speed at which the current position approaches the target is halved. It's convenient because it is self contained, but really doesn't work as proper linear interpolation (which my version above is). On top of that, the way you've written it above doesn't work either because it is constant - it will always return the point that is speed% between A and B, rather than moving between them over time (which involves interpolating between the current position and a target, rather than two targets).