website_badges_steamwishlistAlpha Accesswebsite_badges_steamwishlist

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:

animationStates

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:

animationLayers

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);
	}
}

2 responses to “Multiple character sprites using one animation controller”

  1. alecksia says:

    Just wanted to write that this blog post helped me (especially your update at the top!). Stumbled upon it and recognized the studio from playing Invisigun Heroes last week at the Unite conference. Super fun!

    • Shadi says:

      Hey, that’s great! I’m not sure if we had a chance to chat or not at Unite – but thanks for stopping by and playing. I’m glad I updated this post a while back with the proper, convenient, built-in method. There are plenty of times where I’ve written code to replicate features that already exist. Unity’s pretty deep and it’s not always easy to know what you can and can’t do.

Leave a Reply

©2019 Sombr Studio LLC