x


Vector3.Lerp works outside of Update()

Does Vector3.Lerp() for transforming position work outside of update?

more ▼

asked May 07 '11 at 09:03 AM

ina gravatar image

ina
3.3k 492 551 602

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

It's a plain old function, so it 'works' anywhere - in that it will correctly calculate a value that is the linear interpolation between the start and end values. I use it, for example, in my AI code to compute how 'audible' a sound is based on how far away it is.

However, most of the time, when people are using Lerp(), they're trying to achieve smooth movement from point A to point B. To get smooth movement, you need to recompute the position very frequently - every frame, if possible.

Update() is called every frame, so you could call it there. You could also write a coroutine that does work every frame and calls it:

IEnumerator MoveObject(Vector3 source, Vector3 target, float overTime)
{
    float startTime = Time.time;
    while(Time.time < startTime + overTime)
    {
        transform.position = Vector3.Lerp(source, target, (Time.time - startTime)/overTime);
        yield return null;
    }
    transform.position = target;
}

The advantage of doing it in a coroutine is that when the object isn't moving, the coroutine isn't running, which can save Unity a bit of time; by comparison, if you put the code in Update, Unity will always call Update, even if you end up not doing anything for the frame.

Also, with the coroutine approach, you can (if you wish) trade smoothness for framerate by yielding a WaitForSeconds() object instead of null. The longer you WaitForSeconds(), the jerkier the movement will be, but the less frequently Unity will have to do the computation.

more ▼

answered May 07 '11 at 11:50 AM

superpig gravatar image

superpig
557 8 9 24

(comments are locked)
10|3000 characters needed characters left

It works wherever you want it to, it's a simple mathematical function, min + (max - min) * t

You can generally use it well in coroutines, where you have a fixed start and end, and increment the t value from 0 to 1

Quick example for a coroutine:

IEnumerator MoveTo(Vector3 position, float time)
{
    Vector3 start = transform.position;
    Vector3 end = position;
    float t = 0;

    while(t < 1)
    {
        yield return null;
        t += Time.deltaTime / time;
        transform.position = Vector3.Lerp(start, end, t);
    }
    transform.position = end;
}
more ▼

answered May 07 '11 at 10:07 AM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

can you give an example of how it might work with a coroutine?

May 07 '11 at 10:24 AM ina

done - i haven't checked it, but looks reasonable enough at second glance

May 07 '11 at 09:39 PM Mike 3
(comments are locked)
10|3000 characters needed characters left

No, it is meant to be used within the Update() method.

more ▼

answered May 07 '11 at 09:36 AM

Meltdown gravatar image

Meltdown
5.6k 18 25 49

so lerp does not work at all in a function outside? what if update calls a function outside with lerp in it

May 07 '11 at 09:47 AM ina

It is not meant to be used in Update. In fact it's generally better outside Update. http://answers.unity3d.com/questions/6949/can-someone-explain-how-using-time-deltatime-as-t-in-a-lerp-actually-works/6950#6950

May 07 '11 at 10:29 AM Eric5h5

ina, sure, Update() can call any external method. But any method that Update() calls will still run the context of the Update() method execution. Eric thanks for the informative post, interesting stuff!

May 07 '11 at 11:18 AM Meltdown

Eric, I read your link but couldn't see from your answer why it's bad to call Lerp() inside Update(). Could you elaborate?

May 07 '11 at 11:44 AM superpig

people tend to misuse it a lot, lerping from the current position to the target position, which ends up with a weird almost exponential decay movement, which is dependent on frame rate.

May 07 '11 at 09:45 PM Mike 3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1282
x888
x580
x496
x265

asked: May 07 '11 at 09:03 AM

Seen: 3264 times

Last Updated: May 07 '11 at 09:03 AM