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