How do I make my 2D character attack?

I’ve been working through and online tutorial for creating 2D games in Unreal engine. For the most part it’s been relatively easy and I’ve enjoyed learning how to do it. One area that I’m struggling with is attacking, it seems each tutorial has it’s own way of doing it, some are more complicated than others.

I have attempted to implement a solution to make my character attack but it’s not working. Running around, jumping and changing the direction the character is facing have all worked well.

Here is my blue print that I’ve followed along with to make this happen, it’s important to say that the course I followed didn’t include anything on attacking or other “advanced” movement/interactive concepts.

You’ll see from the blueprint that

  • Update Animation is called on tick as is the Rotate Character.
  • The ‘run’ flipbook is selected if the character appears to be in motion.
  • Directional movement is trigger by the inputs I’ve defined in the project settings, in my case ‘a’ and ‘d’
  • Jumping is handled by inputs I’ve placed in the project settings, in my case ‘space bar’
  • Attacking is bound to ‘f’

The animation for attack doesn’t play despite the flipbook being specified for attack. I tried adding a delay worrying that it was potentially a timing issue but that didn’t help. The inclusion of ‘set looping’ is to allow the attack animation to only play once.

I guess what I’m asking for here is some kind of guidance to make this attack animation happen. Ideally pressing attack ‘f’ should play an attack animation and then the character should go back to being idle.

Any help on this is appreciated.

Hey there @Yanayaya! So it looks like your update animation is overriding your attack since it’s running every frame. So your flipbook is being set back to running or idle every single frame. So there’s multiple ways around this, you can have a branch before your Set flipbook on your update animation function and check a “PlayerAttacking” bool or something like it. This won’t be scalable if you wanna make tons of different animation types, but it’ll get the job done here. Let me know if this helps or if you have any more questions!

2 Likes

Thank you for this suggestion it’s been really helpful. In my blue print, the event ticker checks a Boolean named “Sword Swing” for its state and then selects the appropriate flipbook based on the result. I’ve added a delay for the attack duration and then unset the “Sword Swing” Boolean after completion.

  • Boolean value called Sword Swing added
  • Custom event called Attack_Sword added which selects the attack sprint flipbook if it’s called.
  • InputAction Attack sets the “Sword Swing” to true for a duration of 0.3seconds and then set it to false.
  • Event Tick checks the “Sword Swing” Boolean and if it’s true, calls the “Attack Sword” event, if false, the normal idle animation will resume.
1 Like

Hey all,

I indeed have a problem with scalability here :slight_smile: Could you elaborate on another approach?
Thanks :slight_smile:

Hey there @DrGuanda! Going up from a simple bool would next be a simple enum for the state the player is in with a switch case before the inputs gets passed all the way in. It’s basically like a ‘pre-finite state machine’ in the sense you would have to still manage the states at that point and it becomes a bit more of a hassle if you have a ton of them.

The next scale becomes actual state machines. While UE has a state machine setup in the form of it’s animations and also one for AI, there’s not a standard one for logic operations. For 2D games however much of the time you’re not using the standard animation state machines, so in this case you may need to handle the logic yourself if it gets complex enough to require the need for a full state machine.

There’s other implementations that handle this differently, but I have a background in C# so I’m fairly biased to follow a state machine based style.

1 Like

Thanks SupportiveEntity,

I will go and check out what this all means :slight_smile:

So a quick rundown for you is in order: If you were to handle this with an enum/switch statement it still gets pretty large pretty fast but far more manageable/scalable than a bunch of bools and branches.

So first create an Enum in the content drawer for the states you want.
image
image

(please don’t name it PlayerState like I did here, it will only confuse you later)
Make it a variable in your BP.
image

Then make a switch on (Enum name), and pass all your initial function’s through to the other function they do. This gets to be a problem in massive amounts purely upon the nature of blueprints. If you need more than 10 or 20 states C++ starts to look more managable.

1 Like