AI Finite State Machine
Release Date: April 2023
This project involved designing and working on a Finite State Machine (FSM) in Unity. This allowed me to learn the entire design and creation process for working with an AI system for a game. This version of the FSM contains five (5) main states: Patrol, Idle, Aggression, Attack, and Cover, which the AI will transition to depending on whether it sees the player, proximity to said player vs the distance to cover points, and a few other internal reasons. Working on the FSM allowed me to gain a whole new level of understanding about correctly designing and working with an AI system for a game. Still, it has motivated me to continue working on this project, finding ways to improve it and working on other AI systems.
Your browser does not support the video tag.
The project began by creating a research and design document that helped structure what I had learned throughout the project and plan the AI system. In the PDF document below, I have a collection of the primary research I have been able to do, which includes navigation meshes, FSM, Behavior Trees, and Utility AI, as well as the design of all the states that the NPC would have and related trait systems for the NPCs.
Each NPC also has a standard methods script for movement and an AI State Manager for current state control and essential information. Separately, a GameObject named the Director AI manages the NPCs as a group, limiting the number of NPCs in the Attack state to maintain gameplay balance and other important decisions related to all NPCs.
The NPCs start in the Patrol state with assigned patrol and cover positions. Then, whenever the player enters visual range or fires a weapon nearby, NPCs will transition to an Aggression state. At this point, the NPC calculates a utility value to determine its willingness to attack or seek cover based on player distance and nearby cover points. A higher utility value represents a higher willingness to attack the player and transition to the attack state. The value is stored in the AI State Manager script, and the NPC is added to a list in the Director AI with other NPCs in the Aggression state.
The Director AI periodically processes the list above of NPCs in the Aggression state (Currently every 2 seconds). Next, it checks if the current limit of NPCs in the Attack state has been reached; otherwise, it will probabilistically select the Attack or Cover state with a higher utility value with a lower threshold for the Attack state. In the Attack state, the NPC approaches the player if necessary and fires its weapon, while those in the Cover state choose the closest cover point behind a piece of the cover relative to the player. Finally, if the line of sight is lost, the NPCs switch to the Searching state and go to the player's last known position. At which point, if not in range will return to the Patrol state.
Design Document
Github Repo