How would I have an NPC detect if a player/object is in sight?

Hey, friends! So I’m wondering how to detect if a player is in sight of an NPC. Raycasting directly to the player and raycasting forward both don’t work. Raycasting towards the player means the NPC can see them from behind, and raycasting forward only works if the player is directly in front of the NPC. I am wondering if it is something I could work out with raycasting? Or maybe even a camera? All support, feedback and answers are greatly appreciated!

1 Like

So one method I like to use is the DotProduct method, essentially you get the two look vectors of two objects and compare them using Direction1:Dot(Direction2)

It should return a number between 1 and -1.

Then you can raycast at the player constantly to check if they’re in sight or behind an object.

A very talented roblox scripter who explains this best is Okeanskiy:

While he doesn’t explain the raycasting part, he does give you a good idea on how to go about your ‘NPC detection.’

Hope this helped a little bit, and good luck!

5 Likes

Thanks! This helps a bunch. = D