美文网首页
Melee dual wield weapon

Melee dual wield weapon

作者: iqxtreme | 来源:发表于2020-09-14 10:29 被阅读0次

    TODO:

    • BladeComponent's position is used for playing sfx, but now the DualWieldBladeComponent does't return the right position for sfx.

    TO FIX:

    Implementation

    $Modify the class MeleeWeapon
    • Add a new enum value DualHands = 1000,toMeleeWeapon.WeaponBone.
    • Add the following code to MeleeWeapon.EquipWeapon()'s switch clause:
    case WeaponBone.DualHands:
                        // just let the root be the bone for dual weapon shell node.
                        bone = character.transform;
                        break;
    
    • Just below the MeleeWeapon.EquipWeapon()'s GameObject instance = Instantiate(this.prefab);,add the following:
    if (this.attachment == WeaponBone.DualHands)
                    this.ProcessDualHandsWeaponInstance(character, instance);
    
    • Add the two new methods to 'MeleeWeapon':
    //< geo for dual wield weapon
            public void ProcessDualHandsWeaponInstance(CharacterAnimator character, GameObject dualWieldWeaponInstance)
            {
                // Get the dual wield component.
                DualWieldBladeComponent dwBlade = dualWieldWeaponInstance.GetComponentInChildren<DualWieldBladeComponent>();
                // for left hand
                this.__AttachWeaponToCharacterBone(dwBlade.weaponLeft, character, HumanBodyBones.LeftHand);
                // for right hand
                this.__AttachWeaponToCharacterBone(dwBlade.weaponRight, character, HumanBodyBones.RightHand);
            }
            private void __AttachWeaponToCharacterBone(Transform weapon, CharacterAnimator character, HumanBodyBones bone)
            {
                Transform node = character.animator.GetBoneTransform(bone);
                if (node)
                {
                    weapon.SetParent(node);
                    weapon.localPosition = Vector3.zero;
                    weapon.localRotation = Quaternion.identity;
                    weapon.localScale = Vector3.one;
                }
            }
            //>
    
    $Modify the class BladeComponent
    • Change the CaptureHits() to virtual.
    • Change the EMPTY_GO_LIST to public.
    • Change the Setup() to virtual.
    • Change the Awake() to virtual.
    $Write a subclass DualWieldBladeComponent from BladeComponent
    using System.Collections.Generic;
    using UnityEngine;
    
    using GameCreator.Melee;
    
    public class DualWieldBladeComponent : BladeComponent
    {
        [Header("Dual Wield Weapon Setting")]
        [Tooltip("The weapon node of left or right hand.")]
        public Transform weaponLeft, weaponRight;
        [Tooltip("The blade component of left or right hand(optional")]
        public BladeComponent bladeLeft, bladeRight;
    
        override public void Awake()
        {
            // block BladeComponent.Awake()
    
            // get bladeLeft and bladeRight
            if (!this.bladeLeft)
                this.bladeLeft = this.weaponLeft.GetComponentInChildren<BladeComponent>();
            if (!this.bladeRight)
                this.bladeRight = this.weaponRight.GetComponentInChildren<BladeComponent>();
        }
    
        private void OnDestroy()
        {
            // destroy the weapons
            if (this.weaponLeft)
                Destroy(this.weaponLeft.gameObject);
            if (this.weaponRight)
                Destroy(this.weaponRight.gameObject);
        }
    
        /// <summary>
        /// Override the BladeComponent.Setup().
        /// Call subweapons Setup().
        /// </summary>
        /// <param name="melee"></param>
        override public void Setup(CharacterMelee melee)
        {
            base.Setup(melee);
    
            if (this.bladeLeft)
                this.bladeLeft.Setup(melee);
    
            if (this.bladeRight)
                this.bladeRight.Setup(melee);
        }
        /// <summary>
        /// just call sub weapons' CaptureHits().
        /// </summary>
        /// <returns></returns>
        public override GameObject[] CaptureHits()
        {
            GameObject[] candidatesLeft = EMPTY_GO_LIST, candidatesRight = EMPTY_GO_LIST;
    
            if (this.bladeLeft)
                candidatesLeft = this.bladeLeft.CaptureHits();
    
            if (this.bladeRight)
                candidatesRight = this.bladeRight.CaptureHits();
    
            var ret = new List<GameObject>(candidatesLeft.Length + candidatesRight.Length);
            ret.AddRange(candidatesLeft);
            ret.AddRange(candidatesRight);
    
            return ret.ToArray();
        }
    }
    

    Now we have a new melee framework supporting daul wield weapon.

    How to use it

    $The Weapon Prefab
    • Create the Weapon Prefab with two child node for left and right hand weapon.
    • Add DualWieldBladeComponent the prefab's root node, this node can be seen as the abstract weapon wrapper, the real weapons are its children.
      Add `DaulWieldBladeComponent to the Prefab's root.
    • For the child nodes(the real weapon), just config them as normal melee weapon. as the image above, it just put two MeleePackages's SwordWeapon prefab here as left and right weapon.
    • For the root ndoe's DualWieldBladeComponent, these params will do nothing:
      On prefab root, they're Not useful anymore.
    • These events are only valid on the root's DualWieldBladeComponent, not on the child weapons' BladeComponent, so if you need these events, just focus on the prefab's root.
      These Events are Only Valid on Root's `DualWieldBladeComponent`.
    • Reference the two real weapon nodes to the 'DaulWieldBladeComponent`.


      Reference the two real weapons.
    • Notice that the Trail setting for root is useless.
    • The Blade Left and Blade Right can be assigned manually, or left blank, they will be filled automatically at runtime.
    $The Weapon
    • Now create a weapon as MeleePackage says, then just focus the Weapon Model section:
      Configuration of 'Weapon Model' section for dual wield weapon.
    • As you can see in the image above, just reference the weapon prefab and set the attachment to Dual Hands, That are all the differences from normal weapon. On more thing, Position and Rotation Offset won't work.
    • To adjust the position and rotation offset for the weapon, go inside the weapon prefab.

    相关文章

      网友评论

          本文标题:Melee dual wield weapon

          本文链接:https://www.haomeiwen.com/subject/enqjektx.html