Enemy patrolling around last seen player location

Im making a horror game and want to make it so that if a raycast doesn’t detect the player the enemy begins to patrol the area it last saw the player. I have a simple pathfinding system where the monster just follows the player already but i dont know how to both make the enemy patrol the last place it saw the player and also how to make it use a raycast to see the player.

I would implement the following :
First I would create a chasing: bool variable in your monster that tells if you are chasing or not.
Then every frame :

  • get your position and the player position in variables
  • Cast a Raycast from the monster to the player. How to do this is explained here
  • Check if we hit the player, this can be done by checking if the property collider of the returned value from intersect_ray is the player.
  • If we hit the player, set chasing to true and store the position where the player is, it is the position your are now chasing. If you are using a NavigationAgent, set the target_position to this position.
  • If we did not hit the player, do nothing.

Now we handle the movement of your monster :

  • If chasing is true, let the NavigationAgent walk to the targeted position (last position the player was seen), or walk manually to the last seen player position.
  • Else, set your monster idle animation (random walking around, or standing)

If you are not familiar with NavigationAgent, you may want to read the navigation region and the navigation agent documentation.