• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Darkhalo45 · Nov 15, 2017 at 06:31 PM · animationanimatorweaponequip

How to deal with different Weapon animations on Equip?

I have a database of my weapons in my game, and I am trying to make a script where I can equip those weapons on my hand, but I am not sure what I should do with animations. For now, I just have the Hand animator change everytime I Instantiate a weapon, but it doesn't work very nice and has many problems. What is another way to load up weapons with different animations?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Skaster87 · Nov 16, 2017 at 07:09 PM

I handle this by creating a weapon configuration as a scriptable object including each weapons attack and reload animation, then use an animation override controller to apply those animations at runtime.

alt text First, in the project window, create an empty animation and name it DEFAULT_ATTACK (I use all caps because its a string reference, and that reminds me not to change the string or it'll screw everything up)

Then drag the animation into your animation controller, and add a trigger named "Attack". Make sure the transition goes back and forth with the same trigger value.

the CreateAssetMenu(menuName = ("GameName/WeaponConfig"))] line allows unity to create a custom menu object that can hold all the values of a weapon configuration, including damage, range, and specific animations that can be plugged in and swapped via code at runtime.

  using UnityEngine;
      
      CreateAssetMenu(menuName = ("GameName/WeaponConfig"))]
          public class WeaponConfig : ScriptableObject
      {
              [SerializeField] AnimationClip attackAnimation;
              [SerializeField] AnimationClip reloadAnimation;
              [SerializeField] float timeBetweenAnimationCycles = .1f;
              [SerializeField] float maxAttackRange = 2f;
              [SerializeField] float damage = 10f;
      
            // add public getters i.g. 
             public AnimationClip GetAttackAnimation(){
             return attackAnimation;}}



then a weapon system class to handle controls

 using UnityEngine;
      
       public class WeaponSystem : Monobehavior
          {
              WeaponConfig currentWeaponConfig = null;
              Animator animator;
              CharacterController character;
              const string ATTACK_TRIGGER = "Attack";
              const string DEFAULT_ATTACK = "DEFAULT ATTACK";
      
      void Start(){
                  animator = GetComponent<Animator>();
                  character = GetComponent<CharacterController>();
                  SetAttackAnimation();
                  }
      
      void SetAttackAnimation()
              {
                  if (!character.GetOverrideController())
                  {
                      Debug.Break();
                      Debug.LogAssertion("Please provide " + gameObject + " with an animator overridecontroller.");
                  }
                  else
                  {
                      var animatorOverrideController = character.GetOverrideController();
                      animator.runtimeAnimatorController = animatorOverrideController;
                      animatorOverrideController[DEFAULT_ATTACK] = currentWeaponConfig.GetAttackAnimClip();
                  }
              }
          void AttackTargetOnce()
          {
              if (target == null) return;
  
              transform.LookAt(target.transform);
              animator.SetTrigger(ATTACK_TRIGGER);
            //do damage calculations
           }
      }

then I create the animation override controller and drag it into the inspector for the CharacterController

alt text

And the values get put in at runtime dependent upon the weapon.


animatoroverride.jpg (129.6 kB)
animations.png (295.3 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Renanmgs · Oct 21, 2019 at 05:54 PM 0
Share

This is amazing.

avatar image betaFlux · Feb 24, 2020 at 09:49 AM 0
Share

Thank you very much! There is so little info on this specific topic. $$anonymous$$ay I ask how you handle getting the character into the corresponding weapon stance (idle, movement, etc.)?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

194 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Equip Animation 0 Answers

2D Animation does not start 1 Answer

Can I make animations snap to a frame? 2 Answers

Can you make sprites and animate them in Unity? 1 Answer

Animator position offset 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges