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