Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the use of "lerp" in humanoid movement?

if (target.Torso.Position - targetStart).magnitude < 0.5 then
					local pos = previous:lerp(point, .5)
					local moveDir = ((pos - zombie.Torso.Position).unit * 2)
					goToPos(previous:lerp(point, 0.5))
					previous = point
				end

`What do you use lerp for? I know that is linear interpretation, but I don't really know how to use it? Do you really it?

2 answers

✓ Accepted answer

I can't exactly tell you, since your goToPos function is not defined in your code snippet, but in general, lerp is used to define tweening.

Teleportation movement, i.e. CFrame manipulation or Model:MoveTo() requires tweening using lerp (CFrame:lerp() and Vector3:lerp(), respectively.) (Yes, they are different) in order to have this movement occur over time, rather than instantly.

That, most basically, looks like this:

local part = Instance.new("Part", workspace)

local start = CFrame.new()*CFrame.Angles(math.random(), 0, math.random())
local goal = CFrame.new(15,15,15)*CFrame.Angles(0, math.random(), 0)

local heartbeat = game:GetService("RunService").Heartbeat --the fastest wait available on the Server.
--On the client, RenderStepped is "faster" is some cases, but heartbeat is still preferred for non-visual 'tween's, or for less important time-delayed motion.

local st = tick()
local dur = 5
while true do
	local et = tick() - st
	if et > dur then et = dur end --prevents "overshooting"

	part.CFrame = start:lerp(goal, et/dur)

	if et >= dur then break end
	heartbeat:wait()
end

In terms of Humanoid movement, doing this is redundant as setting the Humanoid's WalkToPoint does not teleport the Humanoid's Model. Without the rest of the code, it's impossible to determine why the original scripter used lerp in this case.

Lerp, short for linear interpolation, is a mathematical technique for producing intermediate states between a start and an endpoint. It is related to tweening.

For a single variable, we can define

function lerp(a, b, t)
    return a * (1-t) + (b*t)
end

By definition, for all implementations of lerp:

lerp(a, b, 0) == a
lerp(a, b, 1) == b
lerp(a, b, x) == lerp(b, a, 1-x)

Two roblox types have a lerp method:

Vector3.Lerp — interpolates all three components as above
CFrame.lerp — uses quaternion slerp to interpolate orientations, and linear interpolation to interpolate positions
For which the previous identities still hold:

vecA:Lerp(vecB, 0) == vecA
vecA:Lerp(vecB, 1) == vecB
 
cfA:lerp(cfB, 0) == cfA
cfA:lerp(cfB, 1) == cfB

2 comments

1

Sorry, but I can't understand what is the use of it. You are just giving me the "How" to use it, but I need the "What" for it's use?

0

I'm not saying your equation is wrong, but there's another one that also works. a + (b - a)*t

This discussion is preserved as it was. New answers, comments, and votes are closed.