Advertisement
MaximilianPs

RPG Kit - MeleeWeapon extension

Jan 29th, 2022 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. /*
  2.  *  This is a VisibleItem extension for RPG Kit.
  3.  *  you can specify which AnimatorOverride to use, to replace animations based on the Weapon Type
  4.  *  
  5.  *  Also it have an Holster, so you can store the weapon somwhere in the character hierarchy.
  6.  *  and Place it in Hand when entering in combat.
  7.  *  
  8.  *  Please note, that by default RPG Kit didn't have any InCombat/OutOfCombat method, and also
  9.  *  it will require a Bool Parameter called "In Combat" used to indicate to Unity which animation to play.
  10.  *  
  11.  *  <TODO>
  12.  *      Call methods from Animator, to get a smoother transition from
  13.  *      holding a weapon, to when putting it back in the holster.
  14.  *  </TODO>
  15.  */
  16. using UnityEngine;
  17. using DevionGames.InventorySystem;
  18.  
  19. namespace MaximilianPs
  20. {
  21.     public class GDMeleeWeapon : Weapon
  22.     {
  23.         [SerializeField]
  24.         private AnimatorOverrideController animatorOverride;
  25.         private AnimatorOverrideController animatorBackup;
  26.         [Space]
  27.         [SerializeField]
  28.         private Transform holster;
  29.         private GameObject weapon;
  30.  
  31.         private bool inCombat = false;
  32.  
  33.         protected override void Start()
  34.         {
  35.             base.Start();
  36.             animatorBackup = m_CharacterAnimator.runtimeAnimatorController as AnimatorOverrideController;
  37.  
  38.             inCombat = m_CharacterAnimator.GetBool("In Combat");
  39.         }
  40.  
  41.         public override void OnItemEquip(Item item)
  42.         {
  43.             base.OnItemEquip(item);
  44.             m_CharacterAnimator.runtimeAnimatorController = animatorOverride;
  45.  
  46.             if(m_CharacterAnimator.GetBool("In Combat") == false)
  47.             {
  48.                 // = GetEquipedGameObjects(attachments[0].region.Name)[0];
  49.                 weapon = attachments[0].gameObject;
  50.                 weapon.transform.parent = holster;
  51.                 weapon.transform.localPosition = Vector3.zero;
  52.                 weapon.transform.localRotation = Quaternion.identity;
  53.             }
  54.         }
  55.  
  56.         public override void OnItemUnEquip(Item item)
  57.         {
  58.             base.OnItemUnEquip(item);
  59.  
  60.             m_CharacterAnimator.runtimeAnimatorController = animatorBackup;
  61.         }
  62.  
  63.         protected override void Update()
  64.         {
  65.             base.Update();
  66.             if(inCombat != m_CharacterAnimator.GetBool("In Combat"))
  67.             {
  68.                 inCombat = m_CharacterAnimator.GetBool("In Combat");
  69.  
  70.                 if (inCombat == false)
  71.                     WeaponInHolster();
  72.                 else
  73.                     WeaponInHand();
  74.  
  75.             }
  76.         }
  77.  
  78.         protected override void Use()
  79.         {
  80.             base.Use();
  81.  
  82.             if(m_CharacterAnimator.GetBool("In Combat") == false)
  83.             {
  84.                 WeaponInHand();
  85.                
  86.                 m_CharacterAnimator.SetBool("In Combat", true);
  87.                 inCombat = true;
  88.             }
  89.         }
  90.  
  91.         private void WeaponInHand()
  92.         {
  93.             weapon = attachments[0].gameObject;
  94.             weapon.transform.parent = m_Handler.GetBone(attachments[0].region);
  95.             weapon.transform.localPosition = attachments[0].position;
  96.             weapon.transform.localRotation = Quaternion.Euler(attachments[0].rotation);
  97.         }
  98.  
  99.         private void WeaponInHolster()
  100.         {
  101.             weapon = attachments[0].gameObject;
  102.             weapon.transform.parent = m_Handler.GetBone(attachments[0].region);
  103.  
  104.             weapon.transform.parent = holster;
  105.             weapon.transform.localPosition = Vector3.zero;
  106.             weapon.transform.localRotation = Quaternion.identity;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement