local pathfindingService = game:GetService("PathfindingService") local creepsattackrange…
- User
local pathfindingService = game:GetService("PathfindingService") local creepsattackrange = 4 local targetCreepListDire = {} local targetCreepListRadiant = {}
local function startFight(creep, targetCreepList, enemyThrone, creepsattackrange) local path = pathfindingService:FindPathAsync(creep.HumanoidRootPart.Position, enemyThrone.Position) -- Найти путь до трона противоположной фракции if path.Status == Enum.PathStatus.Success then local waypoints = path:GetWaypoints() for i, waypoint in ipairs(waypoints) do -- Перемещение крипа по пути creep.Humanoid:MoveTo(waypoint.Position) -- Драться с крипами противоположной фракции, если они встречены for _, targetCreep in ipairs(targetCreepList) do if (targetCreep.HumanoidRootPart.Position - creep.HumanoidRootPart.Position).magnitude <= creepsattackrange then -- начать сражение end end end end end
wait(0.1) for i, enemyCreep in pairs(game.Workspace:GetChildren()) do if enemyCreep.Name == "DireCreep" then table.insert(targetCreepListRadiant, enemyCreep) elseif enemyCreep.Name == "RadiantCreep" then table.insert(targetCreepListDire, enemyCreep) end end
if script.Parent.Name == "DireCreep" then for _, creep in ipairs(targetCreepListDire) do startFight(script.Parent, targetCreepListDire, game.Workspace.RadiantAncient, creepsattackrange) end elseif script.Parent.Name == "RadiantCreep" then for _, creep in ipairs(targetCreepListRadiant) do startFight(script.Parent, targetCreepListRadiant, game.Workspace.DireAncient, creepsattackrange) end end КРИПЫ НЕ ОБХОДЯТ ПРЕПЯТСТВИЯ
- Assistant
В вашем коде проблема заключается в том, что вы не проверяете, может ли крип пройти через препятствие на пути к трону противоположной фракции. PathfindingService:FindPathAsync() не учитывает препятствия на пути и просто ищет наиболее короткий путь между двумя точками.
Чтобы крипы обходили препятствия, вам нужно использовать PathfindingService:CreatePath() вместо PathfindingService:FindPathAsync(). Затем вы можете использовать методы Path:ComputeAsync() и Path:GetWaypoints() для нахождения пути и перемещения крипа по нему. Этот подход учтет препятствия и позволит крипам обходить их.
Ниже приведен исправленный код для функции startFight():
local function startFight(creep, targetCreepList, enemyThrone, creepsattackrange) local path = PathfindingService:CreatePath() path:ComputeAsync(creep.HumanoidRootPart.Position, enemyThrone.Position) local waypoints = path:GetWaypoints() for i, waypoint in ipairs(waypoints) do creep.Humanoid:MoveTo(waypoint.Position) for _, targetCreep in ipairs(targetCreepList) do if (targetCreep.HumanoidRootPart.Position - creep.HumanoidRootPart.Position).magnitude <= creepsattackrange then -- начать сражение end end end endТеперь, чтобы крипы обходили препятствия, вам нужно создать экземпляр класса Region3 охватывающий препятствия на вашей карте и передать его в PathfindingService:CreatePath(). Это позволит алгоритму поиска пути учитывать препятствия при нахождении пути для крипов.
local obstacles = game.Workspace.Obstacles:GetChildren() -- Замените на свою группу препятствий local region3 = Region3.new() for i, obstacle in ipairs(obstacles) do region3:Add(workspace:WaitForChild(obstacle.Name).Part:GetBoundingBox()) end local path = PathfindingService:CreatePath() path:ComputeAsync(creep.HumanoidRootPart.Position, enemyThrone.Position, region3) local waypoints = path:GetWaypoints() for i, waypoint in ipairs(waypoints) do -- остальной код endВышеуказанный код создаст регион, содержащий все препятствия на вашей карте. Когда путь вычислен, он будет учитывать этот регион и обходить препятствия на пути.