I wanted to make a pathfinding system for a monster.
However the Monsters NPC cannot pass this corner and the path is way to close to the corner aswell. I’ve tried setting the AgentRadius up but it either can’t move through the map or gets stuck on this corner.
local PathFindingService = game:GetService("PathfindingService")
local Path = PathFindingService:CreatePath({
AgentRadius = 3.5,
AgentCanJump = false
})
local NPC = script.Parent
local nextWaypointIndex = 2
local Blocked
local Waypoints
local waypoints = game.Workspace.MonsterWayPoints:GetChildren()
local RCParams = RaycastParams.new()
RCParams.FilterType = Enum.RaycastFilterType.Whitelist
local Walls = game.Workspace.map.walls
local NPCWatchDistance = 200
local NPCSeeDistance = 10
wait(3)
local function CheckForTarget()
local Target = nil
for _,v in pairs(game.Players:GetChildren()) do
local Character = v.Character or v.CharacterAdded
if Character and Character.Humanoid.Health > 0 and table.find(game.Workspace:FindPartsInRegion3WithWhiteList(_G.SafeSpawnRoomRegion,{Character.HumanoidRootPart}),Character.HumanoidRootPart) == nil then
if (Character.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Magnitude > NPCSeeDistance then
RCParams.FilterDescendantsInstances = {Character,Walls:GetChildren()}
local RayCastResult = game.Workspace:Raycast(NPC.HumanoidRootPart.Position,((Character.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position) * NPCWatchDistance),RCParams)
if RayCastResult.Instance.Parent == Character then
if Target == nil then
Target = Character
elseif (Target.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Magnitude > RayCastResult.Distance then
Target = Character
end
end
else
if Target == nil then
Target = Character
elseif (Character.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Magnitude < (Target.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Magnitude then
Target = Character
end
end
elseif table.find(game.Workspace:FindPartsInRegion3WithWhiteList(_G.SafeSpawnRoomRegion,{Character.HumanoidRootPart}),Character.HumanoidRootPart) ~= nil then
Target = nil
end
end
return Target
end
local LastPos
while wait() do
local Target = CheckForTarget()
print(Target)
print(LastPos)
if Target then
local success, errorMessage = pcall(function()
Path:ComputeAsync(NPC.HumanoidRootPart.Position, Target.HumanoidRootPart.Position)
end)
if success and Path.Status == Enum.PathStatus.Success then
Waypoints = Path:GetWaypoints()
Blocked = Path.Blocked:Connect(function(BlockedWaypointIndex)
if BlockedWaypointIndex >= nextWaypointIndex then
Blocked:Disconnect()
Path:ComputeAsync(NPC.HumanoidRootPart.Position, Target.HumanoidRootPart.Position)
end
end)
if nextWaypointIndex <= #Waypoints then
NPC.Humanoid:MoveTo(Waypoints[nextWaypointIndex].Position)
end
end
LastPos = Target.HumanoidRootPart.Position
elseif LastPos then
print("Thang")
local success, errorMessage = pcall(function()
Path:ComputeAsync(NPC.HumanoidRootPart.Position, LastPos)
end)
if success and Path.Status == Enum.PathStatus.Success then
Waypoints = Path:GetWaypoints()
Blocked = Path.Blocked:Connect(function(BlockedWaypointIndex)
if BlockedWaypointIndex >= nextWaypointIndex then
Blocked:Disconnect()
Path:ComputeAsync(NPC.HumanoidRootPart.Position, LastPos)
end
end)
local Next = 2
local ReachedConnection
while Next < #Waypoints and wait() do
Target = CheckForTarget()
if Target then
break
end
if not ReachedConnection then
ReachedConnection = NPC.Humanoid.MoveToFinished:Connect(function(reached)
if reached and Next < #Waypoints then
Next += 1
end
end)
end
NPC.Humanoid:MoveTo(Waypoints[Next].Position)
end
if ReachedConnection then
ReachedConnection:Disconnect()
end
if Blocked then
Blocked:Disconnect()
end
end
else
end
end