Pathfinding doesn’t work thru terrain and the NPC runs into the terrain instead of walking around it.
Can anybody please help me find the problem?
(NAV mesh veiw)
local PathFinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Enemy = script.Parent
local Humanoid = Enemy:WaitForChild("Humanoid")
Enemy.PrimaryPart:SetNetworkOwner(nil)
local waypoints
local newWaypointIndex
local reachedConnection
local blockedConnection
local WalkSpeed = Enemy:GetAttribute("WalkSpeed")
local SprintSpeed = Enemy:GetAttribute("SprintSpeed")
local Damage = Enemy:GetAttribute("Damage")
local anim = Humanoid:LoadAnimation(script.Walk)
anim:Play()
local function getPath(destination)
local path = PathFinding:CreatePath({
AgentHeaight = 6;
AgentRadius = 3;
AgentCanJump = false;
gaentCanClimb = false;
Costs = {
water = 100;
DangerZone = math.huge
}
})
path:ComputeAsync(Enemy.HumanoidRootPart.Position, destination.Position)
return path
end
local function findTarget()
local nearsetTarget
local MaxDistance = 100
for index, player in pairs(Players:getPlayers()) do
if player.Character then
local target = player.Character
local distance = (Enemy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < MaxDistance then
nearsetTarget = target
MaxDistance = distance
end
end
end
return nearsetTarget
end
local function Capture(target)
anim:AdjustSpeed(1)
local distance = (Enemy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 3 then
anim:AdjustSpeed(0.9)
Humanoid:MoveTo(target.HumanoidRootPart.Position)
else
target.Humanoid:TakeDamage(Damage)
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:getWaypoints()) do
local target = findTarget()
if findTarget() then
Capture(target)
Enemy.Humanoid.WalkSpeed = SprintSpeed
break
else
Enemy.Humanoid.WalkSpeed = WalkSpeed
Humanoid:MoveTo(waypoint.Position)
anim:AdjustSpeed(0.9)
Humanoid.MoveToFinished:wait()
anim:AdjustSpeed(0)
end
end
else
Humanoid:MoveTo(destination.Position - (Enemy.HumanoidRootPart.CFrame.LookVector * 10))
end
end
function Patrol()
local waypoints = workspace.Waypoints:GetChildren()
local randomWaypoint = math.random(1, #waypoints)
walkTo(waypoints[randomWaypoint])
end
while wait(0.01) do
Patrol()
end
I didn’t make this code since I’m very new to pathfinsing, but I’m trying to learn it.