Hello devs,
Today i got a problem when I was scripting my enemy NPC’s pathfinding system, it simply ignores the obstacle in front of it and the system think that it is a traversable path.
Something like this :
By the way, here is the code if needed :
local PFS = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local rig = script.Parent
local hum = rig.Humanoid
local hrp = rig.HumanoidRootPart
local walkspeedValue = rig:WaitForChild("Walkspeed")
local NearestT = rig:WaitForChild("NearestTarget")
local blocked = rig:WaitForChild("IsBlocked")
local maxDistance = 40
local maxRoamingRange = 15
local roamingDebounce = false
function VisuailzeWaypoints(position)
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Color = Color3.new(1, 0, 0)
part.Material = Enum.Material.Neon
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = position
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
game.Debris:AddItem(part, 2)
end
function GetNearestEnemy()
local nearestPlayer, distance, closet
for _, player in pairs(Players:GetPlayers()) do
if player.Character ~= nil and player.Character.Humanoid.Health ~= 0 then
local targetCharacter = player.Character
if not targetCharacter:FindFirstChild("HumanoidRootPart") then continue end
distance = (rig.HumanoidRootPart.Position - targetCharacter.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
if closet then
if closet < maxDistance then
nearestPlayer = targetCharacter
closet = distance
end
else
nearestPlayer = targetCharacter
closet = distance
end
end
end
end
return nearestPlayer
end
function getPath(destination)
local pathParams = {
["AgentHeight"] = 5,
["AgentRadius"] = 4,
["AgentCanJump"] = true,
["WaypointSpacing"] = 0.5
}
local path = PFS:CreatePath(pathParams)
path:ComputeAsync(rig.HumanoidRootPart.Position, destination)
return path
end
function roaming()
walkspeedValue.Value = 10
if roamingDebounce then return end
task.spawn(function()
roamingDebounce = true
local xRand = hrp.Position.X + math.random(-maxRoamingRange, maxRoamingRange)
local zRand = hrp.Position.Z + math.random(-maxRoamingRange, maxRoamingRange)
local path = getPath(Vector3.new(xRand, 0, zRand))
for i, v in path:GetWaypoints() do
VisuailzeWaypoints(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
rig.Humanoid:MoveTo(v.Position)
end
task.wait(math.random(3, 6))
roamingDebounce = false
end)
end
function moveToCharacter(nearestChar)
walkspeedValue.Value = 16
local path = getPath(nearestChar.PrimaryPart.Position)
if path.Status == Enum.PathStatus.Success then
for i, v in path:GetWaypoints() do
VisuailzeWaypoints(v.Position)
if blocked.Value then continue end
if v.Action == Enum.PathWaypointAction.Jump and hrp.Position.Y < NearestT.Value.PrimaryPart.Position.Y then
hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
rig.Humanoid:MoveTo(v.Position)
end
else
hum:MoveTo(nearestChar.PrimaryPart.Position - (hrp.CFrame.LookVector * 8))
end
end
task.spawn(function()
while true do task.wait()
local nearestChar = GetNearestEnemy()
NearestT.Value = nearestChar
if nearestChar and not blocked.Value then
moveToCharacter(nearestChar)
elseif not blocked.Value then
roaming()
else
task.wait(0.2)
end
end
end)
while true do task.wait()
local raycast = workspace:Raycast(hrp.Position, hrp.CFrame.LookVector*1.4)
if (raycast and raycast.Instance) and NearestT.Value then
blocked.Value = true
hum:MoveTo(hrp.Position - (hrp.CFrame.LookVector * 5))
else
task.delay(0.5, function()
blocked.Value = false
end)
task.wait(0.5)
end
end
An unsticking system using Raycasting has been implemented but nothing seems works.
Any help is highly appreciated!!!