Need help with Bezier Curves on my Tower Defense game

Hello! I am currently creating a Tower Defense game. I wanted to make smooth curves for the zombies, and saw that the name for the curve in question was called “Bezier Curves”. However, creating them looks very complicated for me since I am using AlignOrientation and AlignPosition. How would I make it so that the curve works for these two objects and would it even be possible on them? Here is the code in mind:

if (HumanoidRootPart:WaitForChild("Node").WorldPosition - Path:WaitForChild(_CurrentPath.Value).Position).magnitude < 0.5 then
	  _CurrentPath.Value = _CurrentPath.Value + 1
  else
	  HumanoidRootPart:WaitForChild("AlignPosition").Attachment1 = Path:WaitForChild(_CurrentPath.Value):WaitForChild("Node")
	  HumanoidRootPart:WaitForChild("AlignPosition").MaxVelocity = _Speed.Value
	  HumanoidRootPart:WaitForChild("AlignOrientation").Attachment1 = Path:WaitForChild(_CurrentPath.Value):WaitForChild("Node")
end

1 Like

This is the solution of your problem:

function lerp(a, b, t)

	return a + (b - a) * t

end


function quadraticBezier(t, p0, p1, p2)

	local l1 = lerp(p0, p1, t)

	local l2 = lerp(p1, p2, t)

	local quad = lerp(l1, l2, t)

	return quad

end
function positionBezier(t:number,xyz0:Vector3,xyz1:Vector3,xyz2:Vector3)
	return Vector3.new(quadraticBezier(t,xyz0.X,xyz1.X,xyz2.X),quadraticBezier(t,xyz0.Y,xyz1.Y,xyz2.Y),quadraticBezier(t,xyz0.Z,xyz1.Z,xyz2.Z))
end

--EXAMPLE FOR THE TEST
local firstPosition = Vector3.new(10.25, 0.5, 18.1)
local middlePosition = Vector3.new(1.5, 0.5, 22.65)
local endPosition = Vector3.new(-8.4, 0.5, 7.05)
local oldPos = nil
for i=0,1,.01 do -- i is the Time (must be between 0 and 1)
	local part = Instance.new("Part",game.Workspace)
	part.Position = positionBezier(i,firstPosition,middlePosition,endPosition)
	part.Anchored = true
	part.CanCollide = false
	part.Transparency = 0.5
	if oldPos ~= nil then part.CFrame = CFrame.lookAt(part.Position,oldPos) end
	oldPos = part.Position
end

Result:

2 Likes

I forgot to reply you! The solution on the top.

Damn I forgot to reply you! The solution on the top.

It could work, however, I’m trying to make it work while having AlignPosition and AlignOrientation running at the same time.
Also, I need them to work on waypoints with only 2 needed to make it run.

I found this module. It may help.

I’m trying to do a 90-degree spin to the other waypoint as explained in the picture. I don’t really wanna use 3 parts to make it work, pretty sure there’s a different name for that. Here’s a video of the curve in Tower Defense Simulator:


I don’t know if they are using 3 parts for that, but it seems like it. How would I make the spin while having these two objects “Align Position” and “Align Orientation” running at the same time? Do I have to replace it with MoveTo?

You have to make a curve complex with i guess between 6,7,8,9,10 points to make like this.

for some reasons i was sent this post, i know this is quite an extremely late answer that you probably got the answer to by now but i’ll still answer if you need. Btw i would like to say that tds uses lerp to calculate the cframe/position with some bézier/spline curves then changing the cframe of the primary part using workspace:BulkMoveTo() or smth else (tdx uses that as apparently atrazine said it was the most optimized way to change the cframe of an enemy, which i can agree with a mid-end pc i can stay near 60 fps with over 1k enemies)now instead of align objects anymore

tho to do the scattering the easiest way would simply be to offset the root joint of the torso on the x axis so you don’t need to calculate a new curve of other which would be too heavy on computation for my personal opinion (that’s what i do since i’m lazy to find anything else) or offset the curve thing during the calculation.