How to add melee animation to character? (SOLVED)


I made a character in UE4, he has a run and idle animation with an animation sequence. I have been searching youtube like crazy on how to do this but they are all tutorials on old versions and they are not the same as UE4.18. My question is, I want to add a sword slash animation to my character so that when the left mouse button is clicked, it will play this animation,making the character look like he is in combat. How do I do that?

Hey there, you just need to have a variable in the character and in the animation blueprint called something like Is Attacking. On the character when you press LMB you set that variable to true, On the animation blueprint you need to use the Update event to update the Is Attacking variable of the animation blueprint to have the same value as the character’s. Then create a state Attack where the transition rule from idle to attack is if Is Attacking is true, if you want to be able to attack while running, add the same transition rule from Walk to Attack. To Finish you need to define the transition rule of Attack to Idle where you set the rule of if the time remaining of the Attack animation is less than 0.1 (for example) you do the transition. Don’t forget to set Is Attacking to false when the animation finishes so you can attack a second time.

Hey thanks for answering. I have already made a variable called Is Attacking on the ani9mation blueprint and the character event graph blueprint. How do I make the variable in the animation blueprint have the same value of the character’s? Thanks

Like this?

Is this correct? And how do I make the LMB the trigger to this?

On the Animation Update event on the animation blueprint you should have a cast to the player character, get the Is Attacking there and set the Is Attacking from the animation blueprint.

No you have to connect the try get pawn owner into the cast. then from the Xavier DOTC you drag a wire and get the Is Attacking and that is the one you use to set.

Not yet, you need to drag a wire from Xavier DOTC and do a Get Is Attacking and connect that to the Set Is Attacking. On the LMB input event you set the Is Attacking variable to true. When you leave the Attack state you set it to false.

You need to connect the execution wire from the cast to the Set Is Attacking. For the LMB you just connect the Pressed to the Set Is Attacking to true, and for now you, you can do the Set Is Attacking to false on the Released.

Is this correct now? I pressed play with these settings and pressed the LMB but I did not get anything. Is there something wrong? Let me know if you need screenshots of anything.

Is this correct? I pressed play again and no luck.

It worked! Thank you very much for your time!

by the way, how do I deactivate walking while I’m in this animation? When I’m holding LMB the animation plays but when I press the arrow key the character moves while the animation is playing.

Delete the Set Is Attacking in the update animation and right click on an empty space and type Set Is Attacking and connect the execution wire and to the Get Is Attacking.

Good to know :slight_smile: Please mark the answer as correct and upvote it if you found it helpful :slight_smile:

Does the animation have root motion?

Nope it does not

Then you need to change where you set the Is Attacking to false to only when the animation ends. Then you can add a branch on the movement axis and only process movement is it’s not attacking.

How do i do that exactly? Im still kind of new to the blueprint codes and stuff.

I would just use animation montages for the attack instead of a state in the state machine, when you play the anim montage you set a timer with the duration of the animation to execute an event that sets the variable Is Attacking to false.