Hello devs! Recently decided to add pathfinding for enemy in my game after a long time, but couldnt go far.
I tried many ways on using pathfind but all of them got the same issue - being “dumb”. (like spinning around on the same place, etc.)
warning: those scripts may look terrible because its a small template part from main AI code
So first I decided to use roblox’s pathfinding service, which code was:
local Humanoid = script.Parent.NPC
local Torso = script.Parent.Torso
local MainTarget = nil --actually, MainTorso is targets torso (function to get closest)
local PFS = game:GetService('PathfindingService')
local path = PFS:CreatePath()
path:ComputeAsync(Torso.Position, MainTarget.Position)
while task.wait() do
if path.Status == Enum.PathStatus.Success then
local Points = path:GetWaypoints()
local Point = Points[3] or Points[2] or Points[1] or Points[0] or MainTarget
Humanoid:MoveTo(Point.Position, game:GetService("Workspace"):FindFirstChild("Terrain"))
end
end
this method is bad i’d say, because being dumb as well
2nd method I used which is awful
local Humanoid = script.Parent.NPC
local Torso = script.Parent.Torso
local MainTarget = nil --actually, MainTorso is targets torso (function to get closest)
local PFS = game:GetService('PathfindingService')
local path = PFS:CreatePath()
path:ComputeAsync(Torso.Position, MainTarget.Position)
while task.wait() do
local PFS = game:GetService('PathfindingService')
local path = PFS:CreatePath()
path:ComputeAsync(Torso.Position, MainTarget.Position)
if path.Status == Enum.PathStatus.Success then
local Points = path:GetWaypoints()
for m,n in pairs(Points) do
local Point = n
Humanoid:MoveTo(Point.Position, game:GetService("Workspace"):FindFirstChild("Terrain"))
end
end
end
3rd method I used which was probably one of the best, but it got a huge issue which is working only of enemy has a trajectory of where character is going to, else enemy will stand still.
CREDITS TO MODULE: SimplePath - Pathfinding Module
local Character = script.Parent
local Torso = Character.Torso
local MainTarget = nil --actually, MainTorso is targets torso (function to get closest)
while task.wait() do
local path = require(game.ReplicatedStorage.Modules.PathfindModule).new(Character)
path.Visualize = true
path:Run(MainTarget)
end
And last one method I’ve used so far got same mistake as 2nd method (spinning around)
CREDITS TO MODULE: EZ Pathfinding V5
local Character = script.Parent
local Torso = Character.Torso
local MainTarget = nil --actually, MainTorso is targets torso (function to get closest)
local Module = require(game.ReplicatedStorage.Modules.PathfindingV5)
while task.wait() do
LastPathWay = Module.new(Character, MainTarget)
LastPathWay:Play()
end
Anyone got question how to make perfect AI chasing pathfind? Or maybe any good PF module?