This is the old Animancer v7.4 documentation. Animancer v8.0 is now available.

Weapon Animations

Many games involve combat with various different types of weapons where each type has its own animations for attacking, blocking, and other common actions.

Mecanim

Setting up such a system in Mecanim is generally done by creating a base Animator Controller to define states, parameters, and transitions then creating an Animator Override Controller to replace specific Animation Clips inside it.

  1. Create an AnimatorController asset somewhere in your project and open it.
  2. Assign the asset to the Controller field of the Animator component on your model.
  3. Create the maximum number of states you might need in each category: light attack combos, heavy attacks, special attacks, taunts, etc.
  4. Create dummy AnimationClips and assign them to each of those states to be overridden by a weapon at runtime. If your game includes unarmed combat, those animations may be appropriate for use as the defaults as long as every state has a clip assigned and those clips have unique names.
  5. Give each weapon references to its associated AnimationClips along with the names of the dummy clips they are to override. This is why the states all need dummy clips with unique names; you override clip names rather than state names.
  6. Give your script a public Animator animator field and assign it in the Inspector.
  7. Give your script a private AnimatorOverrideController _AnimationOverride field.
  8. Initialize the override controller on startup (Awake, Start, or OnEnable):
  • _AnimationOverride = new AnimatorOverrideController(animator.runtimeAnimatorController);
  • animator.runtimeAnimatorController = _Override;
  1. When you equip a weapon, assign its clip overrides:
  • _AnimationOverride["DummyLightAttackClipName"] = weapon.lightAttack;
  • Repeat for each clip to override.

Animancer

A similar result can be achieved much more easily in Animancer because you can simply play the animations of any weapon on demand without worrying about keeping an Animator Controller and Animator Override Controller in sync with your code whenever you need to change anything or track down a bug.

  1. Add an AnimancerComponent to your model.
  2. Give your script a public AnimancerComponent animancer field and assign it in the Inspector.
  3. Give each weapon references to its associated AnimationClips.
  4. When you want to play a particular animation, simply pass that clip into the Play method:
  • animancer.Play(weapon.lightAttack);
  1. If you have lots of different weapons and do not want to keep old states from previously equipped weapons in memory, you can destroy them when you change weapons by calling animancer.States.Dispose(weapon.lightAttack);.

The Weapons example demonstrates this concept in more detail.

Similar approaches to either of these could be used to allow game elements to specify their own interaction animations such as a special lever with a Pull animation that isn’t used anywhere else.