How can I make a two weapon switch system like in Call of Duty?

I’ve looked around for tutorials and failed to find one that I could work with and has the following: 2 Weapon switching without (not pick up and drop) and also having variables (ammo, fire rate, etc) separated for the 2 weapons. I am aware that it’s likely to be complex as I couldn’t get much progress by planning myself, so I would be very grateful for anyone who could help me out to create these two major features.

Doubt you’ll find someone who will write a full tut that suits your needs. But at the most fundamental level:

  • create gun classes
  • keep shared variables & functionality in the base
  • extend child classes to do unique things
  • spawn them, attach as / if needed
  • switch between instances of the guns

Each actor has its own skeletal mesh; the player shouldn’t ever worry what the weapons is. It could be a big static mesh rock for all they care. No need to touch / assign Skeletal Meshes at all. Each gun handles the logic internally.

As you can see on the Gun1, Gun2 and GunBase nodes, they’re not skeletal meshes - they’re actor references. They can be of the GunBase type.


and also having variables (ammo, fire
rate, etc) separated for the 2
weapons.

Hopefully, you’re familiar with inheritance. If not, it would be a good moment to look into that. The above suggestion would then allow you do achieve the OOP way of handling something like this.

Do note that this is not a cookie cutter solution, it’s a suggestion for the method. The above is not supposed to be workable. It’s an example only!

Creating a working system is beyond the scope of the AH. Sorry.

Hey Everynone! Following your blueprint, I was unable to get the weapon on the selected weapon. I forgot to mention that I am actually using skeletal meshes for my gun and I’m not sure if that could be the reason as I’ve had to add a couple of things to the blueprint following yours. But I have found that the gun1 and gun2 has spawned in the game. Just not going into my selected weapon (although when i manually add it from the blueprint file, i see it in the editor)

Oh, that is understandable. I tried changing the variable into the actor reference, but now it’s not compatible with the skeletal mesh which I have under my “components” in the blueprint (player blueprint). I do need to use skeletal mesh since I’ve planned animations for the gun itself.

Again, the Skeletal Mesh goes into the gun actor, not into the player. Think about it this way:

  • you will have 30 guns in the game
  • the player will have 2 at a time but can choose from the 30 above
  • you surely do not want to keep 30 skeletal meshes, their animations, sounds, variables and functionality in the player. All of this goes into the gun actor.

Perhaps the confusion stems from the way the first person template example is set up and its Skeletal Mesh here:

350032-screenshot-3.png

This is where you attach your spawned actors, each with its own Skeletal Mesh. That’s where the currently equipped gun points to.

There should be no Skeletal Mesh weapon in the player. We place the ENTIRE gun actor in their hands.

So a system would be closer to this:

I removed the Skeletal Mesh from the player - it’s useless.

  • we spawn 2 guns and attach them to the player’s hands
  • we swap the guns by hiding / showing it and pointing Current Gun var to the currently selected one:

Image from Gyazo

Instead of hiding the unused gun, you can attach it to the player’s back instead. Waaaay cooler. :wink:

Thanks, working as expected! Just a little question… Where can I find the socket of a weapon if I have only variables of actor reference and no skeletal meshes anymore? I don’t think I could just pull out the actor ref variable and get the skeletal mesh off the bp class and bring a socket transform. And btw, I checked out some of your updates on the replies, so thanks! :slight_smile:

Ah, ok, that makes sense, just wanted to make sure I understand what’s happening here. Thanks again! :slight_smile:

Where can I find the socket of a
weapon if I have only variables of
actor reference and no skeletal meshes
anymore?

Not sure what you mean? You do have skeletal meshes - each gun actor has a skeletal mesh component with a skeletal mesh object inside.

Essentially rather than having just a skeletal mesh that does not do that much, you now have an actor with a skeletal mesh that can do a lot.

Lulz, not again…

One more try…

You do have skeletal meshes. Each actor has one. Rather than having just a skeletal mesh that does little, now you have actors that can do so much more (ammo, stats, particles, animations, reloading functionality, timelines, interfaces, widgets, material handling…)

1 Like

I stumbled upon this thread by accident…
God bless your soul for your patience :sweat_smile:

2 Likes