- Home /
Computation of Vector3.Lerp, does it check for change?
A simple question. If I have objects that use
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
in void Update(). Would I need to check if the current position of the object is equal to the destination to stop the execution of the interpolation, or does the Vector3.Lerp already handle this?
EDIT: I have the function working as intended, I am just wondering if I should make sure that it isn't called when it isn't needed.
Answer by Memige · Nov 12, 2012 at 11:09 PM
Lerp will only give an interpolation ranging from the start to the finish based on that third parameter (0 being the start, 1 being the finish) so it will never go past the end point. However, as a note, since Time.time is seconds since the application started, it is likely over 1, which is getting capped, and instantly snapping your object to the end position. Instead try a variable starting at 0 and incrementing by Time.deltaTime every frame. this should give you a smooth movement from one to the other.
--EDIT--
To clearly answer the question: No Lerp does not stop executing when you reach your destination. You will want to put in logic to check for reaching the destination, and skipping the lerp call when it is no longer needed.
This is not what I am looking for. I know the functions third parameter has to within the range 0 to 1. What I am asking for is, when we reach passed 1, does it stop calculations? or how does Lerp handle it. Would it be clever of me to put an ifstructure to check if end destination has been reached, or would that be 'doubling the effort'?. I am currently just calculating a value ins$$anonymous$$d of Time.time, so there is no problem there. The interpolation is working fine.
void Update () {
LerpVal += Time.deltaTime;
if(isShow == false) LerpHide();
else LerpShow();
if(LerpVal > 1) LerpVal = 1;
}
private void LerpShow()
{
transform.position = Vector3.Lerp(hiddenPos, shownPos, LerpVal*0.9f);
}
Ok, my apologies. I do not know if there are optimizations inside the function to skip the process if the third param is 0 or 1, but the function does still execute, so yes, throwing in a check to stop calling it when you reach the destination would help with efficiency.
This, or get a grip of coroutines as Erich5h5 suggested, and use it to control if it is called or not :) Thanks for the help
Answer by Eric5h5 · Nov 13, 2012 at 12:41 AM
You should generally not use Update if you want something to happen only some of the time, since Update always runs every frame (and has overhead each time it's called). Use a coroutine instead, that way you can make it stop running when you're done.
I am not at all familiar with the use of coroutines I'm afraid. But it seems like an important aspect of Unity. So I will start researching it, and try to get my head around it.
Answer by Owen-Reynolds · Nov 13, 2012 at 12:59 AM
Lerp is just a simple math function. There's pretty much one way to write it, and you can decide for yourself when to skip it:
Vector3 Lerp(Vector3 P1, Vector3 P2, float pct) {
if(pct<=0) return P1;
if(pct>=1) return P2;
return P1 + (P2-P1)*pct;
}
1up for the 'inside'-info. Additionally; I was also able to use this in another scenario where I only needed to interpolate a single value from one to another.