• Register
Post news RSS Under the Hood – Mecanim and its Many Uses – Part Two

Part two of looking at Mecanim and how we are using it in Lithic

Posted by on

Hello again, welcome to part two of our Mecanim blog, and today we'll be looking animation layers and state machine behaviours. As was the case with the last Under the Hood, this blog is quite a bit more technical than our usual posts – I will however try to keep it as understandable as possible. I strongly recommend that you go back and read the first part if you haven't already, it's okay, I don't mind waiting.

Right, welcome back! For starters lets have a look at animation layers:

Layers can be used to combine animations, add extra animation on top of another (this is called additive animation) or to override specific parts of another animation.

The above image shows the animation layers for the Hu-Aan. The “Base Layer” controls movement and animations that will only be played when the character is stationary, such as gathering, eating/drinking from a source, sleeping and crafting. This works because the animation controller knows when the character is/isn't moving so can hold off on playing stationary animations till the character stops moving.

So what if we want the character to eat whilst walking? We could make an “eat whilst walking” animation and use that instead right? Well, yes we could... but we also want the character to be able to equip and un-equip items whilst moving too, and also have different arm positions whilst carrying an object too, and talk whilst moving... oh and search whilst moving too.

So, as you can see from my epic run on sentence that making an animation for every single combination of states can quickly turn into a huge pile of unnecessary work. This is where Layers really come into their own – looking at the image again you can see Equip/Eating/Search/Holding/Talking all have their own layers. These layers are set to “override” certain body parts of the character using what is called a “mask”, replacing whatever animations are on the layers above it with another. How much the overriding animation changes the base animation is set by the “weight” of that layer, which can be seen under the label for each layer (the grey bar). Higher weights mean more of the overriding animation will show through.

So using overrides we can have a character walk whilst eating and also have a conversation with another all at the same time, using only three animations in combination with masked body parts then setting weights on the override layers to stop the walking animation from playing on the arms and head.

In addition to overriding animation you can set Layers to be “additive”, this working in a similar way to override but the layer's animation is applied as extra on top of whatever other animations are currently playing. We've used this on the “Blink Layer”, where a simple additive animation which closes and opens the eyes is applied additively to the other layers. You have to be a little careful with additive layers, as moving body parts additively can end with arms and legs over-rotating and looking very broken so it's best to use additive animation in small doses, especially when affecting limbs.

If you're still with me, well done for getting through that bit! Next we'll be looking at State Machine Behaviours(SMBs), which are an incredibly powerful way of getting your animations to communicate with components outside of the Animator and simply affect parameters for within the animations themselves. SMBs are attached directly to a animation as seen in the below image inside the green rectangle.

This image shows the talking Layer and the inspector for the animation “StateSelector”, you can see the StateSelector animation highlighted in blue to the right of the picture. This animation serves as a hub for the other talking animations and has a SMB called “TalkStateSelect” attached to it. TalkStateSelect sets a parameter (discussed in the last Mecanim blog) to a random value between 0 and 4. The StateSelector animation exits to one of the 5 surrounding animations based on this number and a new number is selected every time the StateSelector state is entered. This is a great way to get animations to play randomly. The SMB script itself is very simple:

This script is set to execute “OnStateEnter”, which simply does what it sounds like it does: every time the animation which this script is attached to starts it executes the script. The script simply picks a random number between 0 and 4 as seen in line 8, then sets the parameter “Talking_State” to whatever number it picks, which can be seen in line 10. The parameter “Talking_State” is then read by the StateSelector animation and it know which animation to exit to. When that animation finishes it exits back into StateSelector and the cycle begins again. State Machine Behaviours can also execute at other moments such as when an animation exits or whilst an animation is playing so they're pretty versatile.

That just about covers this particular Under the Hood, thank you for reading and please feel free to get in touch on twitter @StudioFlintlock.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.