I am currently struggling with detecting the player. It may seem simple, but with every tutorial I watch, the enemy can detect me through walls. I have used a raycast which worked, but the problem is the fov since raycast lines are so thin. What is the most simplest way to do this without complex math.
Detect the player using conventional methods that can see through walls, then raycast
- Detect the player by distance, enemy’s fov, any other criteria
- Raycast. But instead of raycasting in the look direction, raycast at the detected player. This still isn’t perfect, since you would only be detecting if the enemy can see a particular point on the player. So, do multiple raycasts. Make sure all of them make sense. Pick raycast destinations that lay on the player (so it would always hit the player if there were no obstacles). Cast a few, but randomize between frames. Eventually one of the rays will hit the player.
I suspect that the best solution is a few raycasts - set up a ray cast directly forwards, and 5 degrees to the left and right until you have reached the edges of the AI’s desired vision. Then you want to make it that he will only start moving when the ray cast hits the player, which can be done by setting up a variable like this:
public LayerMask playerLayer;
Assign the layer your player is on and then make it move when it detects a player.