Elegant solution to play certain animations depending on what weapon a player is using?

I currently have my animations setup for my primary and secondary weapons just fine- a blend by bool check in the animation blueprint decides if the current weapon is primary, and if it is, it plays the primary animations, if not, it plays the secondary animations. However, I have many more weapons ready to implement, and it would be possible for the player to pick up more than 2 at a time. I did get a clumsy setup working for 3 weapons- a messy string of bool checks which goes through each weapon and checks if they are currently equipped. The problem here is that eventually the amount of checks for each action (aim/reload etc.) will become huge and very hard to manage. I have looked around extensively but the only examples I could find were of the ShooterGame- but that only has 2 weapons, therefore it decides on what animations to play with a bool check. What would be the “best” way to efficiently check what weapon is equipped, and play the corresponding animations, assuming a player could pick up 12 different weapons at any time in a game? I was thinking mabye a select on an enum of weapon names, but my initial tests failed to play any anims there, so thought I’d ask here and see if anyone could shed light on it. Many games (including Unreal Tournament) have many different weapons and animations that play fine, so I know there must be a standard way of going about this! Thanks!

1 Like

I’ve been thinking about this lately as well. with my limited knowledge of the UE4 anim system, the best solution I could come up with would be to make a BlendSpace with as many divisions as number of weapons you have, and make one of these BlendSpaces per animation “action”.
so imagine you have 3 animations per weapon and 6 weapons, you make 3 BlendSpaces each of which will have 6 animations to choose from. when the animation is triggered it will play Fire1, Fire2 or Fire3 accordingly, and each of the 3 fires will use the apropriate animation for the corresponding weapon.

but perhaps there’s a better way, I’m all ears

On character bp create an integer variable. For every weapon you equip, set this integer variable to a fifferent value. This value will be switching between state machines on anim bp.

On anim bp make all your state machines for different weapons. Get a node switch by int( cant remember exact name). Hook your state machines to that node and hook the node to final state.

On anim bp event graph cast to character and get the integer variable that you created earlyer. Now create a new integer variable in anim bp.

Now use the variable you got from character bp to set the integer on anmib bp.

Go to anim graph. Get the integer variable. Hook it up to the switch by int node where your state machines are connected. Done.

Wow I am bad at explaining stuff :)))

It’s pretty easy actualy all you have to do is expose the animation sequence-blendspace-aimoffset you want to be custom and give it a variable of the same type than you can change the variable based on what weapon you have equiped

Thanks for this! I had a similar setup before except I tried a switch on enum- which didn’t seem to work out. I’ll work on it again using the switch on int and see- I probably made some unrelated error last time! Good to know this method is doable :slight_smile:

create weapons enum. Put all your weapons in the enum. You can switch the weapons in enum like this:

This is in characterBP.

next go to anim bp and get this enum by casting to character bp and getting this enum. Create a variable in anim bp. Call it weapons or whatever. Set its type to the weapons enum you created earlier. Drag it out and select set. Setit with enum variable from character bp. It looks like this:

Go to anim graph. Get your enum weapons variable. Drag out a pin and type blend poses enum weapons. Get that node. Right click on it and add pins with all the names of your weapons. Connect state machines. Connect the blend poses by enum with final pose. It will look something like this:

Good luck!

4 Likes

Wow thank you, this is exactly what I was looking for!

P.S. sorry for the late reply, haven’t checked on this thread in a while, but the info is greatly appreciated :slight_smile:

My pleasure!

Quite helpful.
I have been searching the forums and google, with very little results. Was going to post a question myself until I saw this answer!

What I have done took like 4 months to make it work well, but it works.

Basically, I have a special object called “ItemAnimList”. This simply holds a list of animations for a certain weapon and skeleton. An example would be “pistolPlayer”, for pistols the player would use.
Now the item can simply run a function called "setAnim(‘aim’), and it finds the correct animation, and sets that as it’s current animation.

The anim blueprint then detects the change in animation, and switches it. Boom! :smiley:

Good thread, I was also about to go through the same process and was wondering whether multiple state machines, one for each type of weapon blended in the animGraph via int was a legit solution, apparently it is.
Cheers everybody!

The int method I was writing from my (bad) memory so it might be of. :slight_smile: But the enum method with pics is 100% good. Glad this old thread is still useful to people.

Thanks Pabooklas.

That right click to add more poses is not very obvious, thanks for pointing it out

Nick

In our project we have our load out selection set up like Shooter and it “does” work well as an example and can support more than 2 weapons.

example.

Load the Player Pawn BP and under the Inventory branch in details you add additional weapon BP’s to the load out. So if you add 5 more weapons each can be selected as you rotate the mouse wheel.

In the weapons BP all the information as to unique weapon requirements is contained with in the BP and you can just cast for the info you need from the current player pawn.

Everything is contained in the weapon BP like 1st and 3rd person as well as weapon animations and weapon specs and you only have to cast for it once based on the current weapon selection. Slick way to do it because once set up in the player animations you can add as many weapons as you wish with out having to add unique requirements to the player character animation BP.

P.S. A requirement ideally suitable for a plugin solution. :wink:

No problem man.

Hi, I’ve followed this exactly yet I can’t quite get the switch weapons to work. I’ve added different animations for each pose and I replicated everything in your pictures yet my character won’t “switch weapons” in game; he just stays in the same pose the entire time. Any help would be greatly appreciated, thank you.

2 years later and this post is still helpful to a Newb Like myself. Thanks Guys I am so surprised noone has done a tutorial on this topic as of yet!!

My hip fire aimoffset works but for some reason everytime i switch to the starter pack ironsights aim offset i made he looks like he is frozen in the idle position of the hip fire, when i go into the aim offset for the ironsight it looks like it works perfectly but something with it running in the animbp is messing it up.

What A Pain In The ■■■, Just Let Us Use Different BP Animation Files Depending On The Gun, Duh

2 Likes

Sorry for the necro, just wanna help those who find this later!

Look into animation layers. Thats exactly what they do, let you use different sub graphs for things. I use this method for weapons specifically as the player can equip things to both the left and right hands.

  1. Create an animation layer interface

  1. Create however many layers you want here, do note that the groups the layers are in matter, I’m still a little confused myself as to why but in my testing things weren’t working until my left and right hand layers were in the same group.

image

  1. Duplicate your players animation blueprint and label it appropriately, I call mine animbp_1hand, animbp_2hand, ect for each weapon type and make sure these new anim bps use the animation layer interface from step 1, adding them is the same as adding a blueprint interface in the class settings

  1. Make sure your players anim bp also uses the interface and from there just add the linked layer to your players anim bp wherever it needs to be. In the layer interface object you can add multiple input pins and do whatever with those inputs in the sub anim bps.

  1. The important part here, is in your sub animation blueprints don’t use the default animgraph, that won’t work. Under the animation layers category use the named layer anim graphs

image

Apologies if I explained that weird, while I’ve been using them for a little bit figuring them out was a bit weird and there’s still more for me to learn. I hope that info was helpful to someone though :smile:

Unreal Docs: Using Animation Blueprint Linking | Unreal Engine Documentation