• Runtimes
  • [UNITY] Animation State Workflow?

Since mecanim isn't an option which is about the only thing I understand at the moment, does anyone have a concept of a working animation controller?

The Spineboy example is a pretty good one with its getting hit and AddAnimation methods, but I'm curious if anyone has a controller that would be more robust.

The requirements I can see needing are Idle, Walk, 1H Attack, 2H Attack, Cast, Block, GetHit, Death just off the top of my head and the only way to do this is write in full logic in Update() to set bool for states?

I see many ways to try to perform this:
1) Set a bool isBlocking in script, when a player is supposed to block set it to true and in an update loop always be checking for isBlocking and if ever true do SetAnimation to Blocking and then set isBlocking to false with an event set in the animation when it ends?
2) Manually just make a method void Block() which does SetAnimation to Block and isBlocking true with the same event that tells when the block animation is done and sets isBlocking back to false, but this way it is all just procedural calls to specific "Trigger" animations (same idea as an attack animation).

If player A hits MyAttack which is a 1h melee swing animation and they are blocking, you would simply AddAnimation() to the skeleton so that they would immediately perform that animation when they are done blocking?

Sorry if this seems like beginner questions, I am just used to the Mecanim workflow which handles all the state management and animation system.

I see needing to mix and match Update() loop game logic checks vs isAttacking, isBlocking etc which need to never be overridden.

Related Discussions
...

IMO, it's a good question. Beginners wouldn't even think of asking something like it, and prefer solutions that don't involve having to design reusable classes and OOP encapsulation and things like that. To be realistic, you don't need scalability and deliberate architecture for everything.

But seeing your move list, I have a hunch that you will need a little bit of it.
And it's true, there are a lot of ways to do it. You'll probably end up trying different things and seeing what works and what doesn't anyway, but instead of answering your questions individually, let me explain a whole recommendation.

A model-and-view setup is really helpful. This setup is kind of what Mechanim follows (for the most part anyway).
Basically: Mechanim actively watches values you tell it to watch (in the recommended setup) then decides what animations to play.

So in a Model-And-View setup.
One script handles the Model: It should encapsulate all the game logic of the character, interacting with game rules and the world and stuff like that, and keeping all the character's stateful variables. Ideally, this script should have NO IDEA that you're using Spine. It should not try to talk to Spine.Skeleton, Spine.AnimationState or SkeletonAnimation at all.

Another script handles the View: This is your own Mechanim. It should interpret and react to changes in the Model and show it visually, in this case by playing animations through the Spine objects. At the same time, the View script is aware of what Spine is doing so it only plays animations when and how you want it to, according to the state of both the character logic and Spine's animation. By placing this in a separate script, you can manage your animations better. And trust me, as your character has more and more moves, it can get really messy if animation logic gets mixed up with game logic. It also allows you to scale up to fancier things (like the transition stuff that Mechanim is capable of)

One relevant point about the View script is that (while it has different implementations), it's usually a change detector:
The View script should be able to tell when there's a relevant change in the character Model's state, and then play appropriate animations when that happens.

Change detection can be through (1) polling/checking bools and numbers on the Model while keeping track of the previous values, (2) the Model actively informing the View of any changes through callbacks or actual method calls to the View's methods, or (3) some mixture of both.
So to answer your question: yeah, it likely has an Update() that keeps checking if something changed, and interpret it accordingly a change is detected.
Another thing to note though is that your character likely has stable and non-stable states, with corresponding looping and non-looping animations. You likely have to handle those in two different ways.

Then you have to work out some kind of hierarchy for animations for the View script to abide by. This is equivalent to building the State Machine in Mechanim. For example, the logic can go: If the character attacks, play the attack animation once. After it's done playing the animation, let it check if the character is blocking. If he's blocking, just loop the blocking animation. If the character stops blocking, check if the character is walking. If he's walking, play the walk animation. If not, play idle.
It may be helpful if you draw this as a diagram on paper to map out how you want it to work, and then run the game and try getting your character into different situations to find unexpected, complex cases.

Just note, if you're an OOP purist, that especially in games, there will always be some intermingling between these two responsibilities (some people want it so that game-logical colliders follow what the image looks like, or timing needs to come from animation data. There are always pros and cons to how you wanna do these things.) How much of that "impurity" or violation of this separation of concerns you want is really up to you. Just know, for performance and maintainability reasons, that it's always there.

You should also hit up @Mitch. He demoed something for Spine-Unity that does some of the things Mechanim does. Not sure what happened to it though.

Thanks for taking the time to type out all your thoughts, this community has some great contributors and your post is greatly appreciated 🙂

Separating out the visible logic from the model logic makes good sense and seems like it would work, but like you confirmed it does sound like it has to be a mixture of things running in frame based logic and also direct call logic. In short, it doesn't sound like there's a real "just do this" answer and that was something I was needing to confirm to make sure I wasn't re-inventing the wheel before trying to design out my logic.

After looking at the Spineboy more closely it is a bit more clear that they are setting a hit bool and then only allowing movement once the current animation of the skeletonAnimation is not the hit animation. This is kind of what I was thinking, but I was actually considering setting events in all my "you can't do anything while this anim is playing" types of animations like Hit and Block that if active completely stop the ability to walk or attack.

I was also considering putting events in the Attack animations so that they could be interrupted by Blocking and thus never fire the event for attackAnimComplete and never perform the attack at all. Is that a typical workflow? We have some unique things in mind that could be simply driven by code like hitRecoveryTime where you could get bonuses to that stat which would possibly speed up the getHit animation and thus fire the getHitComplete event to trigger hit to be false again.

The "just do this" solution would've been for crazy-simple cases. You did seem to be looking for a more comprehensive solution that was supposed to stand in for Mechanim, which doesn't exist yet for Spine as far as I know.

For everyone else who might not be aware, animation logic can potentially be complicated as heck and it still requires you to consider what happens in your game specifically, even if you did have a system in place like Mechanim. Some NES and SNES games had animation state machines that were pretty extensive and look great.

Spineboy is a bare bones sample just to get things running for a beginner so they can get acquainted with Spine's API with minimum scripts and architecture, just like most Unity sample scripts. The API is more immediately discoverable that way. The only other reason you'd do it that way is if you know that the logic you need is really simple and not made up of dozens of cases to consider.

That particular line you described where it checks the animation to determine game logic. That's one way to do it. But especially for what you're doing, it sounds terrible to me and seems like it would break too easily and be too hard to balance. In some cases, you have to ask yourself: should the animator decide game balance things? Do logic changes have to go through the animator so they can be applied?

If you're meticulous enough about the timing and you're thinking of things like hitRecoveryTime, do that it in code. Just use code and timers and keep it within the Model, and make small helper classes to make this task easy.
Don't let the logic depend on the animation and animation events by default. It'll be easier to balance the game and make adjustments that way, especially if you're talking fast-paced stuff where every fraction of a second counts.

Of course, it'll become obvious to you if some implementation is too much trouble. If you can easily move Spine events around frequently and if doing it that way makes your code more readable, or if you don't care much about fine timing, I guess using events might be less trouble. You could always just try it on one character first then find out later if your code and workflow a huge mess or not. Some of these things can just be so circumstantial when you see the smaller details in actual implementation.

Thanks, I've decided to go with a full FSM model for states of the enemy and player separately to drive the animations instead of letting all the animations drive the game. It makes sense in some situations where an event for AttackComplete fires to trigger some visuals or possibly other things, but I think I have to agree that it should possibly not drive actual game logic. I was just thinking how easy it would be to only initiate an attack when the event from the animation fired so that it was spot on, but that might create some way too finite control over what should be a much more forgiving combat system.

When it arrives, unity 5.0 will support custom behaviour controllers, much like the animator controller for mecanim now.

With a little love, I'm sure someone could write an editor plugin which hooks it all up with Spine.

http://blogs.unity3d.com/2014/06/26/shi ... unity-5-0/

mr_malee escribió

When it arrives, unity 5.0 will support custom behaviour controllers, much like the animator controller for mecanim now.

With a little love, I'm sure someone could write an editor plugin which hooks it all up with Spine.

http://blogs.unity3d.com/2014/06/26/shi ... unity-5-0/

Yep. Thats what i'm waiting for. 🙂
(also already in the 5 alpha/beta, so yea. i'll handle it in the official spine runtimes when its time)

In Unity 5 you can create animation assets; StateMachines, States, Controllers, Layers, Blentrees, etc., using scripts in the Editor!

Honestly this is the biggest holdup; theres no API for adding stuff to a state machine until Unity 5 🙁

🙂 I thought you might be doing something like that. Nice one

I just want to say, that I wrote Playmaker Actions for Spine and with these you can create full and robust animation controller. See viewtopic.php?f=7&t=3288

Hmm, should I wait on building my own FSM with Unity 5 on the horizon? We aren't even in prototype phase of our game, so it seems well worth it.

Mitch have you had a chance to try to create a true FSM yet with U5? If you can attach a script to a node and it does all the magic for you that will be pretty exceptional so that you do not have to write a switch(state) and constantly update the transitions in and out 🙂

I haven't had the time to really dive into it yet heh; sorry. Unity 5 isn't slated to come out for quite a while - I'd roll your own solution for now.