loader image

NPC AI Control System

A C# system that works hand-in-hand with the AI system used to control NPCs (inspired by The Bureau: XCOM Declassified).

Scroll to
View More

mouse

As part of the Unidad Moribunda video game I was programming, I had to create an NPC system that was sufficiently maneuverable to benefit the player and not get in its way, but also sufficiently intelligent so as to make its own decisions when necessary. The rules were simple: it had to combat in an effective way against the player, which meant it could shoot his gun, search for ammo, melee, search for cover, and even be directed to noisily or stealthily kill a zombie.

The case for NPCs was simple: The game was story-driven, and we needed characters to be memorable and also be an asset to the gameplay; however, this was not a straightforward task regarding the AI. I decided to settle for a traditional FSM-based AI where the different actions would be logically “divided” into states.

First, I needed to create a control script that would handle player controls, input, and directions so that the NPC could follow the commands of the player. To simplify things, I would the NPC mimic the player’s stealth position (e.g. if the player was crouching, the NPC would crouch). The NPC did represent a real human and could be targeted by zombies, so it would search for cover automatically if the player crouched, and would also kill a zombie stealthily when commanded to kill one.

Using an FSM AI made it especially easy to integrate with Unity’s Mecanim Animator. I created several blend trees and combined a plethora of different animation variants for rifles, pistols, and other positions to make it as realistic as possible. The huge amount of blend tree variations lets the AI perform all its actions. The three types of AI I wanted to represent: are “Follow” (follows the player), “Guard” which stays in position and guards an area, and “Patrol” which would wander around in an area.

The end result was quite sophisticated and utilized the NavMesh for pathfinding while using only core Unity libraries and code written by me. You can check it in action here: https://www.youtube.com/watch?v=lzq_Xh_Cgg8