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

How to smoothly move along bezier curve?

So I'm making a game that requires an object to travel along a bezier curve. I have it generating using CrazyMan32's Bezier Curve Module, but now I need to smoothly let a part travel along it. What I have right now:

local points = {startPoint, midPoint, endPoint} --Generate Points
	
	local bezier = Bezier.new(unpack(points)) -- Create Bezier
	for i = 0, 1, 0.001 do
		local point = bezier:Get(i)
		workspace.Mover.CFrame = CFrame.new(point)
		wait()
	end

Is super jittery. Is there any way to smoothly lerp between points?

2 answers

I'd probably use an event. There are many that Roblox provides you. The most prominent one being RenderStepped. Here's an explanation of each of the events. (Note that they are all members of the RunService class.)

  • RenderStepped: Fires before a frame is rendered.
  • Stepped: Fires before physics are simulated for a frame.
  • Heartbeat: Fires after a frame is rendered.

In your case, I'd use Stepped since your bezier involves objects. You can simply connect to this event and get the delta time.

local runService = game:GetService("RunService")

local duration = 1 -- amount of seconds for your bezier to take effect
local startTime = tick()
runSerice.Stepped:Connect(function()
    local deltaTime = tick() - startTime
    local point = bezier:Get(deltaTime / duration)
end)

3 comments

0

This was helpful. However, I'm not talking about lag. I'm taking about using a leap or tweeting function, I just don't know how to go about that.

0

Lol autocorrect, a lerp or tweening function

0

aren't you already using lerp by interpolationing through each node?

While there is a way to make a part travel along a curve using tweens, I wouldn't really recommend using this method since you're very limited on how you can configure the curve.

To make a part move along a curve, we have to first create a tween that will make it move on the X/Z axis and another separate tween to make it move in a curve. Since we can't have multiple tweens edit a single property at the same time, we'll have to create a Mesh object and have the other tween affect the offset property. This way the tween won't overwrite each other.

local TweenService = game:GetService('TweenService')

local Folder = script.Parent
local Part = Folder.Point
local p1    = Folder.Start
local p2    = Folder.End

local curveHeight = 20
local t = 1 -- time it takes to complete

Part.Position = p1.Position

local xzAxis = TweenService:Create(
	Part,
	TweenInfo.new(t,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,true),
	{Position=p2.Position}
)
local yAxis = TweenService:Create(
	Part.Mesh,
	TweenInfo.new(t/2,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,1,true),
	{Offset=Vector3.new(0,curveHeight,0)}
)

while true do
	xzAxis:Play()
	yAxis:Play()
	wait(t + 1)
end

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