Constant speed while lerping despite changed distance

Bascially im trying to make a path for my enemies in Tower Defense game, but problem is that when i lerp value is between 0-1 which essentialy comes with different speed on distances. So im looking for solution how i would make constant speed despite changed distance.

It can be even different script or lerp implementation, because in my opinion using heartbeat is not a best solution

local p: {Part} = {}
for i, k in pairs(workspace.Points:GetChildren()) do
	table.insert(p, k)
end

local e = workspace.Enemy

local lerpT = 1
local alpha = 0

task.wait()
local dis = (p[1].Position - p[2].Position).Magnitude
game["Run Service"].Heartbeat:Connect(function(dt)
	alpha += dt/(lerpT * dis)
	print(alpha)
	e.Position = p[1].Position:Lerp(p[2].Position, math.min(1, alpha))
	
	if alpha >= 1 then
		alpha = 0
	end
end)

1 Like