Make part face bezier curve path

I have this script that makes a part move along a bezier curve, however I want it to be facing the path it’s going while it’s moving. Best way I can explain it is with the green line in this gif.

function quadBezier(t, p0, p1, p2)
	return (1 - t)^2 * p0 + 2 * (1 - t) * t * p1 + t^2 * p2
end

local pos0 = Vector3.new(-600, 165, 0)
local pos1 = Vector3.new(0, 105, 0)
local pos2 = Vector3.new(600, 165, 0)

local part = script.Parent

part.Position = pos0

for t = 0, 1, 0.01 do
	local position = quadBezier(t , pos0, pos1, pos2);
	part.Position = position
	game:GetService("RunService").Heartbeat:Wait()
end
2 Likes

you can do the bezier twice, and use cframe.lookat / cframe.new except make the t value an extra step

function quadBezier(t, p0, p1, p2)
	return (1 - t)^2 * p0 + 2 * (1 - t) * t * p1 + t^2 * p2
end

local pos0 = Vector3.new(-600, 165, 0)
local pos1 = Vector3.new(0, 105, 0)
local pos2 = Vector3.new(600, 165, 0)

local part = script.Parent

part.Position = pos0

for t = 0, 1, 0.01 do
	local CF = CFrame.new(quadBezier(t , pos0, pos1, pos2), quadBezier(t + 0.01 , pos0, pos1, pos2));
	part.CFrame = CF
	game:GetService("RunService").Heartbeat:Wait()
end

but your position will have to become a cframe unless you make another variable for it

4 Likes