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?
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