What is the :lerp function?

what is lerp? I tried watching tutorials but I still don’t get it I looked on the dev hub and i am seeing it a lot and I really want to know what is lerp? sorry for my grammar

20 Likes

lerp stands for linear interpolation. It uses a linear function from A to B, where alpha applied is the placement in the linear function. Alpha must be between 0 to 1.

Skärmavbild 2021-03-30 kl. 15.47.18

31 Likes

I do not know what linear functions are

3 Likes

Simply in math:
y = kx + m

A linear function is just a straight line that is defined by the equation above. Why don’t you play around with graphs for a little?

6 Likes

what are k and m ???

1 Like

Those are variables by any number. The numbers are determining the function’s graph in appearance. It doesn’t change the curve, but it does change its offset and the tilt.

8 Likes

so i can decide what k and m are

Yes, but in this case of linear interpolation, the A and B as I have previously mentioned are two different values. If you use linear interpolation, they will shift closer to A or B, linearly. The k is always 1 and m is 0. The function’s limit is 1 ≥ x ≥ 0.

7 Likes

In simple terms, lerp is used to get a point between two other points. For example say we had part1 and part2, we can position apart halfway between them using this.

local Part1 = --
local Part2 = --

local Part3 = Instance.new("Part", workspace)
Part3.CFrame = Part1.CFrame:Lerp(Part2.CFrame,0.5)

The second parameter (in this case 0.5, should be a number between 0 and 1. It is basically saying what percentage we want to lerp with.)

62 Likes

so then what is the point of having k and m ?

That’s for explaining linear function, those doesn’t have to contain anything because that’s all theory. Explanation provided by @XdJackyboiiXd21 is a practical example of that.

2 Likes

so how dose this math help me understand what :lerp is?

Understanding that concept makes it easier to comprehend on how the function actually works. You can go around Studio and test that line around for fun. :slight_smile:

Although it’s math, not a lot of calculating is required to understand its functionality.

5 Likes

so lerp is is a linear function y = kx + m

1 Like

Not exactly, it is linear interpolation. The difference is that the interpolation is an input of an x that determines the y value. The maximum y and the minimum y values defined where x returns a value between them.

Therefore, linear interpolation, or lerp, uses a linear function that returns a value on input of an alpha, which is always between the two values.

5 Likes

so how dose this math connect to the lerp function how dose the parameters effect this

1 Like

Lerp is interpolation, it can be used in CFrames, Vector3s and Vector2s (I think)

In roblox I believe lerp means approximating a location from the target given the number you specified (0 - 1)

1 Like

Here is another example of using Lerp. We loop through, starting at 0.1, and ending at 0.9, increasing by 0.1 each time. Then I create a new part each loop and position it that much between the two parts.

local Part1 = game.Workspace:WaitForChild("Part1")
local Part2 = game.Workspace:WaitForChild("Part2")

for i = 0.1, 0.9, 0.1 do
	local Part = Instance.new("Part", workspace)
	Part.Anchored=  true
	Part.CFrame = Part1.CFrame:Lerp(Part2.CFrame, i)
	local Suface = script.SurfaceGui:Clone()
	Suface.TextLabel.Text = i
	Suface.Parent = Part
end

Result:

As you can see, lerping by 0.1 creates a part very close to “Part1”, lerping by 0.5 creates a part in between, and lerping 0.9 creates a part very close to Part2

47 Likes

Say

local p1 = Vector3.new(0,10,0)

and

local p2 = Vector3.new(0,0,0)

If you were to lerp these using :Lerp(), you would do

local l1 = p1:Lerp(p2,0.5)

this would mean l1 = Vector3.new(0,5,0) becuase 0.5 would be half. Half way bettween p1 and p2
1 would be Vector3.new(0,0,0) and 0 would be Vector3.new(0,10,0). And so on.

8 Likes

can you use :lerp for things other then CFrame like Color3

2 Likes