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

How to stop stuttering in a pathfinding follow NPC?

Asked by 6 years ago

Trying to make an NPC that uses pathfinding to follow a player, but whenever I test it the NPC stutters and lacks behind. Any help?

01local workspace = game.Workspace
02 
03 
04 
05function moveToPostition(moveTo)
06    local path =  game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Parent.LowerTorso.Position, moveTo, 200)
07    local points = path:GetPointCoordinates()
08 
09    for p = 1, #points do
10        script.Parent:MoveTo(points[p])
11        repeat wait() until script.Parent.MoveToFinished
12    end
13end
14 
15while true do
16    wait(0.01)
17    walkevent = moveToPostition(workspace.BoxedHouses.Torso.Position)      
18end
0
MoveToFinished is not a boolean, it's an event. User#19524 175 — 6y
0
Well I don't know how to make it wait for an event so can you be a bit more descriptive? JarFullOfMayonnaise 48 — 6y
0
1/2 To work off of what incapaz mentioned, you can use script.Parent.MoveToFinished:Wait() to yield properly. Not sure if that will necessarily solve your problem, though. Roblox's pathfinding yields a ton of unnecessary points (as do most pathfinding algorithms). My solution to the unnatural stuttery movement was simply raycast between each brick to gauge visibility, and then deleting extraneous saenae 318 — 6y
0
2/2 bricks. Of course, this comes with a performance cost, but I've found that it's pretty worth it for the fluid movement. saenae 318 — 6y
View all comments (7 more)
0
Also, as a note, you can't yield for 1/100th of a second, so your wait(0.01) is probably just better placed being written as wait(); :P Doesn't make a difference functionally, but the number is misleading. saenae 318 — 6y
0
Using script.Parent.MoveToFinished:Wait() made it stutter less, but it waits any time I turn. JarFullOfMayonnaise 48 — 6y
0
I don't understand what you mean by raycasting in-between bricks. There are no bricks. JarFullOfMayonnaise 48 — 6y
0
My problem is, instead of chasing me (always taking the shortest and fastest route) it more of just mirrors the moves I made half a second ago. JarFullOfMayonnaise 48 — 6y
0
My mistake, 'bricks' was the wrong convention to use there, you have them denoted as points. Same concept; you can simply raycast between each of those 'points' and ensure that there is nothing in the way. That would just fix unnatural movement upon further reflection though, I'm not really sure why you're yielding at each turn. Does it wait long? saenae 318 — 6y
0
I just read your latest comment, it's probably just an issue that you aren't pathfinding often enough. See, you're just following the path until you've reached the last point, rather than actually checking for the player every so often. saenae 318 — 6y
0
Problem is, I want things to be in the way. Also its just a stutter when it stops. JarFullOfMayonnaise 48 — 6y

1 answer

Log in to vote
0
Answered by
saenae 318 Moderation Voter
6 years ago

Alright, not sure if I'm still misunderstanding the problem somewhat, but let me know if the following works:

01local pathfindingCoolDown = 0.2;
02 
03function moveToPosition(moveTo)
04    local path =  game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.Parent.LowerTorso.Position, moveTo, 200)
05    local points = path:GetPointCoordinates()
06 
07    local startTime = tick();
08    for p = 1, #points do
09        script.Parent:MoveTo(points[p])
10        script.Parent.MoveToFinished:Wait();
11        if(startTime-tick() > pathfindingCoolDown) then
12            return
13        end
14    end
15end
16 
17while wait() do
18    moveToPosition(workspace.BoxedHouses.Torso.Position)      
19end
0
That made it slower. JarFullOfMayonnaise 48 — 6y
0
Try replacing the while wait() do with a while true do. Seems to make it stutter free for me. Just accommodate it with some error checking to be sure that it doesn't run unyieldingly while paths aren't being generated. saenae 318 — 6y
Ad

Answer this question