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

Why isn't this part moving through out these points?

Why isn't this part moving? I'm using a Bezier function i found in the ROBLOX wiki and I kinda get it but I'm not sure why it's not moving through out the 3 points without any errors?

function lerp(a, b, c)
	return a + (b - a) * c;
end;
 
function cubicBezier(t, p0, p1, p2, p3)
	local l1 = lerp(p0, p1, t);
	local l2 = lerp(p1, p2, t);
	local l3 = lerp(p2, p3, t);
	local a = lerp(l1, l2, t);
	local b = lerp(l2, l3, t);
	local cubic = lerp(a, b, t);
	return cubic;
end;


local StartPoint = Instance.new('Part')
StartPoint.Anchored = true
StartPoint.Size = Vector3.new(1,1,1)
StartPoint.Parent = game.Workspace
StartPoint.CFrame = game.Workspace.Model.A.CFrame

local X = CFrame.new(cubicBezier(3, StartPoint.CFrame.p, game.Workspace.Model.A.CFrame.p, game.Workspace.Model.B.CFrame.p, game.Workspace.Model.C.CFrame.p))

StartPoint.CFrame = StartPoint.CFrame:lerp(X, 2)

1 answer

✓ Accepted answer

You're using the function wrong, the t parameter is the point in time along the lerp it is as alpha (i.e. between 0 and 1 / decimal percentage). Here is how it should be used:

function lerp(a, b, c)
    return a + (b - a) * c;
end;

function cubicBezier(t, p0, p1, p2, p3)
    local l1 = lerp(p0, p1, t);
    local l2 = lerp(p1, p2, t);
    local l3 = lerp(p2, p3, t);
    local a = lerp(l1, l2, t);
    local b = lerp(l2, l3, t);
    local cubic = lerp(a, b, t);
    return cubic;
end;

local StartPoint = Instance.new('Part')
StartPoint.Anchored = true
StartPoint.Size = Vector3.new(1,1,1)
StartPoint.Parent = game.Workspace
StartPoint.CFrame = game.Workspace.Model.A.CFrame

for i = 0, 10, 0.5 do
	print(i);
	local X = CFrame.new(cubicBezier(i/10, StartPoint.CFrame.p, game.Workspace.Model.A.CFrame.p, game.Workspace.Model.B.CFrame.p, game.Workspace.Model.C.CFrame.p))
	StartPoint.CFrame = X
	wait();
end

1 comment

0

Thank you so much!

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