How do I Make an NPC Find Cover?

I’m currently working on some artificial intelligence for a “soldier” and I need to figure out how to detect “cover”. I made a quick sketch on what I’m trying to achieve,

Basically I need to have this guy in the red,

recognize that there’s cover he’s able to use.

If you could offer me any help on how I could accomplish this, it would be greatly appreciated.

You could probably use Raycasts to create an algorythm which determines the smartest and most logical location for the NPC to take cover in.

The Raycasts can be used to determine if the NPC is in range, hit direction, and/or in danger of being shot at in order to trigger the NPC’s decision to search for cover. After the NPC determines that it in fact DOES fit the requirements, then you can begin to make the NPC look for cover.

In order to do this effectively, you can either create a folder that holds all the parts that can be used as “walls” OR you can use raycasts (which can turn out to be expensive depending on your case) to determine where the NPC can hide.

After finding a wall to hide behind and making sure that the shooter can’t in fact shoot the NPC from that angle, then you can move your NPC behind that wall using some CFrame math and possibly using PathfindingService.


The logic I have provided is mostly just a baseline to give you an idea on how to accomplish this but if you want the “best” results, then I believe that you should research on what serves your case best and how you can improve the overall intellegence of your NPC(s).

Remember that making an NPC “too” smart can become expensive and laggy!

Hope this helps! :wink:

4 Likes