Pathfinding gets stuck most of the time with over 3 NPC's

I put it right here


https://gyazo.com/cde2bb18e62120a84199da0b2c1fa927

Below the path creation

And btw, not only almost 2 seconds, it jumped up to 15 seconds as well

Oh no, donā€™t use what I had raw. I meant to actually spread those lines out around your code. In your case, I suggest starting with the pathfinding part. Iā€™ll set that up for you:

-- For finding nearest root
local preRoot = tick()
local nrstt = FindNearest(hroot.Position,zeto)
print("Time to find nearest:", tick() - preRoot)

-- For your path computation
local preCompute = tick()
path = pfs:CreatePath()
path:ComputeAsync(hroot.Position, nrstt.Position)
waypoint = path:GetWaypoints()
print("Time to compute path:", tick() - preCompute)

Alright, iā€™ll have to do that tomorrow, thanks.

Alright, hereā€™s some results, and since itā€™s a Stepped function then thereā€™s pretty much quite a lotā€¦

https://gyazo.com/933ca530d7b8093bb33792e81fc06bd6.png

https://gyazo.com/6f6b0e76f1fac58922f0129ab35d8b93.png

https://gyazo.com/1edc0ffb4789bd15deed9d4bf41111c8.png

Now, that was some of the humanoids that actually detected me or somewhatā€¦
There is always this one that randomly gets stuck here, and this is his output

https://gyazo.com/130470ad8fec1af7a28f82243a388577.png

If need be I can even invite you to a private place with the same script to have some testingā€¦

Ah. It seems that the slowdown is coming from path computation which is taking a noticeable amount of miliseconds to complete. Thankfully, that helps isolate the speed issue, though Iā€™m not quite sure about the jump issue.

Unfortunately for me, the Developer Hub is having a few unresolved issues so Iā€™m going off of what I remember as well as an API reference site. Iā€™d be happy to help further if youā€™d like though.

Within CreatePath, you can pass a dictionary that modifies the path creation. These are

  • AgentRadius
  • AgentHeight
  • AgentCanJump

I donā€™t know what the first two are responsible for, but they have something to do with what the pathfinding agent can use to modify the path. I believe they both essentially form an area around the character as a pathing reference.

For starters, I know that there is no plausible way to turn the speed in your favour - youā€™ve done everything you need without including any waits, so the bottleneck is the time it takes to compute a path. Part of this computation expense may come from how long the path is. We thus need to think of movement differently.

Hereā€™s what Iā€™m thinking; instead of relying on PathfindingService to get an enemy to a player, split the action. Continue using the same method to find the closest humanoid, but branch here. Set up a condition that will determine how the enemy moves to the player.

  • If the player is in direct line of sight with an enemy and is not obstructed, use MoveTo and send the enemy directly to the character. Use a raycast to check for obstruction between the character and the enemy.

    • Remember that long rays are expensive, but firing off short rays quickly will not hurt speed. You can send off an obstruction ray for the character and to determine if an enemy should jump. Two at the same time.

    • Typically to handle jumping, especially if you have stairs, I kill two birds with one stone. The two birds are collision-clipping issues and pathfinding, the stone being an invisible brick placed overtop of the stairs diagonally. This way, instead of relying on stair parts for collision, youā€™re actually traversing an invisible angled brick

Since pathing computation is noticeable, we want to handle that ā€œin the backgroundā€ or when it is required. We then want to look to inexpensive methods of moving an enemy around if we save the computation for needed cases, so instead we call MoveTo straight on the target point which takes no cost at all.

That should alleviate all your speed problems. Jumping may still be an issue worth looking into once all of this has been attempted and it doesnā€™t work.

2 Likes

Thank you, donā€™t mind the jumping issue that was very irrelevant to state in there but iā€™ll try to use the ray I have already created, and for the stairs I have placed a transparent wedge to support it, seems to be working averagely okay.

Right now iā€™m working on something different, so iā€™ll get into that issue again a bit later.

Iā€™ll update you, thanks!

1 Like

you can delete the extra stuff NPCS Uses,like if the npc wont have climbing disable it