Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Pathfinding will not do as intended when stuck on a wall. Can anyone help?

Asked by 3 years ago
Edited 3 years ago

Alright, so I got a code for an NPC that will chase you and was supposed to get itself unstuck when it runs into a wall. However it just stays stuck forever. There aren't any errors. The NPC was supposed to get itself unstuck at line 91 and 92..


local figure = script.Parent local humanoid = figure:WaitForChild("Humanoid") local humanoidRootPart = figure:WaitForChild("HumanoidRootPart") local waypoints = game.Workspace.Waypoints local PathfindingService = game:GetService("PathfindingService") figure.PrimaryPart:SetNetworkOwner(nil) local function canSeeTarget(target) local origin = figure.HumanoidRootPart.Position local direction = (target.HumanoidRootPart.Position - figure.HumanoidRootPart.Position).unit * 40 local ray = Ray.new(origin, direction) local hit, pos = workspace:FindPartOnRay(ray, figure) 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 = 40 local nearestTarget for index, player in pairs(players) do if player.Character then local target = player.Character local distance = (figure.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude if distance < maxDistance and canSeeTarget(target) then nearestTarget = target maxDistance = distance end end end return nearestTarget end function getPath(destination) local pathParams = { ["AgentHeight"] = 5, ["AgentRadius"] = 3.5, ["AgentCanJump"] = true } local path = PathfindingService:CreatePath(pathParams) path:ComputeAsync(humanoidRootPart.Position, destination.Position ) return path end local function attack(target) local distance = (figure.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude if distance > 8 then humanoid:MoveTo(target.HumanoidRootPart.Position) else --local attackAnim = humanoid:LoadAnimation(script.Attack) --attackAnim:Play() target.Humanoid.Health = 0 end end local function walkTo(destination) local path = getPath(destination) if path.Status == Enum.PathStatus.Success then for index, waypoint in pairs(path:GetWaypoints()) do if waypoint.Action == Enum.PathWaypointAction.Jump then humanoid.Jump = true end local target = findTarget() if target and target.Humanoid.Health > 0 then print("TARGET FOUND", target.Name) attack(target) break else print("Moving to ", waypoint.Position) humanoid:MoveTo(waypoint.Position) humanoid.MoveToFinished:Wait() end end else print("STUCK LOL") humanoid:MoveTo(destination.Position - (figure.HumanoidRootPart.CFrame.LookVector / 5)) humanoid.Jump = true end end local function Patrol() waypoints = game.Workspace:WaitForChild("Waypoints"):GetChildren() local randomNum = math.random(1,#waypoints) walkTo(waypoints[randomNum]) end while wait(0.25) do Patrol() end
0
On line 49, I set the radius as 3.5 hoping it would work. I know it's supposed to be 2 because it's the default R6 model. Pee_Ninja 15 — 3y

Answer this question