@NeedHelp12 I'm making a FPS game where the player fights robots. I had a similar problem as you, and here's how I managed it:
First, I made use of Finite State Machines (FSM). I learned about FSM by reading this great book. When the robot is in the ‘Attack Player’ state, it's attacking (shooting at) the player. But it does more.. It casts a ray to the player to verify the player is still visible. It also continuously records the player's last visible position, and records the player's last visible velocity (direction).
Now say the player runs behind one of your obstacles. When the robot casts a ray to the player's new position, it comes back FALSE because there's an object in the way. The robot cannot remain in the ‘Attack Player’ FSM state, so it changes to a compatible state. The robot may do a couple things:
- Enter a ‘Standing’ FSM. It will stop where it is, stand and look around for the player, waiting for him/her to reappear.
- Enter a ‘Search’ FSM. It will walk to the last known position, then it will walk in the player's last known direction for ‘a while’.
- If the player is not spotted after ‘a while’, it may:
- Return to its original position, and reenter the ‘Standing’ FSM state.
- Enter a random ‘Walk Around' FSM state.
- Enter the original ‘standing’ FSM state at the current location.
- Enter a ‘Patrol' FSM state, where it walks back-and-forth from the current location to the original location
When a robot enters the ‘Standing’ or ‘Search’ state, I add some simple animations that move the robot's head and torso from side to side. Since the rays are cast from the robot's eye, this means it's likely to see the player, even if they're hiding in a corner.
Besides FSM states, that book does a great job describing how to use messages in games. In my game, every sound (footstep, weapon firing) results in messages being sent to all robots in the game. The message basically say , ‘The player made a sound at position XYZ’. Upon processing the message, the robot decides what to do: it may use position XYZ and enter a ‘Search’ FSM if it's close enough to hear the sound.
-Scott
PS I remember the author of the book has a website where you can read it all for free!