• Register
Forum Thread
Poll: Is raycasting a good way to give NPC eyesight? (7 votes)
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.

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.

Vicci-Games I'm Nervous
Nov 2 2019 Anchor

Physics Raycast

Your NPC GameObject will need a component that is shooting a Physics.Raycast in the vector3.forward direction; you will need an out parameter to register what has been hit.


Some developers use triggers by adding a mesh that would become the area of the NPC's sight. The mesh gameObject will then have a script that uses the callbacks from OnTriggerEnter() and OnTriggerExit(); Recommended to disable the Mesh Renderer for obvious reasons.


The most absolute simplest method I can think of is calculating the distance between the player and NPC. Not necessarily "vision" but you can use this method to implement the ability for an NPC to sensewhen the player is around.

You've probably already solved this problem, I just want to type something.

Much love.

Also realizing that I didn't read the full original post lol.


We are expecting the beta for Immunity to launch around November 10. Join our DIscord to be the first to know when it launches.


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.