# Multiple character sprites using one animation controller

Update 2015.02.26 So, this post is actually a terrible way to go about this. When I was going down this path, I wasn't aware of the built-in ways to handle something like this. Namely, you just need to create a single character's animator controller and animation clips to feed it. Then you can create an "Animator Override Controller" that derives from that base animator for each additional character, and simply replace all the clips with unique ones. Part of this blog is to document the journey through my first Unity project, and, well, this is one of those learning experiences.

If you have several characters that your players can choose from, but all share a similar tree of Mecanim states and animations, you can use a single animation controller for all of them. Note that part of the process requires creating layers to "sync" to the master layer, and this is a Unity Pro-only feature. Here's a quick example of some common player states, and how the process works:

With your animation states set up, make sure that you have created animation clips for each character for each of the states. For example, for the "player_fire" state, you would have animation clips such as "character1_fire", "character2_fire", "character3_fire", etc. Have your first animation clip assigned to the state above.

With the first character all set up, simply add more layers in the animation controller for each character that you have. In the other layers, make sure you have "Sync" enabled (Pro-only feature). This syncs all the states with the top master layer, so all layers share the same states. Then you can simply click a layer to edit, and for each of the states, assign a different animation clip. In this example, I've added 4 other characters:

The last step is in your code. You can loop through all the layers in your animator, and set all their layer weights to 0 except for the chosen character, which should be 1. Here's an example function that does this:

void SetCharacterLayer(int characterLayer)
{
Animator animator = GetComponent();
for (int i = 0; i < animator.layerCount; i++)
{
float layerWeight = (characterLayer == i) ? 1f : 0f;
animator.SetLayerWeight(characterLayer, layerWeight);
}
}