So I'm making a zombie game, and these zombies use pathfinding to not run into walls, I've never used pathfinding before so I helped myself with a youtube tutorial, and the pathfinding actually worked decently, problem is, when the zombie has high walkspeed, it glitches, like this:
(It's a gif) https://gyazo.com/cfd6fd7e9b350809dd352f2ed6cac2e9
This is the pathfinding script, correct me if something is wrong with it, as I still don't get the hang of pathfinding perfectly:
--- Services ---
local Plrs = game:GetService("Players")
local RunS = game:GetService("RunService")
local PathfindingS = game:GetService("PathfindingService")
--- Variables ---
local char = script.Parent.Parent
local Hum = char.Humanoid
local HRP = char.HumanoidRootPart
local CanJump = false
local FOV = 50
local CharSize = char:GetExtentsSize()
local PathInfo = {
ModelRadius = (CharSize.X + CharSize.Z)/4,
ModelHeight = CharSize.Y,
ModelCanJump = CanJump
}
--- Initialization ---
HRP:SetNetworkOwner(nil)
--- Functions ---
local function FindClosestPlr()
local players = {}
local ClosestDistance = math.huge
local target
for index, targetplr in pairs(Plrs:GetPlayers()) do
local TargetChar = targetplr.Character
if TargetChar == nil then
continue
end
local distance = (TargetChar.HumanoidRootPart.Position - HRP.Position)
if distance.Magnitude <= FOV then
table.insert(players, {
Magnitude = distance.Magnitude,
Player = targetplr
})
end
end
for i, entry in pairs(players) do
local magnitude = entry.Magnitude
local player = entry.Player
if magnitude < ClosestDistance then
ClosestDistance = magnitude
target = player
end
end
return target
end
function Chase()
local plr = FindClosestPlr()
if plr == nil then
return
end
local plr_character = plr.Character or plr.CharacterAdded:Wait()
local plr_humanoid = plr_character.Humanoid
if plr_humanoid.Health <= 0 then
return
end
-- Chase
local plr_HRP = plr_character.HumanoidRootPart
local destination = plr_HRP.Position
local beginning = HRP.Position
local path = PathfindingS:CreatePath(PathInfo)
path:ComputeAsync(beginning, destination)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for cry, waypoint in pairs(waypoints) do
if waypoints.Action == Enum.PathWaypointAction.Jump then
Hum.Jump = true
end
Hum:MoveTo(waypoint.Position)
Hum.MoveToFinished:Wait()
end
else
Hum:MoveTo(HRP.Position)
end
end
--- Connections ---
RunS.Heartbeat:Connect(Chase)
Keep in mind both zombies are exaclty the same, just one has different part colors, and has 26 walkspeed, instead of 12.
