- Home /

# Can someone help me to understand what vector3.lerp does and can be used for

ive seen a tutoiral on youtube on a smooth gun movement script what the script does is every time i turn left of right the gun is alittle slower then catches up to the camera to make ait look more realistic but i want to know how this actually works to maybe implement it somewhere else

**Answer** by fafase
·
Oct 28, 2012 at 08:05 PM

```
transform.position = Vector3.Lerp(start, to, t);
```

It is an interoplation between start and to by t.

In human word, it means you take the start(transform.position) and you look at to (target.position). Then you move the object by the amount of t. If you give t= 1 it moves suddenly from start to to. If you give 0.5 it will move 1/2 each frame.

Often it is used like this:

```
function Update(){
transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime);
}
```

If there would be 10 m between transform(0) and target(10) and deltaTime is 0.2 (20%) then you get:

20% of target-transform (10-0) = 2m so transform = 2

20% of target-transform (10-2) = 1.6m so transform = 3.6

20% of target-transform (10-3.6) = 1.28m so transform = 4.88

And so on

Hopefully you get the idea.

i understand everything i think except whats t? and how to i change it?

t is the percentage you want to interpolate. So you either give it a value between >0 and 1 (0 would mean no movement so anything greater than 0). 1 is full movement at once. By giving any value in between you interpolate each frame to the next value. Note that you actually never reach the target since you always move of a percentage of the distance between. Just like limits in algebra, you get so close but never reach.

What do you mean? you give it a value that is it...between >0 and 1 (0 would mean no movem...hold on I said that already. I gave Time.DeltaTime because it is a small value but give what you want between >0 and 1 (0 would mean Grrrrhhhh.

**Answer** by Xroft666
·
Oct 28, 2012 at 08:06 PM

Lerp - is short version of Linear Interpolation

What it does: it takes 2 vectors and an float variable from 0f to 1f.

If you give 0 then the function returns the first given vector, if you give 1f it returns the second vector. If you give for instance 0.3f, the function returns (interpolated \ crossfaded \ blended) vector of those two, where are 0.3 parts of the first one and 0.7 parts of the other.

Example: Lerp( (10,0,0), (0,10,10), 0.5f ) = 5,5,0

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Add a Vector3 to the rotation of an obj and have it behave with logic! 0 Answers

Lerping Issue 1 Answer

why one works, the other does not 2 Answers

How do I change this code from a new Vector3 to Vector3.Lerp? 1 Answer

MoveTowards won't translate on the y axis and lerp leaves character jittering in mid-air 0 Answers