- Home /
How to write a generic Mecanim Animation System in an EntityComponentSystem Architecture
I am currently creating a game which shall use Mecanim to animate characters and enemies. For this i want to use Mecanim and was successfull in my first attempts. But these included to write a script for each single animation i made because of the string references to parameters and layers within the mecanim statemachines. Since i am using the Entity-Component-System architecture to structure my code i would prefer to have one single System which handles all animations in the game.
Unfortunately i have no clue how to bring the versatility mecanim provides with its parameters, transitions and other statemachine stuff into one single system, which then knows how to behave properly for each mecanim animation i provide. What needs such a System to know? Is there code from someone who did it before? Is it even possible to write something like this?
Answer by Mmmpies · Jul 20, 2014 at 07:50 PM
Not up to speed with ECS but looking online it appears Unity is a partial ECS system itself.
I think it would be possible to put all animations in one Mecanim animator but unless every character behaves in a very similar way it will be messy.
You could set a bool for say isPlayer or isSkeleton or isZombie and have one script do it all but would you want your skeleton to walk the same as your zombie? If not then the animator would be horrific and not just because of the zombies and skeletons!
Really you'd be better having a prefab of each distinct enemy/player/NCP and build a script and animator for each.
I'm only playing with Unity for a hobby but for example I've got a player animator that has different layers for walk/run/idle, cast spell, blade weapon, bow weapon, blunt weapon and swimming. Not to mention any character interactions that I haven't done yet.
Then I've got an NPC that goes mining, if it mines enough it smelts into an ore bar, it goes to sleep at a set time and gets up at a set time as well as attacking. If I tried to add all this together within mecanim with my player setup it would be really messy but if I want another miner then I just instantiate another miner prefab.
I'm aiming for an Oblivion style of NPC's and Enemies having jobs and activities that are unique them so one animator/script of me would be harder to achieve than separate ones, however...
If all enemies are going to use the same motions then yep one script and one animator would be completely acceptable.
To have a script for every $$anonymous$$ecanim statemachine is what i would love to avoid but what seems to be impossible. That Unity is an ECS itself i do know even if it is not one whose architecture i like. But that is a topic of its own. I will do as you suggest but i dont like the idea of having lots of classes which basically all do the same: handling animations Nevertheless thanks you for your reply. It assures what i feared but finally i can go on :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613154500im_/https://answers.unity.com/themes/thub/images/avi.jpg)