Introduction:
I’ve extended the zombie game codebase by implementing two additional states to the AI Agent which include “Searching” and “Attacking”, bringing the total number of states for the Finite State Machine model to 5 and enhancing the functionality associated with the “Wandering” state to favor the closest available waypoint rather than follow a linear progression of the data in the waypoint game objects array. The functionality associated with every state presented from the tutorial has been overhauled in favor of extending the complexity of the FSM model and improving the player-to-agent interactions.
Summary:
Coroutines: Unity coroutines are adopted throughout every state to customize the functionality available to the AI Agent in each state.
Animations: New animations are added from Mixamo for the additional functionality that has been integrated into the updated FSM model.
Stats: Player & Agent stats system have been implemented for tracking each entity’s health and agent’s view angle and visibility distance.
Crowd AI: Functionality to alert nearby agent when the player is spotted is implemented allowing for simplistic crowd AI behavior.
States:
- Wandering – The agent travels to the nearest waypoint computed at startup and finds the next closest one each time the current is reached. When the player is in view of the agent, determined by checking the angle between the agent’s forward direction and the direction to the player against a custom view angle, a raycast check is issued to locate if there is any object obstructing the ray before it reaches the player. When the ray reaches the player with no obstructions, the player’s transform data and a direction towards this transform’s position are saved and issued to nearby agents which transition into the “Chasing” state and attempt to catch help catch the player together.
- Chasing – The agent’s target is set on the player’s transform and is updated every time the coroutine restarts by checking the custom view angle and raycasting for any obstructing geometry. When the agent reaches a specific distance close enough to the player’s transform, it will transition into the “Attack” phase. The player can outmaneuver the agent if moving out of range where the agent’s visibility distance extends to or moving behind geometry where the agent is unable to successfully track movement. When the agent loses sight of the player, it will transition into the “Searching” phase.
- Searching – The agent travels to the last seen transform position of the player and checks whether the player is visible using the custom view angle and raycasting for any obstructing geometry. If the goal is obstructed, whilst looking for the player’s current transform position, a counter will increment and upon reaching a specific value, the agent will switch to the “Wandering” phase.
- Attacking – The agent stops in its current position to play the attack animation and resumes back to tracking the player through the last seen transform position. A view angle and raycast check is performed to instruct the agent if a transition to the “Chasing” or “Searching” state should occur.
- Dead – The agent’s interactive components are disabled and the ragdoll mechanics are enabled. The object representing the agent is destroyed in a specified timeout.