Finite State Machine Issue - Attacks Repeating

I believe I’ve built a functional Finite State Machine for my game, but I’m running into a persistent issue with animations. Specifically, while all the main animations work fine, adding in an attack when a button is pressed is causing the animation to continue over and over for as long as the attack key is pressed, or to stick with the attack animation (just once) until another button is pressed. The latter of these is what I am seeing right now.

I set up a timer called attackTimer that is meant to give me a variable to work with for this. I meant for it to be a cooldown, but no matter how many times I implemented it with variying delays, I still had issues. The attack animation itself is very quick, 0.05s, and I had things set so when the F key (my attack button for now) is pressed, the animation changes to Attack1. At this point the timer is started, and the attack animation is played. Only when the timer was past 0.05s would the attack button repeat the action, and if it wasn’t pressed, after 0.05s the character would return to idle (or any other state, i.e. falling or jumping).

What would happen though is this: holding down the F key caused the player to attack over and over on a loop until released. Alternatively, by moving the attackTimer requirement around, I was able to get it to play the attack animation only once but if I was to move, jump, fall, or anything else while still holding the F key, the animation would start over again.

My main goal here is this - the player can run, jump, fall, etc. and if the F key is hit, it will animate an attack (and create an object for checks), but when finished, return to the state the player would otherwise be in. I want to be able to attack on the run, or when jumping, without causing the entire animation process to stop or allowing the player to attack endlessly by just holding the key down… like a metroidvania would allow. Hope that makes sense.

I feel like I’ve tried 100 things and it’s not working. Hoping maybe someone here can help? Here are some screenshots of the main “Finite State Machine” that I thought I build right:

As a new user here I can only post one image, but here’s one of the functions I made:

The same thing applies essentially for each movement option - idle, falling, running, and jumping.

Hello and welcome!

It looks like you are doing pretty good tackling an advanced topic. It might be just that your events are conflicting somewhere or maybe the timers are off in one place and mess it all up. Sometimes it’s hard to pinpoint.

I pulled the attack sequence out of the movement events so I could use a boolean variable to trigger the other movements when the attack is done. Not sure if you could do this or if it would not work for your Finite State Machine - I don’t use them like the Wiki so I’m only guessing what you’ve got going on. Also I don’t do many projects with player controlled characters. So with this warning that I’m not the best to help here, I have some snips that you might be able to work into your Finite State Machine or give you some ideas.

This first snip shows the player can hold the F down and it will keep attacking as soon as the animation is done. I was mainly checking if the boolean variable was working to send the Player back to the correct animation after the player released F.

The second one has a timer added. Note because of the platform behavior the Player still moves forward while attacking when the player is holding F if they are also holding the movement key. So if you want it to stop from a run during an attack you would need to add that.

I foresee difficulties with the Hit sequence. Not sure how you’ve got Hit set up if the player happens to be pressing F. Also none of this deals with flipping the Playere when facing the opposite direction. Anyway you could tweak it how you like it, and hopefully some more knowledgeable people about Finite State Machines for player controlled characters will step in with more specific help.