Ignoring parts with PathfindingService

I’m working on adding a new feature to my game that is requiring me to work with the PathfindingService. I have ran into an issue, however, that I’m not sure how to go about.

I have walls (black boxes) around my roads so that vehicles, which also use the PathfindingService, can follow and stay within the boundaries of the path.

I am planning on adding a character in which the player can control. This character will also be using the PathfindingService but I need it to be able to ignore these walls so that it can walk off-road.

Is there any way to do this?

1 Like

There is no way to ignore parts from a calculated path with PathfindingService, unfortunately. The workflow seems to be concrete; a path is generated between two points with modifications for the agent in terms of radius, height and if it can jump.

You can probably experiment around with the AgentRadius to see if that does you any justice. If not that, I don’t quite have any other answer other than a custom pathfinding implement (which is far beyond me).

Do you think adding a gap under the walls and making the height smaller might work?

I’m not too familiar with the path generation from pathfinding, so the best answer I can give personally is to try it out.

The Navigation Mesh may be of use to you. It essentially determines boundaries for paths; any path created will stay within the NavMesh. I’m not too sure this will help though; this only shows where paths will stay within and nothing more.

The Navigation Mesh is available through File -> Settings -> Studio -> Visualisation.

1 Like

So having the path go under the walls was an issue. The path that needs to follow the walls sometimes thinks it can go under. I tried going OVER the walls and that seems to work a lot better. I disabled jumping for the path that must follow the walls.

The only problem I see is that the AI decides going around everything is a lot easier than going over each part.

I’m not too familiar with pathfinding, but using your gap idea may work. If you set the AgentHeight to be larger than the gap you create for the vehicles and then smaller for the characters, maybe I’m suggesting something you’ve already tried though.

Another idea you could use would be to only have the walls locally for each player when they’re in a vehicle so the pathfinding will account for them, then you can remove them for the pathfinding of the characters. If there’s pathfinding for both vehicles and characters at the same time then maybe you could toggle whether the walls are there or not when determining the path. Providing the part count isn’t enormously high, and they’re completely transparent then this should suffice without performance issues.

A custom pathfinding implementation would be a perfect solution to this. In fact, your current situation makes it much easier since your cars need to follow a strict path with little to no deviations (crossings would be the only offenders). If you decide to make a car follow the same path the entire time, you’ll only have to check if there is another vehicle or a character in front of it with Region3s; otherwise, you could just make nodes have different weights and use A* or even Dijkstra if you can’t figure it out (Dijkstra only has issues with large maps).

Dijkstra is an algorithm that floods your node map until it reaches the target to check which path is the shortest. It’s far from good as it goes over areas that don’t even need to be checked, but it’s often used in other algorithms that require data about the entire map like flood field, where it doesn’t have a target and instead stops running when it can’t spread.

A* is excellent in cases where the map is variable. If a wall can block the passage in one case, or if you want to avoid traffic jams in your case it is the optimal choice, but it can be replaced by algorithms that use a pre-processed path list if none of that happens.

One of such algorithms is “contraction hierarchies”. It just simply generates all possible routes and checks which one of them is the shortest. As said, it is optimal for static maps, but useless in variable settings. It is also often used in combination with flood field to avoid having to flood the entire map, instead dividing it into static sectors. It’s also huge in memory consumption (a map sized 10x10 would require 99 paths for each node, making it 9900 possible routes to store).

A relatively easy solution (although probably not preferable) would be to create a temporary copy of the entire map to use for pathfinding and delete any parts you want to ignore. You can copy everything, make it invisible, and move it to its own designated location for pathfinding and then just translate the path points back to the real map. It’s a hacky solution I guess, but it’s the easiest one that I can think of.

8 Likes

Adding onto what I said, considering your game is probably some kind of a city building genre, your nodes would be placed in crossing and connected depending whether there is a road between them. The best solution would be a pre-processed map, you would update the map every time you change the roads, while the path from building to building would be calculated by checking what road they lie on.

To generate the list of paths, go over each node, check their neighbors, if they were never reached for the current node before, add the path to the list. The path has to contain the nodes in order from the first to the last and its length. After that, to find which path the vehicle has to take, you should get the 2 nodes that are connected by the road and find the closest one to the building itself and get the path for that node. Once the vehicle reaches the node (on the same road as the building), just make it reach the closest point on the road to the building.

This one I endorse. It’s relatively simple and works great in terms of “ignoring parts”, without needing to create your own pathfinding system.

Currently roblox’s pathfinding API is actually performance-efficient, the only issue is the lack of customization.

1 Like

It’s been a while since I worked on this because I got frustrated with it and started on something else.

This worked :smiley:

image

2 Likes

Can you show me the AI script?I’m not understand about how you du it.