Need help making Part follow bezier curve

Hello everyone!

I am trying to work on a system so that a part can follow a bezier curve.
Here is my current code, it simply makes the part go to a certain location and stay there.

local p0 = workspace.P0.Position
local p1 = workspace.P1.Position
local p2 = workspace.P2.Position

local mark = workspace.Part

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

for i = 1, 100 do
	wait(0.05)
	
	mark.Position = Vector3.new(quadBezier(i, p0, p1, p2), quadBezier(i, p0, p1, p2), quadBezier(i, p0, p1, p2))
end

Any help would be appreciated. To be clear, I am looking for a 3-point bezier curve so that a part could go on one axis (flat, not complex bezier curves that go up and down) in a 3D space.

The bezier function returns a Vector3 value, so you can just do

part.Position = quadBezier(i,p0,p1,p2)

Whether or not it goes up and down is determined by if p0, p1 and p2 go up or down

2 Likes

For some reason, this doesn’t work and makes the part go far away?

Edit:

Forgot to divide my increment i by 100, now it works, marking you as answer now! Thanks!

1 Like

I know this is very old but I didn’t want to make another post. Would anyone know how to do this for a circular path/part?

You can’t make a perfect circle with Bézier curves but you can do an approximation by linking four cubic Bézier curves (one for each quadrant). Edit: specified that it should be cubic Bézier curves.

It would be easier to just use trigonometry plus you get a perfect circle that way.

Like this:

-- `t` is the time variable from 0 to 1 (like a percentage from 0% to 100%)
local function getCirclePos(centre: Vector3, radius: number, t: number) : Vector3
    local fullRotation = 2 * math.pi
    local offset = Vector3.new(
        math.cos(t * fullRotation),
        0,
        math.sin(t * fullRotation)
    )
    return centre + offset*radius
end

You can optionally extend this function to include any arbitrary rotation to the circle:

-- Draws a circle relative to `reference`.
local function getCirclePos(reference: CFrame, radius: number, t: number) : Vector3
    local fullRotation = 2 * math.pi
    local offset = Vector3.new(
        math.cos(t * fullRotation),
        0,
        math.sin(t * fullRotation)
    )

    local pointOnCircle = offset * radius -- vector in object space of `reference`
    return reference * pointOnCircle -- convert vector to world space
end
1 Like