Pathfinding horror AI getting stuck/bumping on walls

Hello,

So I’ve been using the Teddy AI pathfinding script from the youtuber GnomeCode and have been running into an issue where it gets stuck or even bumps into walls while chasing you in my maze. I’ve tried getting a response from them but had no luck, tried looking for threads relating to this issue but didn’t find anything useful for myself. I have no clue what to do to say the least as I don’t specialize in scripting nor know very well how the pathfinding system works by heart.

Video of what’s happening:


As you see, it does chase you but gets stuck on walls when you are walking next to them.

How things are set up (monster is in Workspace):
image

Code inside the “AI” script:

local Igor = script.Parent
local humanoid = Igor.Humanoid
Igor.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
	local origin = Igor.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - Igor.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, Igor)
	
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 60
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (Igor.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")
	
	local pathParams = {
		["AgentHeight"] = 8.25,
		["AgentRadius"] = 2.85,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	
	path:ComputeAsync(Igor.HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function attack(target)
	local distance = (Igor.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

	if distance > 2 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		local attackAnim = humanoid:LoadAnimation(script.Attack)
		attackAnim:Play()
		target.Humanoid.Health = 0
	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 target and target.Humanoid.Health > 0 then
				print("TARGET FOUND", target.Name)
				attack(target)
				break
			else
				print("Moving to ", waypoint.Position)
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (Igor.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait() do
	patrol()
end

This has been a problem for the longest time ever since I implemented it in and have no idea how to fix this, I’d truly appreciate anyone who could be kind enough to assist me with this problem I’m facing! Somehow doesn’t give me problems in the testing game but once in put it in my main, then that’s where the problem starts.

4 Likes

It could be due to the agent radius not being large enough, make sure you have factored in the head and left a little room for error

The height is right, the radius should be too since the AI is about 4.8 studs wide and the radius would have to be half of that (2.8). Yet, the AI continues to tweak out thus the calculations being correct.

What do you mean factored in the head? If you mean like including it when seeing how wide it is, then yeah I also included it. Leave some room for error? As in what to be specific, sorry I don’t understand this that well.

Every other part except the humanoid root part is cancollide off right?

Followed the same tutorial, having this issue as well.

1 Like

Having encountered many similar issues with PathfindingService, I highly recommend adding in-world debugging visualization to your game during development. I place an anchored, non-collidable neon Ball part at every waypoint position I get from the pathfinder. I personally use white balls for walk-to nodes, and yellow ones for jump nodes, but you could also place agent-radius-sized cylinders if that would be useful. This can help you find out if waypoints are being placed correctly in world space, and that the path is sensible.

3 Likes