Joaquin De Losada

AI State Machine
Hierarchical Finite State Machine (HFSM)
2023
C#/Unity
New Alert and Searching States: An alert state was introduced to create a brief delay before the AI identifies the player as a threat. This offers the player a tactical advantage and more immersive gameplay. The searching state improves AI responsiveness by adding layers to its behavior.

FSM to HFSM Refactoring Challenges: The migration required restructuring due to multiple references to the FSM manager. A utility-based trait system was updated to accommodate both FSM and HFSM implementations, ensuring appropriate script checks and behavior adjustments.

Enhanced Parent State Responsibilities: Vision checks and awareness logic were centralized to parent states (non-combat and combat), making them responsible for timings and player detection ranges. This mimics human-like behavior by expanding awareness as danger becomes apparent.


Intro:

This post extends my previous project, “AI Finite State Machine.” After a few days of completing the project and conversing with a few friends about it, I realized that there were parts of the AI that I could improve by making it a Hierarchical Finite State Machine (HFSM). So, after some planning, I sketched out a new state machine that would implement the states I had already created as well as two new alert states and make the entire project in a Hierarchical Finite State Machine.


Changes:

The main changes made to the AI was to move most of the vision checking/information to the parent states so that each parent state was in charge of its timings and distances for looking for the player. As the player transitions from the parent non-combat state to the parent combat state, the AI will become more aware of the player and use its “senses” to look for the player. Most people might do this when searching and attacking a person, as in the beginning, they would be observant of their areas and look in a shorter area. But as they realize an enemy is nearby, they will look at a further distance, allowing them to keep track for longer.

Another major state I added was an alert state as well as a searching state. The main work for this group was adding an extra layer of searching for the AI. This would allow a few seconds before the AI initially notices the player as a threat. This is meant to give the player a slight advantage to get into a better position or start attacking the AI first to make the player feel faster.

If at any point during this alert state or being in the patrol state, the player is to shoot or another AI in an audio range is heard, they will transition to the aggression state (Combat parent state). This will lead the AI to return a utility value sent to the director AI to determine which ones are meant to attack or run to cover.


Challenges:

One of the main challenges when refactoring the project code from an FSM to an HFSM was the fact that there were multiple instances of calling the main FSM manager script. This is because, in the original FSM, I included a basic trait system that would increase or decrease the utility value when determining how willing the AI might be to attack the player. The solution I came up with was to check whether or not the AI it was fitting was an FSM or HFSM and then check the appropriate scripts.