Making a smooth Bezier curve

Hello everyone i am making a Bezier curve with my characters position towards my mouse position. The problem is that for the first half its smooth but for the second half it becomes laggy and choppy. I tried various things such as removing the wait() and etc but it doesnt seem to help (removing the wait() makes it basically teleport to mouse).

Code

	local function QuadraticBezier(t,p0,p1,p2)
		return (1-t)^2*p0+2*(1-t)*t*p1+t^2*p2; 
	end;
	
	--- curving parts
	local Start = character.HumanoidRootPart
	local End = Instance.new("Part",workspace)
	End.Transparency = 1
	End.Anchored = true
	End.CanCollide = false
	End.CFrame = Mouse
	End.Name = player.Name .."'s End Bezier curve(FlipKick)"	

	local Curve = Instance.new("Part",workspace)
	Curve.Transparency = 1
	Curve.Anchored = true
	Curve.CanCollide = false
	Curve.CFrame = End.CFrame:Lerp(Start.CFrame,0.5) * CFrame.new(0,80,0) 
	Curve.Name = player.Name .."'s Curve Bezier curve(FlipKick)"
	
	spawn(function()
		for t = 0,1,0.01 do
			local TargetPosition = QuadraticBezier(t , character.HumanoidRootPart.Position, Curve.Position, End.Position);
			character.HumanoidRootPart.Position = TargetPosition;
			game:GetService("RunService").Heartbeat:Wait()
		end;
	end)

https://gyazo.com/fedbc4ffcacaf6aaaece8a245d0e77f4

3 Likes

i think the issue isn’t the bezier calculations. but instead the loop. If the calculations in QuadraticBezier are efficient. it shouldn’t make any problems there. Positioning the parts also shouldn’t be the issue.
Therefore the issue of things becoming choppy must lay in the for loop.

There are multiple ways to make movement smooth.
1: Using runservice, hearthbeat or Bind To RenderStepped.
2: using roblox’s physics. (Wich suck if you want to have maximum control over movement)
3: Tweening
If you want stuff to move smoothly from the client i would reccomend using renderstepped to have maximum control and so you don’t have to do weird unanchoring and welding stuff like with tweening models.
If it is strictly serversided, you cannot use the runservice and thus have to use Tweening.
If you are super lazy and don’t want to do maths, roblox’s physics wil still be there for you.
quick tip: remove prints and warns from loops when you want it to run at max efficiency. these prints and warns in loops often seem to get the framerate very low on my PC.

1 Like

Hmm, I’m guessing what @/spacebadboy123 replied is correct as I see no other explanation, but I have a few suggestions for your code:

You can skip all of this by just doing this since CFrame has a Position property:

local End = Mouse
local Curve = End:Lerp(Start.CFrame,0.5) * CFrame.new(0,80,0) 

I have also heard that using coroutine.wrap() works better than spawn()
Hope you find the solution!

1 Like

I have heard of it, but it seems hard to use and sorry for late responce guys ima try it now

1 Like

Im having the same problem, with this new tween code

spawn(function()
	for t = 0,1,0.01 do
		local TargetPosition = QuadraticBezier(t , character.HumanoidRootPart.Position, Curve.Position, End.Position);
		character.HumanoidRootPart.Position = TargetPosition;
		
		local Bgoal = {}
		Bgoal.Position = TargetPosition
		local BInfo = TweenInfo.new(1)
		local Btween = TweenService:Create(character.HumanoidRootPart,BInfo,Bgoal)
		
		
		game:GetService("RunService").Heartbeat:Wait()
	end;
end)

You’re changing the start point of the Bézier curve every iteration, p0, which leads to a constantly changing curve, resulting in irregular motion. The start point should be kept fixed, i.e: at the humanoidrootpart’s initial position before it started following a curve:

local initial = character.HumanoidRootPart.Position
local End = Mouse.Position -- I assume this is the mouse's position
local p1 = initial:Lerp(End.Position, 0.5) + Vector3.new(0, 80, 0)

for i = 0, 1, 0.01 do
    character.HumanoidRootPart.Position = QuadraticBezier(t, initial, p1, End)
    game:GetService("RunService").Heartbeat:Wait()
end

Unless you need them for visualising, you don’t need to create parts to be used for the bézier curve

5 Likes