I have a script were my AI character moves to point a to point b using path finding. Though my AI doesn't really avoid the obstacles. He turns for a little bit when the Path is written but a second later he moves directly to the direction to the target point. Please help?
**NOTE: **The path finding part is in the MoveToSpawnPoint() function (Lines 112-128).
-------------
--Variables--
-------------
local TC = nil
TC = script.Parent.Configuration.TeamColor.Value
local pointModel = Instance.new("Model",game.Workspace)
local hum = script.Parent.Humanoid
-------------
--Functions--
-------------
--Finds the closest Enemy
function findClosestEnemy()
--Variables
local p = game.Players:GetChildren()
local closestEnemy = nil
local maxDistance = 50
--Add NPCS to the players table
for _, v in pairs (script.Parent.Parent:GetChildren()) do
if v.Name == "Noob" or v.Name == "Guest" then
table.insert(p,v)
end
end
for _,player in pairs (p) do
--If "player" classname is a player then
if player.ClassName == "Player" then
if player.TeamColor ~= TC then
local char = player.Character
if char ~= nil then
local humanoid = char:findFirstChild("Humanoid")
if humanoid and humanoid.Health>0 then
if closestEnemy == nil and (char.Torso.Position - script.Parent.Torso.Position).magnitude<maxDistance then
closestEnemy = char
elseif (char.Torso.Position - script.Parent.Torso.Position).magnitude<maxDistance then
local NCEMag = (char.Torso.Position - script.Parent.Torso.Position).magnitude
local CEMag = (closestEnemy.Torso.Position - script.Parent.Torso.Position).magnitude
if NCEMag<CEMag then
closestEnemy = char
end
end
end
end
end
--If "player" classname is not a player then
else
if player ~= nil and player ~= script.Parent then
if player.Configuration.TeamColor.Value ~= TC then
local humanoid = player:findFirstChild("Humanoid")
if humanoid and humanoid.Health>0 then
if closestEnemy == nil and (player.Torso.Position - script.Parent.Torso.Position).magnitude<maxDistance then
closestEnemy = player
elseif (player.Torso.Position - script.Parent.Torso.Position).magnitude<maxDistance then
local NCEMag = (player.Torso.Position - script.Parent.Torso.Position).magnitude
local CEMag = (closestEnemy.Torso.Position - script.Parent.Torso.Position).magnitude
if NCEMag<CEMag then
closestEnemy = player
end
end
end
end
end
end
end
return closestEnemy
end
--Finds the closest Enemy SpawnPoint
function findClosestEnemySpawnpoint()
local SpawnsContainer = script.Parent.Parent:GetChildren()
local closestSpawnPoint = nil
for _, spawn in pairs (SpawnsContainer) do
if spawn.ClassName == "SpawnLocation" then
if spawn.TeamColor ~= TC then
if closestSpawnPoint == nil then
closestSpawnPoint = spawn
else
local NSPMag = (spawn.Position - script.Parent.Torso.Position).magnitude
local CSPMag = (closestSpawnPoint.Position - script.Parent.Torso.Position).magnitude
if NSPMag < CSPMag then
closestSpawnPoint = spawn
end
end
end
end
end
return closestSpawnPoint
end
--Visualizes the path (only for testing)
function visualizePath(path)
-- clear old path visualization
for _, point in pairs(pointModel:GetChildren()) do
point:Destroy()
end
-- calculate new visualization
for _, point in pairs(path) do
local part = Instance.new("Part")
part.Parent = pointModel
part.FormFactor = Enum.FormFactor.Custom
part.Size = Vector3.new(1,1,1)
part.Position = point
part.Anchored = true
part.CanCollide = false
end
end
--Move to SpawnPoint
function MoveToSpawnPoint(s)
local PF = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Torso.Position,s.Position,512)
local points = PF:GetPointCoordinates()
local closestPoint = nil
visualizePath(points)
for i = 1,#points do
wait(0)
if points[i] ~= nil then
hum:MoveTo(points[i])
end
if (script.Parent.Torso.Position-s.Position).magnitude <3 then
hum:MoveTo(s.Position,s)
end
end
end
--Move to Enemy
function MoveToEnemy(e)
local PF = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Torso.Position,e.Torso.Position,512)
local points = PF:GetPointCoordinates()
local closestPoint = nil
--visualizePath(points)
for i = 1,#points do
wait(0)
if (script.Parent.Torso.Position-e.Torso.Position).magnitude > 3 and points[i*2] ~= nil then
hum:MoveTo(points[i])
else
hum:MoveTo(e.Torso.Position,e.Torso)
end
end
end
-------------
--Task Area--
-------------
while wait(0) do
--Finds if there is any enemy close
local Enemy = findClosestEnemy()
if Enemy~= nil then
MoveToEnemy(Enemy)
--If theres not any enemy close, then go to the spawn points
else
local Spawn = findClosestEnemySpawnpoint()
if spawn ~= nil then
MoveToSpawnPoint(Spawn)
end
end
end
