I made a zombie npc, that detects the player and walks to waypoints, however when patrolling it gets stuck on walls or corners, i have no idea why this is happening even when using pathfinding
-
What is the issue?
still gets stuck on walls even with pathfinding -
What solutions have you tried so far? Tried searching on devforum, yt
--///
local Zombie = script.Parent
local humanoid = Zombie.Humanoid
local attack1 = script.Parent.AI.attack1
local attack2 = script.Parent.AI.attack2
local damage1 = script.Parent.AI.damage1
local damage2 = script.Parent.AI.damage2
local damage3 = script.Parent.AI.damage3
Zombie.HumanoidRootPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
local origin = Zombie.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - Zombie.HumanoidRootPart.Position).unit * 75
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, Zombie)
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 = 75
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
pcall(function()
local distance = (Zombie.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) and target.zombieInfected.isAZombie.Value == false then
nearestTarget = target
maxDistance = distance
end
end)
end
end
return nearestTarget
end
local function getPath(destination)
local PathfindingService = game:GetService("PathfindingService")
local pathParams = {
["AgentHeight"] = 15,
["AgentRadius"] = 4,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(Zombie.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Zombie.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 2.3 then
--R15--
--humanoid:MoveTo(target.UpperTorso.Position + target.UpperTorso.Velocity)
--R6
humanoid:MoveTo(target.Torso.Position + target.Torso.Velocity)
else
local attackAnim = humanoid:LoadAnimation(script.Attack)
attackAnim:Play()
target.Humanoid.Health = target.Humanoid.Health -11
local random = math.random(1, 3)
if random == 1 then
attack1:Play()
damage1:Play()
elseif random == 2 then
damage2:Play()
attack2:Play()
elseif random == 3 then
damage3:Play()
end
humanoid.WalkSpeed = 1
wait(0.3)
humanoid.WalkSpeed = 9
end
end
local distance
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoints2 in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
print("PLAYER FOUND", target.Name)
humanoid.WalkSpeed = 13.6
attack(target)
break
else
humanoid.WalkSpeed = 9
print("Moving to ", waypoints2.Position)
local waypointPosition = waypoints2.Position
humanoid:MoveTo(waypointPosition)
repeat
distance = (waypointPosition - humanoid.Parent.PrimaryPart.Position).magnitude
wait()
until
distance <= 30
end
end
else
humanoid:MoveTo(destination.Position) -- (Zombie.HumanoidRootPart.CFrame.LookVector * 70))
end
end
function patrol()
local waypoints = workspace.waypoints2:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait(0.25) do
patrol()
end