How to implement weapon-dependent character stances using Mecanim?

My player character uses different weapons, ranging from pistol to heavy machinegun weapon types. I would like the player character overall animation stance to be based on the type of weapon currently equipped. For instance, he will carry and fire the machinegun from the hip and have a rather heavy walk/run animation, while the pistol will use a different fire/reload/run animation set.

What is the best strategy to implement this system using a generic mecanim rig? Shall I setup one animation controller per weapon type, or a single one with specific branches for each type of weapon?

Using the legacy animation system, I would simply replace the run/fire/reload animation at run-time with the weapon-specific run/fire/reload one based on currently equipped weapon. I’m not sure how to translate that to mecanim though.

Any pointers on the topic are much appreciated!

2 Answers

2

In Unity 4.1, you can change animation controllers at runtime, so multiple animation controllers might be a good option if you synchronize them so there isn’t a stutter in the animation when you change.

If you’re using Unity Pro, consider using Sync Layers. Your Base Layer can define typical run/fire/reload/etc. states. Additional layers sync’ed to Base Layer will fill in the states using weapon-specific animations. When you change weapons, you simply smooth out the old weapon layer’s weight to 0 and smooth in the new weapon layer’s weight to 1.

On my current project, I took a different route and built one animation controller with sub-state machines for different weapons on the same Base Layer. Changing weapons means transitioning from one sub-state machine to another. I chose this approach for two reasons:

  1. There was a beta bug (since fixed) with sync layers, and
  2. I’m using G1NurX’s Event System for Mecanim (Event System for Mecanim [Unity5 supported] | Animation Tools | Unity Asset Store) to trigger Fire() messages. Each weapon’s fire animation needs to trigger Fire() at a different time. It was easiest to separate them out into separate state machines to prevent multiple Fire() messages (only one of which would be valid for the active weapon).

This approach has worked really well for me. Hope these ideas help!

hi @tonyli, the answer is really good and what i was looking for. can you show an implementation of a sub state machine ?

there is a great article about it.

http://www.faq.rpg-kit.com/discussion/2901/tutorial-weapon-specific-animations-using-mecanim