Making NPC path to line of sight

I have this soldier NPC than requires line of sight to shoot at a target, using a raycast. But what I noticed is that if there are multiple soldiers, they tend to block each other’s line of sight and push each other around, because they path straight to the target when they lose line of sight. So, how would I make the NPC detect the nearest spot where they could have line of sight with the player, and path to it?

1 Like

Well, you could always try blacklisting, but then you would NPCs shooting through each other, so what I would do, is use pathfinding to find a location where the NPC could potentially shoot a target, but lets say there is something in the way, then move them again until they find a spot where they do have a clear shot. Better yet, you could pathfind to get the NPC’s next location and just ray-cast from there, this way the NPC can go straight to its next position.

If you have issues where the NPC gets stuck alternating from one position to another, you can just create a table and log all previous positions.

you could compute waypoints to a path and raycast from each point until the ray hits a player. then pathfind to the position of that waypoint.

That’s the same thing as pathfinding until the ray hits a player, which I’m already doing.