- Unreal Version: 5.0.0
- Template: Third Person
- Project Name: MyProject
This switching weapon example is a type of weapon structure where the character can only have one sidearm and one rifle at a time. When the player overlaps a weapon pickup the weapon will snap to the character and be added the players weapon map. This is not production ready code nor is it a perfect example of how a weapon system should work.
There's dozens of ways to pick up and switch items, and mileage will vary depending on a game's needs. I'm sure there are better ways of doing this, but this was the way I construtcted it and I learned a lot throughout the process. There's a variety of different engine and code features this example touches so I figured it might help.
The animations I used don't line up exactly with where the weapons are placed on the body, but that's alright, it gets the point across and we can always import other animations later
- Add Animation Starter Pack to project
- Add Military Weapons Dark to project
- Each pack above should be free. When you click
Add To Project
in your Library's vault you'll probably need to clickShow All Projects
then select your project then select the latest version the asset pack provides.
- Add Inputs
- Retarget Skeleton mesh to be able to get the idle pistol, idle rifle, equip pistol, and equip rifle animations.
- Create montages of equip animations
- Create new C++ class from AnimInstance called MyAnimInstance
- Reparent Mannequin's AnimInstance parent class to MyAnimInstance
- Create sockets to hold the guns
- Update Anim Graph with layered animations. I layered the idle animations alternatively you can create animation blends
- Update Animation Blueprint with nodes for isHoldingPistol and isHoldingRifle
- Add AnimNotifies to the Equip Pistol and Equip Rifle animations at about half way through the animations
- Update Event Blueprint to fire the equip functions from the AnimInstance file.
- Create MyWeapon class
- Create Data Table that uses the WeaponData Struct and add a pistol and rifle
- Make Blueprint with parent PickupWeapon, add the row name and the data table variable and then drop them in the editor.