Introduction

          Using Unreal Engine 4 in C++ and Blueprints, I created my own stealth AI that uses a state machine to interact with the environment around them. Here is the architecture and states that I used to make this enemy AI

State Machine Architecture

          Each state starts first being inherited from a base class. Each state then gets an OnEnter, Update, CheckForTransition, and OnExit, where all functions get the AI controller and controlled pawn passed through them. For the CheckForTransition function, a Boolean and new state needs to be returned just so the state machine can be notified if there are any changes to the states.

States
Patrol

          For this state, the AI pathfinds to a given point. The developer can use a spline to further define where the AI goes inside of the level. At the end of each point, the AI stops for a given amount of time before it advances to the next point. To achieve these behaviors, I split up the two behaviors into another state machine so that the main state class would not become cluttered. To transition to either the search or shoot states, it first needs to see the player in its detection cone (which can be defined by the user) for a user-controlled time period.

Search

          For this state, the AI gets a point where the player was last seen and wanders around in a developer-controlled radius until either the player is found or they give up and they move back to their patrol route. Unlike patrol, the AI does not use any extra state machine to figure out where it needs to search.

Chase

          For this state, the AI knows where the player is and is now actively chasing them. If any AI goes into this state, then all of the AI in the level do so as well. When the player is within range, the AI will automatically fire at them. However if the AI is out of ammo, they will then reload their gun. Much like the patrol state, these actions are broken into a state machine inside of this state. For the AI to get out of this state, all AI's in the level will not be able to see the player. For this sight check, the cone arc is a whole 90 degrees, instead of being something smaller like 30 degrees.