- Home /
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?
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.
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
And the values get put in at runtime dependent upon the weapon.
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
Follow this Question
Related Questions
Character Equip Animation 0 Answers
2D Animation does not start 1 Answer
Can the mecanim animators Controller var be set in code? 1 Answer
Accessing AnimationState in Animator 0 Answers