How to Create a Realistic NPC Eyesight System

,

as the distance of the target from the origin increases, the field of view boundaries increases. this is a directly proportional relation. as this distance D increases let there be two vector2 values, C which denotes the maximum boundary, and T which denotes the minimum boundary. let B be D divided by the maximum distance the npc can see, yielding a [0,1] value and the limitation is that the target must be in front of the origin.
let H be a value which influences how much B is multiplied by
let G be B*H
as B changes, C = v2(G, G), T = v2(-G, -G). you can take the relative position of the target from the origin and check if the targets position is within T and C based on the targets X and Y position

5 Likes

bruh its fine it doesn’t matter as long as it works and isn’t unreadable :derp:

3 Likes

It’s just a small nitpick. I’m not attacking them, I’m just sharing my thoughts.

2 Likes

(post deleted by author)

1 Like

(post deleted by author)

1 Like

if you mean generalized iteration, it is fairly new to luau, being introduced a year ago, and should be used over an iterator such as next or iterator function such as pairs

2 Likes

(post deleted by author)

1 Like

(post deleted by author)

1 Like

I thought this line should be FOV/2 and Guard = NPC.
But your code is great. Thanks.

if Angle <= FOV/2 and Distance <= Range and CheckLineOfSight(NPC.Head.Position, Character.Head.Position) then

2 Likes

(post deleted by author)

1 Like

Thanks for your feedback.
I see that:

  1. The main center line is the Lookvector of the NPC’s face.
  2. So the angle from the center line to the limits will be FOV/2
  3. Currently we are calculating the angle between the lookvector and the vector connecting the face of the NPC and the player’s face, so we will compare it with FOV/2
  4. Since the heights of the NPC’s head and the player’s head are different, I think it’s a good idea to remove the y component by multiplying the vectors by vector3.new(1,0,1) and then comparing in the xz plane would be more accurate.
    Here it is assumed that the NPC is always looking sideways (not looking up or down).

local NPCToCharacterFlatten = NPCToCharacter * vector3.new(1,0,1)
local NPCLookVectorFlatten = NPCLookVector * vector3.new(1,0,1)

local DotProduct = NPCToCharacterFlatten:Dot(NPCLookVectorFlatten)

3 Likes

(post deleted by author)

Wait can I ask,I want to check if a dead body/corpse is in the vision so I can make like withnessers. How do I do that?

1 Like

(post deleted by author)

(post deleted by author)

1 Like

Hello everyone! I completely rewrote this tutorial with better code and a cleaner layout.

2 Likes

Hi,
Can you provide a .rbxl or editable demo place?

Thanks

That defeats the purpose of following the tutorial!

3 Likes

Hey i want to tell you that This Eyesight script is literally useless in multiplayer,

The script stops looking to other players if it cant see the first or a player thanks to the “return”,

I would like you to remove the return and add the Check detectibles into them

1 Like

Hi,

Do you have a .rbxl file with a fix for the issue you mentioned?

Thanks