0
\$\begingroup\$

In Unity3D (all versions, I believe), there is a static function for the Vector3D class called "lerp". It interpolates a point between two points a point, based off a provided percentage related to the desired distance between the two.

Alternatively, I could just get the direction I want to go as a point, subtract it from my current position, Normalize() the resulting vector, then multiply that times a speed value.

So:

  • Is one way faster than another?

and

  • Is there a third better/faster/easier/more efficient way I don't know about?

Thanks.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ In your first example you mention a "desired distance", yet in the second one you use a "speed value". Can you maybe clarify? Anyway, lerp() is only one scalar-vector multiplication and a few additions; it should be extremely fast. \$\endgroup\$ Feb 11, 2013 at 16:15
  • 1
    \$\begingroup\$ Honestly, unless you're doing this a LOT (and I really mean a LOT) of times per frame, it's just going to be down in the noise on any performance graph. \$\endgroup\$ Feb 11, 2013 at 16:19
  • \$\begingroup\$ For the sake of consistency, lets say we still using rates, but recorded differently. So the first one would use 5%/sec, while the other would be 5units/sec. True to Unity form, these would get Time.deltaTime 'd . \$\endgroup\$
    – Kirbinator
    Feb 11, 2013 at 16:20
  • 1
    \$\begingroup\$ As with all performance questions, benchmark. It would be trivial to write a little script that does this, crank up the data to appropriate levels, and then test it on whatever target platform you're using (assuming you have Unity pro with the profiler). \$\endgroup\$
    – Tetrad
    Feb 11, 2013 at 17:49
  • 1
    \$\begingroup\$ That being said I highly doubt either approach would be slowing you down by any measurable amount, why are you asking if one is better? Just use whichever one is the cleanest/most maintainable until you reach a point where you have to optimize something. \$\endgroup\$
    – Tetrad
    Feb 11, 2013 at 17:50

3 Answers 3

4
\$\begingroup\$

Velocity and lerping are for different purposes. Lerping is great when you know you're going to end at the target in a given time; it's good for many UI animations, for example.

Use velocity when what you have is the speed of movement and the intent to get to a target at that constant speed. Using the velocity let's you smoothly change directions mid-course, slow down or speed up, etc. I had a place I was lerping vectors and changed to steering (velocty/force) because then I could make objects twist and "dodge" each other if they crossed paths.

In general, use lerp for static animations, use velocity (or full physics) for in-game character/object movement.

\$\endgroup\$
1
  • \$\begingroup\$ Ah, I see, they are more for framing movement in different ways. \$\endgroup\$
    – Kirbinator
    Feb 12, 2013 at 19:08
1
\$\begingroup\$

In the two situations you give, the former will always go whatever percent you enter in regardless of distance and in the second you'll always go the distance (speed * time) you enter in regardless of end goal distance.

Calculating a speed and multiplying by the normalized vector will give you a consistent speed of travel toward the destination at the cost of normalizing (which uses sqrt). This is a pretty straightforward way of doing what you want and has the advantage of making it easier to store current momentum for if the target position changes.

Using Lerp will allow you to travel a percent distance between the two points very easily, but it will not only be dependent on the end point, it will also vary behavior on framerate if you do so repeatedly while updating the source position and leaving the destination. This is not an issue if you only update the percent value over time like you mention in your comment while leaving the start and end points the same. That being said you can do an extremely cheap elastic band effect by giving Lerp a fixed percentage and moving the start point to the current.

Overall they are two different tools with different purposes that can be used to the same effect (as per your example). While Lerp is going to be the faster between the two, the actual effect probably won't be felt unless you're doing it a lot (for instance, if you were doing it for every particle in a very busy particle system). To see if the speed difference matters for your purposes: try it, measure, then see if it's enough actively concern you.

As to the last part of your question, take a look at Vector3.MoveTowards. I'm pretty sure it's doing basically what you describe in your first example behind the scenes, but it's set up in a fashion similar to Lerp with the third piece being a distance instead of a percentage.

\$\endgroup\$
1
  • \$\begingroup\$ MoveTowards is something I've been looking for, thanks! \$\endgroup\$
    – Kirbinator
    Feb 12, 2013 at 19:15
0
\$\begingroup\$

For velocity, you probably need directional vector (normalization). With lerp, velocity would depend on distance between two points (maybe you want that).

Normalize may be slower because it performs sqrt.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .