Making pathfinding smoother

Hello, I am new to working on how to make an enemy for my game. The problem would be that my enemy will stop at each waypoint and then continue towards the enemy. How can I make it smoother as if it was not following any sort of line towards the player

local function findTarget()
	local dis = 300
	local target = nil
	for i,v in pairs(game.Workspace:GetChildren()) do
		local hum = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("raydetector")
		if hum and torso and v ~= script.Parent then
			if (ctorso.Position - torso.Position).magnitude < dis then
				dis = (ctorso.Position - torso.Position).magnitude
				target = torso
			end
		end
	end
	return target
end

while wait(.000001) do
	local target = findTarget()
	if target then
		local ray = Ray.new(ctorso.position, (target.Position - ctorso.Position).Unit * 200)
		local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
		if hit then
			if cHumanoid.Health <= 1 then
				alive = false
			end
			if alive == false then
				local rip = script.ded
				local ded = cHumanoid:LoadAnimation(rip)
				script.Parent["Meshes/Head (1)"].Sound:Play()
				ded:Play()
				wait(4)
				ctorso.Parent:Destroy()
			end
			path = pathfind:CreatePath()
			path:ComputeAsync(ctorso.Position, target.Position)
			print(path.Status)
			waypoints = path:GetWaypoints()
			if path.Status == Enum.PathStatus.NoPath then
				cHumanoid:MoveTo(target.Position)
			end


			for i,waypoint in pairs(waypoints) do
				print(waypoint.Position)
				cHumanoid:MoveTo(waypoint.Position)
				cHumanoid.MoveToFinished:wait(2)
				if (waypoints[#waypoints].Position - target.Position).magnitude > 10 then
					break
				end
				if (ctorso.Position - target.Position).magnitude < 10 then
					cHumanoid:MoveTo(target.Position)
					local attack = script.Attack
					local fire = cHumanoid:LoadAnimation(attack)
					fire:Play()
					for i,v in pairs(ctorso.Parent.Damagepart:GetChildren()) do
						if v:IsA("BasePart") or v:IsA("UnionOperation") and v ~= script.Parent.HumanoidRootPart then
							v.Touched:Connect(function(p)
								if p.Parent:FindFirstChild("raydetector") then
									if ouch then
										ouch = false
										p.Parent.Humanoid:TakeDamage(49)
										wait(2)
										ouch = true
									end
								end
							end)

						end
					end
					wait(2.5)
					ouch = true
				end
			end

		end

	end

end

Instead of using all of that, if you want the enemy to attack the player directly; you can just use a :MoveTo line that makes the enemy move to a player.

Example:

Humanoid:MoveTo(player)

(Humanoid being the humanoid of the enemy, player being the target)

Would I need waypoints for it to move around walls however?

Ah, if you’re gonna have them move around walls, here’s something that may be helpful.

Well, the issue I have is that they stop at each waypoint, I can make them move around walls. I want for it to be more smoother

cHumanoid.MoveToFinished:wait(2)

Change that to

cHumanoid.MoveToFinished:wait()

Only difference is you were waiting 2 seconds in between waypoints, so I modified to make it wait the default wait time.

This worked, thank you very much!

1 Like