So one of the tools in my toolbox that hasn’t been touched in a long time and keeps being a source of pain is how to manage animations over numerous character models. I’m looking to refactor it.
One part of the refactor I’m pretty solid on and another part not so much.
So the first part solves the problem of managing different state machines across a variety of characters in a way that doesn’t drive me crazy. Basically I’m moving all of the configuration into classes where I can use inheritance to define everything in a sane way, and then generate the actual animation state machines in code.
The second part which is actually triggering animations and controlling when they start/stop I’m looking for how others have tackled this. Currently I’m using triggers. I have a SetAnimation method that takes a trigger and an optional duration. I just call SetAnimation pretty much every frame to whatever the current animation should be. If SetAnimation is called with the same animation id that’s currently playing I early out so no real peformance hit for calling it that often. if it’s different I fire the trigger.
That approach seems to work ok, but not sure if there might be something better.
In previous projects, these were the layers in my character control system:
Control logic (player input or AI)
Virtual Controller
Character State Machine
High-level Animation State Machine
Animator
For a player-controlled character, input filters down from the Player Input Module to the Virtual Controller to the Character State Machine, which determines the character’s state (crouched, falling, reloading, etc.).
The Character State Machine sends control signals to the Animation State Machine (e.g., play the reload animation) and receives events (done reloading).
Finally, to get to your question, the Character State Machine and Animation State Machine handle all world interaction (am I grounded?) and model state (when starting to reload, put a clip model in the character’s hand; when done, hide the clip model).
This allows the Animator to be as simple as possible. It plays all animations in a vacuum, with no knowledge of how the model is actually interacting with the world. I use a combination of trigger-based transitions and, mostly, Animator.CrossFade() to minimize the mess of lots of transition arrows, especially when changing to different sub-state machines.
So thought I’d share this both to get feedback and maybe it’s useful to someone.
So one thing that that has always bugged me is having a bunch of integers defined via StringToHash in your controller, or worse yet actually calling stuff using strings from all over the place in your game logic. And managing animations on different layers was always kind of a pain.
So I whipped up a script where you can define enums for your animations and layers, and use those in your game logic. The script lazily creates the hash id’s and caches them. It also tracks which animation id is active per layer. Not fully fleshed out yet but better then what I had before.
This particular script assumes you are using triggers, and relies on using a naming convention where your enum names match your trigger names.