
01
Visibility Test
Using the Raycast function to check whether NPC is within the player's line of sight. Different layers were set up to distinguish obstacles and the player.

02
Find Hiding Spots
To hide from the player, the NPC needs to hide behind something such as walls and doors.
- Physics.OverlapSphereNonAlloc function is utilised to find and filer all colliders touching or inside the sphere within a certain range
- NavMesh.SamplePosition is called to project the given point onto nearby navMesh instances along the vertical axis. As a result, the hit points’ position will be the world position on the floor
- Computing the dot product of the normal and the player direction to get a float which can represent the relative position of the player
- Cos90 equals 0. If the dot product is larger than 0, that means the player is in your front. So if we have a negative dot product, that means the position there is a potential hiding spot


03
Regrets and Possible Improvements
This game AI is using a state machine to control the behaviours. Behaviour tree could be implemented instead, since behaviour tree has a better performance. More AI behaviours could be added such as predicting player paths and evaluating path safety. Currently, NPCs may choose the path that would possibly expose their locations.