Dev Log#22: Adding secondary weapons handling swap functionality
- camjhill737
- Nov 18, 2024
- 1 min read
I implemented a secondary weapon so players can have two weapons and with there being a mix of long range, mid range, and short range weapons it means players would be able to deal with different types of gunfights.
It's similar to how swapping weapons works in Space marine 2 you simply press E to swap and if you are holding two weapons the gun you currently holding is dropped if you pick up another weapon.
This was achieved by using creating a new type of weapon state which is an enum class for the secondary weapon.
I attach the secondary weapon to the player character's backpack. I'm also handling the collision settings for the secondary weapon as collision and physics needs to be disabled when its attached to the backpack.

I refactored this code into separate functions as there's a lot of code happening in this function and the readability might get a bit weird otherwise.
So in the first few lines I'm creating a local variable in order to change the equipped weapon to the secondary weapon and vice versa.
I'm then handling the weapon state and attachment of the equipped weapon. Updating the ammo values as different guns will have different values. Playing the sound of swapping weapons.
If there is no ammo in the current magazine for the gun you've just swapped to it will automatically reload.
Finally I'm handling the weapon state and attachment of the secondary weapon.
Comentários