AI bumping into walls

Hello there,

So I’ve been working on this AI for a little while now but it keeps bumping into walls at certain points as shown in the video down below:


I’ve checked multiple different posts & tried all of it but it still ain’t working.
Here’s my code:

local pathfindingService = game:GetService("PathfindingService")
local players = game:GetService("Players")

local monster = workspace:WaitForChild("Monster")
local primary = monster.PrimaryPart

local humanoid = monster:WaitForChild("Humanoid")
local waypoints = workspace:WaitForChild("Points")

local maxFindingRange = 30
local maxKillingRange = 5

local chasingSpeed = 17
local regularSpeed = 12

local prevPoint = nil

primary:SetNetworkOwner(nil)

task.wait(2)

local function GetPath()
	local path = pathfindingService:CreatePath{
		['AgentRadius'] = 2,
		['AgentHeight'] = 5,
		['AgentCanJump'] = false,
		['AgentCanClimb'] = false,
		['WaypointSpacing'] = 4,
		['Costs'] = {
			Water = math.huge,
		}
	}
	
	return path
end

local function WalkTo(dest: Vector3, isPoint: boolean)
	local path: Path = GetPath()
	path:ComputeAsync(primary.Position, dest)

	local points = path:GetWaypoints()
	if not (points) then return end

	for _, point in pairs(points) do
		local part = Instance.new("Part", workspace)
		part.Position = point.Position
		part.Color = Color3.fromRGB(255, 255, 15)
		part.Shape = "Ball"
		part.Anchored = true
		part.CanCollide = false
		part.Size = Vector3.new(2, 2, 2)
		game:GetService("Debris"):addItem(part, 1)

		humanoid:MoveTo(point.Position)
		humanoid.MoveToFinished:Wait()

		print('reached waypoint')
	end
end

local function GetRandomWaypoint()	
	local int = 1
	
	if (prevPoint ~= nil) then
		int = tonumber(prevPoint.Name) + 1
	end
	
	if (int > #waypoints:GetChildren()) then
		int = 1
	end
	
	local point = waypoints:FindFirstChild(tostring(int))
		
	if (point) then
		prevPoint = point
		return point
	else
		point = waypoints:FindFirstChild('1')
	end
end

local function Patrol()
	local point = GetRandomWaypoint()
	
	print('Found point: ' .. point.Name)
	
	WalkTo(point.Position, true)
end

while task.wait() do
	print('Patrolling')
	Patrol()
end

No errors are displayed either,

2 Likes

Can’t say for certain, however it looks that this is just a classic example of the PathfindingService operating poorly when it comes to it’s mathematical calculutaions (How to get from point A to point B)

Could be a simple mistake somewhere in the code, or something to optimize - But i can’t put my finger on anything large that could be going wrong when running your script.

Don’t hang me up on anything though ;D

1 Like

Any suggestions on modules that people have made to optimize it / to make it work better?

Try changing the AgentRadius and maybe the WaypointSpacing.

1 Like

This is just a temporary fix, but when I set the AgentRadius to 1 it seems to work fine. Although I believe this might cause bugs later when I implement a bigger figure (the real model for the AI) & when I add obstacles.

I guess I’ll just try to play with it a bit more & see if it works out fine

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