My character is using the wrong animation while holding guns

Hi,

To put you in context, my character can pick up weapons from the floor (but you cannot drop it) and storage it in an inventory. You also can unequip the gun to run faster. In total my character has 3 states/blendspaces : unarmed, holding pistol and holding big guns (assault rifle, shotguns, etc)

When i pick up the pistol, the character auto equip it (feature), but is using the “big guns” animation(Picture 1). If i unequip the pistol, it doesnt change to the unarmed state (Picture 2)

After some prints and debugs, i found the source of the problem, in my “Weapon System”, the function isnt working as it should be.

The Weapon System is just a Enum Blueprint + Switch Broken Weapon System posted by anonymous | blueprintUE | PasteBin For Unreal Engine
The function that contains the issue Broken Weapon System 2 posted by anonymous | blueprintUE | PasteBin For Unreal Engine
Note : I know that "Has Weapon 3 isnt connected, but is not related to this error)

In my first try, i replaced the Branch “If the character has the pistol” from the function , forcing the code to use the correct animation, with a cast to the pistol (if the cast fails, it means that the character doesnt have the pistol) but then, the character holds two guns, the pistol (first weapon in the game) and the second one (no matter witch one) (Picture 3)

The function with the branch replaced by the cast Broken Weapon System 3 posted by anonymous | blueprintUE | PasteBin For Unreal Engine

Can someone enlighten me? I know there are some cool tutorials, but i’m trying to make my own system and i dont want to give up :frowning:

Hi @ZEREUX, I think I can help.

From what I’m reading and seeing the pictures, your trying to have the player switch state when the player equips and unequips a weapon, correct?

If that’s the case, I recommend you code the functionality inside your Animation Blueprint. You linked a lot of code, but I’m not sure if it’s in the Animation Blueprint.

There is a simpler way of doing this, but I want to acknowledge that you want to make your own system so I’ll give you some tools and hopefully you can find them useful.

1. Blend Poses by Enum:
Inside the Animation Blueprint, there is a node called “Blend Poses by Enum” which will blend to different states based on the Enum that was plugin. (The Enum can be anything, in this example it’s blending between different damage states)
image
MORE INFO: Blend Nodes | Unreal Engine 4.27 Documentation.

2. Blueprint Interface:
A blueprint interface is a way to for blueprints to communicate with each other without casting to a specific object/actor. (This will allow you to get information from an actor/object without casting as long as the actor/object is valid. In short, it allows for less hard coding which is a great practice in game development and in coding in general)
MORE INFO: Blueprint Interface | Unreal Engine Documentation

If you’d like me to explain how you can implement this in your own game please let me know, I just want to give you a chance to make it yourself since you’d like to make your own system which is respectable.
Hope I was able to help! :v:

1 Like

Hi ArcReed3215!

Yes, in my system i try to change the state based on the gun used and if the gun is in your hands or not, but the entire system is located in the character blueprint.

In my Animation Blueprint, i use BlendSpace by bool 'cause, at the end, every “question” can be answered with yes or no. Do you have the pistol? Y/N, Do you have a Heavy Weapon? etc

Im not sure if the Blend Poses by Enum will make an effect, 'cause HasPistol? and HasRifle?* are acting in the same way as the Enum

Note : HasRifle? means “HasHeavyWeapon” but i didnt change the name

Your pick up/drop equip/unequip logic needs to SET a State based on the action. Then use that State in your state machine(s).

I’d use an Enum.

Hi @ZEREUX, Blend by Enum will make your code easier to read and allows adding more weapons easier and you can also get rid of all the Y/N booleans that determine if your holding a pistol, rifle, or nothing.

Yes, blend pose by bool would work, but if you were to add another state you would need to implement MORE code and add ANOTHER blend pose by bool. The issue of your player not changing states may be because of all the code that is not necessary which causes confusion to you and the game. By using the Blend Pose by Enum you can add as many states and weapons as you’d like and only using one node. It’s cleaner coding and would most likely fix the issue.

  1. By looking at your code, it looks like your weapons are a separate actor than the player. You can create an Enum that list the different types of weapons. Inside your weapon, you can make a variable of that new Enum type and hard code the type of weapon.

  2. Inside your Animation Blueprint, you would also need a variable of that new Enum and use that variable inside the Blend Poses by Enum.

  3. Finally you would need to set the Enum variable (from the Animation Blueprint) to the type of weapon you are holding. I’m not sure how you system is setup, so I’ll leave that step to you. It could be as simple as accessing the Enum variable inside the weapon and setting it to the Enum variable inside the Animation Blueprint.

Adding a new weapon type is as simple as adding a new Enum to the Enum list and a new state inside your Animation Blueprint. You wouldn’t need any extra booleans.

Once you finish that, it should fix the issue of you player not switch states because if he is not holding a weapon a different pose will fire from the Blend Pose by Enum and the same idea occurs if he’s switches to a different weapon type.

Hope this help! Let me know if you need me to clarify something, best of luck! :+1:

Hi!
Sorry for the late response.

Yes, i’ve a master gun BP and different weapon actors, the Character just attach the weapon to the socket and then i set the visibility.

I already did the Enum System for the AnimBP, and the Char does switch between the blendspaces in the AnimBP, now im trying to figure out how to do it the moment you pick up the gun.

Thanks for the tips!

Hi @ZEREUX, thanks for the update.

To fix this, you want to make a function inside your Animation Blueprint and have the Enemy as a parameter so you can change the Enemy that is inside your Animation Blueprint. Whenever you pickup a weapon or switch into a different weapon, you want to call this new function and set the Enum to the correct weapon type. You can get your Animation Blueprint by getting the player character and getting it’s Anim Instance from the mesh component, then you can cast to you Animation Blueprint. This should get you the result your looking for.

Hope this helps! :v:

Hi again!

While i was working on the new Enum function i figured out the source of the problem.

In my Weapon Selector function i set to true one boolean (pistol or heavyweapon) depending if the weapon used. The problem is, if the player pick 2 (or more) weapons, the function will be called again and both booleans will be True. Has Pistol and Has Rifle are used in the Anim Blueprint so for the game, both BlendPoses by Bool will be true and looks like UE uses the “last true bool” for the AnimBP.

I fixed with a Do Once node, but anyways my code was designed for 2 weapons, adding more will be a pain in the *ss so i will change to an Enum System like you said.

Thanks a million!