How to prevent NPCs from going off edges/going up steep slopes?

I’m making an RTS game, and I use some basic bodymovers to position the units. There are some cases where I would want to make a temporary barrier when a unit is about to move into water, steep slope, cliff, etc. What would be the best way to do this, considering there may be hundreds of units in the game at a time?

1 Like

Can’t you just make an invisible barrier? Use collision groups to make the player and other items not collide with them?

I suppose this could be an option, but it would make maps take a lot longer to make. I suppose I could make a script that raycasts around the map looking for slopes/cliffs and adds in the barriers. The issue with this though is that some units would need to be able to traverse those areas while other units would be prevented. this could be done with collision groups, but I already have units set to a “units” collision group for other purposes, and you cant have multiple collision groups per object.

The collision group issue could be solved by including a second part welded to the model however. This already exists in my system for other reasons and it does not have a collision group set, so that could work. The only real issue with this method is the fact that it would have to be done for every slope, cliff, and body of water on the map, which could take quite a while. It would also mean that player generated maps would be impossible. As mentioned above, a script could be made to automate this process during runtime, but i am not sure what the best way would be.

One way would be to run over the whole of the map with raycasts, creating a 1x1 part everywhere a slope is detected. To get accurate collisions, this would have to be done on a large scale and would take thousands of parts for complex terrain. This may not be too big of an issue as they would all be anchored, but a game of this scale should be made with optimization and efficiency in mind. there could potentially be made a system that combines bordering colliders to reduce part count after the operation

Are you using paths? You can set materials as costs in the set up and they will not walk on that material. Then it’s a matter of placement of the “blocked” material part. That can be changed as needed in real time also.

1 Like

You’re probably should do what @PiercedMissile says, surely the easiest solution, but I’ll still say some other alternatives that come to mind.
If you’re using roblox Pathfinding Service you can use Pathfinding modifiers to make them avoid certain places, if you wanna go full on control you could even make your own pathfinding (something like A* for example)

1 Like

I am not using pathfinding at the moment! That is definitely something I want to implement if possible, but I am unsure of how I should do it at the moment. Standard Roblox pathfinding isn’t designed to work with large numbers of NPCs, so if i used it for this purpose i might run into performance issues, or it could just periodically stop working altogether (im not certain, but i think it might be rate limited). I have considered the possibility of batching clustered units and drawing a single path for all of them, but that does pose some of its own problems

I have actually made my own pathfinding before in another game! It was designed for 3D spaces, and worked decently well. It also struggled with large numbers of NPCs, however. Perhaps it could be used in this scenario because it would be working with 2 dimensions instead of 3, but I remember A* being a little expensive to use en masse.

Pathfinding itself turns somewhat expensive when a lot is needed, it can be optimized at the cost of precision by making the nodes farther from each other, i believe you could also make NPCs follow each other if they’re near each other and heading to a somewhat similar endpoint

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.