Hi guys. I have wondering a bit lately about the best way to sync my AI state and animations.
So my current setup is like this. I have a an AI state machine that performs all AI logic. I then have a graphicsHandler script listening to the events from the AI and setting animator triggers. The animator plays the animation and has a transition back to idle when the action is done. I am having some problems though syncing up the AI behaviour and the transitions. For example if the animation isn’t done before the AI goes into a “moving” state then the object moves while still playing the previous animation. I am trying to solve this by listening to animation events, but I feel that even last frame events don’t line up perfectly with the end of the animation.
Should I maybe couple this differently by not transitioning at all between states in the animator without explicit trigger from the AI (except for different animation states that happen in the same AI state)? So for example listen to the end of animation trigger, set the AI state and let the AI state fire a trigger that causes the transition in the animator instead?
As one other implementation I see in this Brackeys tutorial that he just lets the animator control everything by overriding the state behaviours:
Does anyone else not really like this approach? I feel like it gives very tight coupling between the animator and the AI in the wrong direction, I would rather have my AI control the animator then the other way around.
I agree with your assessment. I prefer to use animations as “display only,” giving them no control over my program flow.
That said, animation events (and state change events) are pretty powerful, and if you give in to using those, you should be able to get precisely the right behavior in all timing cases, as long as you update the event position when you change the animation.
The only other viable alternative is to hand-track the length of animation states and have your AI manage its own timer and switch states in correspondence to the displayed animation. It’s yucky and requires maintenance, but the good news is it will never fail, and I like never fail.
You could automate the maintenance of these time intervals: have a editor-only codegen step that grinds through specific animations you desire to know the length of and produces a public static class containing all the clip length times, which your AI code would be written to use. Definitely worthwhile at scale, but for 3-4 animations, probably not.
Thank you for your reply Kurt. I feel my problem is that I use a 2D sprite animation that is playing frames relatively slowly and the event on the last frame is not the true end of the animation causing the GameObject to move just before the animation ends, which feels off. I guess I can look into moving the event causing the AI state to change to the OnStateExit of the animation?
Ok in this case it turns out the problem wasn’t the end of state event but rather some other animator settings that were not correct. Thanks Kurt, It’s still really useful information! Sometimes I just need a sanity check to see if the way I am implementing systems actually makes sense.