- Editado
Mecanim FSM + Skeleton Animation Example
I got bored today and thought since not everyone has NodeCanvas that I would put together a small example of how Mecanim can be used as an FSM tool to control Skeleton Animation (NOT Animator) by using State Machine Behaviors. I usually prefer programming everything out, so getting this working on a very basic example so easily was surprising.
Here's the video and attached project. Keep in mind this is stripped down very bare and doesn't make use of public variables which make it easy to expose drop down selectors of spine animations. That would require a bit more work, but it "appears" easily doable and with the free built in tool.
In case people aren't familiar with Mecanim, it's basically a system you feed inputs to and then create transitions from based on the current state of those variables. If you change your movement speed to .5 and are in Idle currently and Idle defines a transition to Run with movementSpeed >= 1, it simply moves to that new state.
The code on the right at the end shows the behavior I place on every state in the FSM.
I'm horrible at explaining anything, true coder with no capacity to communicate so if you have questions or just want to know what's going on feel free to reply.
Thank you for that dude, I'm totally new to Spine but I've been working with Mecanim quiet a long time and I'm really happy to see that it can be used with Spine.
Have you pushed your test further ? Any downside that I should be aware of ?
And just to be sure, you're not working with the skeletonAnimator here right ? So you really just use mecanim to "trigger" (set your skeleton state, i think that's the proper term for spine) skeleton animation (and no animation clips) ?
thanks again
Correct this is the SkeletonAnimation, NOT Animator which creates unity animation clips.
I'm already using nodecanvas to do this so haven't pushed testing further, but I imagine the only limit is how well you can code and how good you are with mecanim if you need to add layers etc. I don't see any limitations, it's a visual FSM.. if anything hard coding it out is more limiting.
The main issue with Mecanim is that it uses unity's serialization for saving and updating values - so if you write your own custom StateMachineBehaviours, then one of the has a compile error, you lose all of your serialized data. T_T
You saying any script you ever have a compile error on you lose all your serialized data on even your own custom classes? That's not normal behavior if so.
Majicpanda escribióYou saying any script you ever have a compile error on you lose all your serialized data on even your own custom classes? That's not normal behavior if so.
Thanks for a good demo.
I purchased Node Canvas just for this, but still trying to learn it.
I got to the point where I generated animation clips and formed animator mecanim as I desired my character to have.
However, I did look into the shared files on this post, but I still quite can't figure out a few things.
- How did you make the unity Animator communicate with NC FSM?
- Is it mecanim or FSM actually firing the setAnimations to SkeletonAnimation? Or is it something else?
- In my attempt I called in methods with FSM via implemented action, but not quite getting the smooth results as yours.(a sample could be a ton useful)
In my current version of player character animation controller I have few issues with how I was trying to solve it myself so far. For instance I have set up my animation clips like; jump -> transition_jumpToFall -> fall -> transition_fallToIdle all in separate animation clips. I'm not quite getting where I want it to be just with if spaghetti-ing and firing up animation with setAnimation. Any tips?
Hey Affinity, this post was for those that do not have NodeCanvas. You do not combine Node and Mecanim.
If you use this example, you would still instantiate your animations as SpineAnimation not Animator. This one allows you to place a behavior on each mecanim node.
Did you just mean to reply to Spine + NodeCanvas? by chance?
Majicpanda escribióHey Affinity, this post was for those that do not have NodeCanvas. You do not combine Node and Mecanim.
If you use this example, you would still instantiate your animations as SpineAnimation not Animator. This one allows you to place a behavior on each mecanim node.
Did you just mean to reply to Spine + NodeCanvas? by chance?
Oh, I totally misunderstood when you said things about NC. The miscommunication was totally on me and my poor reading skills, so don't worry haha.
No, I wanted any input from you since you've done what you've done and I'm still intrigued to learn it.(Still need to figure out the magic of NC, but that's another topic. It really lacks clear representation to visual scripting newbies like me)
I still want to know what really went through your demo, though. I tried to figure out myself with your attached demo, but couldn't.
If this method you used to utilize both mecanim node and spine skeleton animation at the same time that's what exactly could help me out with my transition animations being really turdy.
Also, one of the problem I had trying to check out your demo was I couldn't figure out why your animation behaviour script keep giving me syntax error. if you ask me now I have to re-check it since it was some time ago, but yeah.
Sorry looks like I put some transitions in game that simply don't have animations to go to, so it gets super pissed off about that
So to break this down a bit more.. if you were to call any of this from code, you would be setting horizMovement and vertMovement based on your character controller.. hopefully that part is easy. You definitely need to already know mecanim before using this example.
Any State -> Attack, this would be called if you used animator.SetTrigger("attack"), then once that state enters it would play your Attack animation and the Behavior attached to this state would exit automatically because it is not set as loop in the inspector of that node and has an exit time of 85% into the animation. This sets a trigger called "transition" to fire which is simply the condition on the transition from Attack to Idle if you click on the transition line.
Are you getting a compile error or just runtime? Don't click the getHit trigger, it's just there as an example.
I definitely don't use nodecanvas anywhere in this attached example. The Node example is in my other thread and it does not use mecanim.. they are totally separate ways of implementing SpineAnimation.
Hi @Magicpanda !
Track entry has been updated, any chance to know how you updated your scripts since that happened ?