- 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
Follow this Question
Related Questions
How the heck does Mathf.Lerp work? 2 Answers
How to move character a certain amount? 1 Answer
Lerping Issue 1 Answer
Vector3.Lerp move weapon up to players face 2 Answers
Add a Vector3 to the rotation of an obj and have it behave with logic! 0 Answers