Melee Support
1 Melee Weapon Design
The structure of weapon system is given below.
For melee weapon, we need to add melee weapon type class. Create the child blueprint of BP_WeaponBase_USG for melee weapon (See BP_ShootingWeapon_USG for shooting weapon).
- /Game/USGT/Framework/Weapon/BP_MeleeWeapon_USG
Implement two functions from parent:
-
StartAttack
-
StopAttack
If you have some logic to do when equipping or unequipping weapon, you can implement following two functions.
-
OnEquipWeapon
-
OnUnEquipWeapon
The basic equippment logic is implemented in the weapon base class(BP_WeaponBase_USG) so that you need to calls the parent function first if you implment these two functions.
1.1 StartAttack Implementation
In order to make weapon system uncoupled, we need to add several interfaces (IF_WeaponOwner_USG), which will be implemented in the weapon owner class.
-
IsForbidMelee: Whether the melee attack is allowed.
-
OnMeleeStarted: Tell owner to change to melee state.
-
OnMeleeFinished: Tell owner to end melee state.
Implement StartAttack:
StartAttack is called from locally controlled player and then you should notify server to broadcast to others client to display melee.
We use a montage to display melee attack and you can change this logic if you have more complex animation to display.
From the character blueprint, you need to implement the above interfacesof IF_WeaponOwner_USG.
The following interface decides whether the character can enter melee state, also see Pawn State Control chapter.
The following interfaces is used to tell the character enter or leave melee state.
1.2 StopAttack Implementation
Make some clear operations in the StopAttack function.
1.3 Trigger Damage Event
In order to determine which actor can receive damage event, we add another interface blueprint IF_WeaponDamage_USG, which will be implemented in those actors who want to receive damage event.
In order to trigger damage event, we add a sphere collision and use the overlap event to trigger damage. You can adjust the sphere size and location from the child blueprint. Also, you change the mesh for different melee weapon from the child blueprint.
Then, implement the damage interface for character or other actor.
The event is triggered from server and you can broadcast damage event to clients if you need (Already done in our project).
2 Melee Weapon Example
We use item system to add melee weapon as well, also see details about creating item in Item System
2.1 Child Melee Weapon Class
Add child blueprint of BP_MeleeWeapon_USG, use stick as an example.
- /Game/USGT/Game/CoreModules/Weapon/Stick/BP_Weapon_Stick
Config the mesh, montage, hit sphere and attach info. Also, see the parameter details from section 3.1 in Weapon System chapter.
2.2 Add Pickup Entity
Add a pickup entity as follow and config the item id for pickup.
- /Game/USGT/Game/CoreModules/Weapon/Stick/BP_PickupStick.
2.3 Add Item Entity
Add an item entity as follow and config the .
- /Game/USGT/Game/CoreModules/Weapon/Stick/BP_StickItemEntity.
2.4 Config Item Table
Add the item definition to the item table.
- /Game/USGT/Game/CoreModules/Items/DT_ItemDefinition
2.5 Drag Pickup Enity To Level
Drag the pickup entity to the level and play, then you can pickup the melee weapon to attack.
Press H to holster current weapon.
3 Unarmed Melee Design
Without melee weapon, it means that we need to attack with punching. We can also add an empty weapon for this case.
3.1 Add New Weapon Type and Slot
As we don’t want this unarmed melee weapon to be replaced by other melee weapon, we need to as a new weapon slot type for this.
First, add a new weapon type as follow in EN_WeaponType_USG.
Then, add a new weapon slot in the BP_WeaponComponent_USG. Here, we config it as the default property, you can config it from character blueprint as well.
3.2 Create Empty Melee Weapon Item
We also create an melee item as section 2. We don’t need pickup entity becasue we will add this item from begining and this item can’t be discarded.
- /Game/USGT/Game/CoreModules/Weapon/UnarmedMelee/BP_Weapon_UnarmedMelee
You only need to config the hit sphere for punching damage, the montage and the attach info.
- /Game/USGT/Game/CoreModules/Weapon/UnarmedMelee/BP_UnarmedMeleeItemEntity
Set the weapon type to UnarmedMelee, then add the item to the item table as well.
Notice that this item cannot be discarded because this unarmed melee ability is permanent.
3.3 How to Equip Melee
This unarmed melee item is not being picked up from ground, but add from the mement that character created.
In order to add this item when the character spawned, we add a property, called DefaultItemList, for base character class (BP_Character_USG).
Then, add default items from Possessed(possessed by controller) function.
In order to switch to unarmed melee weapon, we can add a property, called UnarmedWeaponSlot, we switch to this slot when we call the holster weapon function.
When we press N to switch next weapon, it will normal switch to next effective weapon. When we press H to holster current weapon, it will try to switch to unarmed melee weapon to promise the character has the unarmed melee ability.
Finally, we config the default items from the child character blueprint in the game module.
3.4 Run Test
Run the level and click left mouse, you can see the character punching normally.
-
Press H to holster current weapon.
-
Press N to switch to next weapon. If you have one shooting weapon, press N, it will go to the above unarmed melee weapon.