Player Detection via Enemy AI

This is my first proper post on the forum, so I am sorry if this isn’t formatted in the correct way. Anyways, my issue that I am currently having is that I can’t seem to find a way to make a proper detection system for enemy AI. What do I mean by this? Well, if you take a look at the enemy AI from Breath of the Wild for example, you can see that when Link gets within the FOV of any enemy - within a certain range - a question mark will begin to appear above their heads. Just like the question mark above this Moblin: Screenshot 2021-06-04 192231

I am wanting something similar in my project, however unsure how you would detect whether the player is within the FOV of the enemy. At first, I thought you could use raycasting from the enemy’s head to see if the player is in the FOV of the enemy, but that only works for when the player is literally within the line of sight of the enemy; not the FOV.

Any help would be greatly appreciated! Thank you :smiley:

Use position and magnitude. You can compare the relative positions of two objects easily using .magnitude.

For example:

if (Object1.Position - Object2.Position).magnitude <= FOVvalue then

If you wanted to be even more precise you could probably also see if the lookvector of the NPC’s head is within an uncertainty value.

Good video I found when I first started out with this.

1 Like

That’s what I am planning to use to check how far away the player is from the enemy, however if I were to only use that, the enemy would be able to detect a player that it isn’t even looking at. I will take a look a lookvectors though, thanks!

Oh nice, I think that is exactly what I am looking for! Thank you so much :grinning_face_with_smiling_eyes: