First of all, you need 2 points, one is the head of the player and one is the head of the bot (in the simplest case).
In the code, I will call thoses 2 points respectively player_pos and bot_pos, you also need the direction the enemy is looking (bot_rotation). All three are Vector3
. The rotation will be in the same "format" as godot's Spatial
(doc) (YXZ-Euler angles)
Then, to have the FOV correct, you have differents solutions, those are two of them:
- Calculate a vertical and horisontal angles (code here). With this you can do the next step only if the angles are in certain ranges (and thus have full control over the FOV).
- Create a
Area
(3D) and do the next step only if the player is in this area (it allows for more complex vision shapes)
Once this step is complete, you need to do a ray-cast from the bot to the player using a RayCast
node (doc) or the space_state interface (doc). If the raycast collides with a wall or a terrain element, the bot does not see the player, otherwise it sees him.
If you want a place for the player to hide in like bushes in Assasin's Creed:
you can put an Area
(3D) around the bushs that collides with the RayCast and thus stop it like a wall.
There is also a great video from GMTK explaining how it might work in stealth games which is similar to fps game detection: link.
I hope this helps.