Forum Thread
Poll: Is raycasting a good way to give NPC eyesight? (5 votes)
  Posts  
How to give NPCs eyesight (Forums : Coding & Scripting : How to give NPCs eyesight) Post Reply
Thread Options
Jan 9 2019 Anchor

I am currently creating an advanced npc system with c# in Unity. I am experimenting with giving the npcs eyesight so they can evaluate their surroundings. Currently the most effective method I have used is raycasting. I am concerned about how this would effect performance on a larger scale. Just a few npcs in the scene all shooting anywhere from 1 to 30 raycasts a second is ok but what about 50 npcs or even 100. Does anyone have any suggestions for how to give npcs eyesight. I will be posting some code snippets and gifs soon.

2018 09 12 9

Description of photo above:

The npcs is shooting 3 raycasts a second (blue rays) towards the player, if it hits the player then that indicates the player can see the npc. When the npc (small red person center of image) realizes it is in view of the player (green capsual) it shoots the yellow rays and then shoots a raycast from the player to the location the yellow ray hit. If the ray shot from the player hits a location within a close range to the yellow ray then that indicates the player will be able to see the npc if it tries to take cover there so the ray appears red, otherwise it shoots a green ray at the locations that are not within a close range to indicate better cover locations.

naret
naret code monkey
Jul 2 2019 Anchor

the simple solution is to check what the player sees. If a player sees an NPC, the NPC can react, if the player doesn't, then he won't realize if the NPC doesn't see him either. You can add raycast for the latter case, but it's nothing high priority that would affect the player/gameplay. You can prioritize these checks by the distances to the NPC or relevance for the player.

Checking what the player can see is usually done by occlusion queries. Modern engine have systems for that, "Advanced NPC Systems" sometimes have their own versions of that.

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.