Pathfinding AI clipping walls

How can I make my enemy not clip into my walls?
I made this pathfinding script and it works but it likes to clip on walls, ive tried changing agent radius to many different values and I see no change in its behavior, other than setting it too high makes it unable to generate a path, ive also tried making the wall material cost be math.huge.

And ive tried making the hitbox smaller and larger.
Do I just have to make my own pathfinding script without the service?

Video: https://youtu.be/qryDlB4smfI

local Pathfinding = {}

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local path = PathfindingService:CreatePath({
	
	AgentRadius = 1.5,
	AgentHeight = 6,
	AgentCanJump = false,
	WaypointSpacing = 4,
	Costs = {
		Rubber = math.huge,
		Carpet = 1,
		Plaster = math.huge
	}
})

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

function Pathfinding.followPath(AIcharacter,PlayerCharacter)
	print('runig')
	local target = PlayerCharacter:WaitForChild('HumanoidRootPart').Position
	local humanoid = AIcharacter.Humanoid
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(AIcharacter.PrimaryPart.Position, target)
	end)
	if success and path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		waypoints = path:GetWaypoints()
		-- Detect if path becomes blocked
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				print("Blocked")
				Pathfinding.followPath(target)
			end
		end)

		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					-- Increase waypoint index and move to next waypoint
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end

		-- Initially move to second waypoint (first waypoint is path start; skip it)
		--print(waypoints)
		nextWaypointIndex = 2
		if waypoints[nextWaypointIndex] and waypoints[nextWaypointIndex].Position then
			humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		end
		--print(waypoints)
	else
		warn("Path not computed!", errorMessage)
	end
end

function Pathfinding.showPath()
	for i,v in ipairs(game.Workspace:GetChildren()) do
		if v.Name == "WaypointThis" then
			v:Destroy()
		end
	end
	
	for i,v in ipairs(waypoints) do
		local part = Instance.new("Part")
		part.Name = "WaypointThis"
		part.Parent = game.Workspace
		part.Size = Vector3.new(1,1,1)
		part.Anchored = true
		part.CanCollide = false
		part.Position = v.Position
	end
end

return Pathfinding

Increase AgentRadius significantly, or adjust the level geometry by making walls thicker or adding invisible “padding” walls for the pathfinder. Thin walls have problems with AgentRadius.. I see you’re using Costs.. I like to make one fake one.. create a material that looks like the one that is there (off a material you’re not using), then use it to line trouble areas.

Thank you for replying! 2.5 agent radius is the highest I can go before it cant generate a path.
My walls are more than 4 studs thick. Should it be thicker? Because that already seems pretty thick.
And my walls are the Plaster material and there is a rubber material that is lining the walls, both of which are math.huge.
After all this, its the same result. And im not too sure if I am understanding your fake cost sentence correctly.

Well, it’s not “fake.” I mean, you can make it look like something that is already there by using a material you’re not using anywhere else then making it look like what is there (if that even matters to you). Odd that they would even walk on it if this is the case. Your wall thickness sounds fine. You may have to go all out here with invisible padding walls along the walls to force the mesh farther out. The spacing can do this also. Try messing around with that a bit. This stuff is far from perfect, you do have to mess around with it. The one I learned the most from was one I had it show balls at the way-points and had a line go in-between so I could see just what it was doing.. How wide your “fake” material is, is going to count with the spacing too.

1 Like

Ok thank you. Ill start putting some plastic around the walls.

1 Like

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