x


Vector2.Lerp is not supported. Any Alternatives?

Is there an easy way to smoothen my 2D Vector? Currently, only Vector3.Lerp works...

more ▼

asked Jan 26 '10 at 11:04 AM

Adriaan gravatar image

Adriaan
65 2 2 5

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

2 answers: sort voted first

Sure. In essence, a "Lerp" is nothing more than:

return from + (to-from) * t;

In Unity, the Lerp functions also clamp 't', ensuring the range doesn't go below zero, or above one. So to implement a "Lerp" function for Vector2's yourself, it can just be a 2-line function:

public static Vector2 Lerp(Vector2 from, Vector2 to, float t)
{
    t = Mathf.Clamp01(t);
    return new Vector2(from.x + ((to.x - from.x) * t), from.y + ((to.y - from.y) * t));
}

Although it does seem like a bit of an oversight that it wasn't included in the Vector2 class.

more ▼

answered Jan 26 '10 at 11:11 AM

duck gravatar image

duck ♦♦
41k 92 148 415

I must agree with Unity's oversight... Oh well, Thanks a lot!

Jan 26 '10 at 11:15 AM Adriaan
(comments are locked)
10|3000 characters needed characters left

For the record, Vector2.Lerp does exist in Unity 3.

more ▼

answered Nov 06 '12 at 07:39 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

(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:

x5085
x195

asked: Jan 26 '10 at 11:04 AM

Seen: 1445 times

Last Updated: Nov 06 '12 at 07:39 PM