Roblox pathfinding doesn't avoid terrain walls

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.

3 Likes

And it’s also unable to find complicated paths thru terrain. Like instead of crossign a dirt bridge the NCP goes under the bridge

1 Like

I’m also seeing this problem. Very easy to reproduce, take baseplate map, Generate terrain size 1000x1x1000, select Plains,Mountains,Hills. Visualize nav mesh, use plugin like Pathfinder to visualize path/waypoints. Terrain is ignored except for water. If you move the baseplate below terrain, you can get a path with waypoints , but it completely ignores non-water terrain, and is just using the positions on the baseplate part:


Here is the actual terrain above the part that it should be following:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.