Animator State Machine AI

I'm polishing the Richter boss fight in the Night Market. I've achieved a decent AI by combining her animator state machine with her AI logic, augmented with external param-settings sensors and state machine behaviours. No external Unity assets required, and everything is modular.



Why

First question: why on earth would you do this, why not script it? Why combine the animator and the AI states?

The AI is gonna be a finite state machine one way or another, we're not making neural nets here AI behavior decisions largely depend on which animation state it's in Visually editing the AI state machine is faster, easier to read, and easier to maintain than scripting a state machine I can make another boss fight with the resources here without writing a single line of C#

How

Here's how I implemented it, in 5 steps.





Step 1: Prototype the state machine on paper with the fight in mind. I want this enemy to be punish-based, so they're going to remain ~3 tiles away from the player if they can. If the player enters their zone, they'll either dodge or throw out a quick counter. If the player is still near them after a spacing modification, they'll throw out a larger, more mobile attack.

In the end-lag after an attack they'll be staggerable, so if the player can close the distance with a dash they'll be able to get a good combo off.





red arrows are randomly chosen transitions

the "maintain spacing" sub-state machine

Step 2: Without making any of the actual animations, create the state machine in Unity.









Step 3: Hook up sensors that will set the parameters you need. The implementation of said sensors is left as an exercise for the reader.





this is the second iteration of the boss, we don't talk about the first

Step 4: Add state machine behaviours to track simple things like movement and player orientation. Not shown, but I also have a "random transition in state" that will, you guessed it, instantly choose a random next state to play from a given list.





Step 5: Animate. I set fine-grained properties like staggerable frames during animations, instead of at the state level via state machine behaviours.





Step 6: Playtest and keep iterating :~)