The Combat State Machine

At the heart of any melee combat system is what I call the combat state machine. This is a state machine that controls any entity that’s going to be directly involved in the melee. It’s important to note that a game might not explicitly implement their system using a state machine, however the behavior of the game will still likely fall in line with the principles I’m covering. The following is the core Combat State Machine.


So, let’s go through it shall we. First off the initial starting state of this machine is the Idle state. It is called Idle not necessarily because the character is idling, but because he is idling in terms of combat. You can call this state Neutral if it’s easier to think about it that way.

Idle State: The Idle state is the hub of the entire state machine. It represents the state where the player or AI can take an action. As we go deeper into studying the state machine we’ll see that exiting and entering the Idle state begins and ends a single combat sequence. What that means will depend on the game; it can mean completing a combo, finishing a quick time event, or whatever else the designer can come up with. For now I’ll keep it generic. I like to borrow a term from music and refer to one of these sequences as a “phrase”. A player will likely end up spending most of his or her time within the Idle state. Non-combat elements like movement, jumping, and interacting with the environment will occur while in the Idle state. Those elements will have their own implementation that has little to do with the combat state machine, but as far as the combat system is concerned the player or AI is idling.

The Idle state has a transition to every other state. Transitioning to the Attack state or the Defense state is an active transition, meaning that the transition is caused by some action that the player knowingly decided to take. Transitioning into the Hit Reaction state is a more passive transition, meaning that the transition was the side effect of something that happened to the player and requires no direct action. Leaving the Idle state means that the player no longer has total power over how the state machine will flow and instead is at the mercy of the rules of combat. The Transitions from the Idle state are as follows:

Idle → Attack: Player Initiates Attack (Generally via a button press)
Idle → Defense: Player Initiates Defense (Generally via a button press)
Idle → Hit Reaction: Player or AI collides with someone else’s attack

Attack State: No mystery here. While the machine is in the attack state the character that the state machine controls is in the middle of an attack. As I mentioned before a state machine can only be in one and only one state at a time. This means that while the player is attacking he doesn’t have access to the behaviors in the idle state. Usually this means he cannot move, or at least cannot move as freely. In general the player must wait until he transitions back into the Idle state before having access to his entire range of choices again. Here Attack has only one transition, and that’s back to Idle.

Attack → Idle: Attack ends (Generally means an animation finishes)

The important thing here is that there is a perceptible amount of time spent in the Attack state. It is not the case that you initiate the attack and immediately return back to idle. As described in the transition description a player must wait for the attack to finish before returning to Idle. Though the amount of time spent in the Attack state is usually defined by the length of the attack animation, what actually constitutes as the end of an attack is up to the designer. Just remember though that returning to the Idle state means that the player has full discretion as to what action to perform; this includes attacking again.

Hit Reaction State: The machine will enter this state when someone else’s attack successfully hits the character. An interesting note is that if one machine entered the Hit Reaction state that means that another machine is in the attack state. This is assuming that the environment did not cause the hit react, which will be the case in many games. The Hit Reaction state has only one transition.

Hit Reaction → Idle: Hit Reaction ends (Generally mean an animation finishes)

Just like with the Attack state a Hit Reaction almost always means an animation plays. Exiting the Hit Reaction state means that the animation has finished playing, but as always what actually constitutes a hit reaction is up to the designer. As I mentioned before, where there is a hit reaction there is an attack, and one of the purposes of the hit reaction is to reward the attacker for making a successful attack. Another very important purpose of the hit react is to allow the attacker to finish his attack. Because the attacker is in the attack state he does not have access to his full range of actions. He is making himself vulnerable to someone else’s attack. By successfully landing his attack and putting his target in the Hit Reaction state the target also no longer has access to its full range of actions. This exchange between attack and hit reacts is what I referred to before as a combat phrase and the phrase ends once the target returns to its Idle state. This relationship between attack and hit reaction is very important to keep in mind, and the amount of time spent in the Hit Reaction state needs to be comparable to the attack that caused it. This is one of the big areas where balancing needs to be done, but more on that later.

Defense State: The purpose of the defense state is, as the name suggests, to defend against attacks. It’s the rock to attack’s scissors. If an attacker lands an attack while his target is idle, then the attack is a full success and the target goes into the Hit React state. If the attack lands while the target is in the Defense state then the attack is not a full success and something else happens. Now that something is left completely to the imagination of the designer. The defense state can be something like a block where the target hit reacts but does not take damage. There are games that use the defense state for a dodge that have their own rules of success and failure based on timing. A new trend that’s emerging is that defending means countering an attack where a successful counter initiates its own attack. The defense state is much more fluid than the other states in the combat state machine and beyond negating an attack it does not have a concrete definition. However regardless of what “defending” means it occurs in the Defense state. Here’s the Transition:

Defense → Idle: Defense ends (meaning is game specific)

Some games like platformers don’t have a Defense state. This is ok because melee combat is not a central part of that game experience, but any game where melee needs to be even a little more complex should try to come up with some kind of Defense mechanism, but it certainly doesn’t have to be the traditional block or dodge.

We now have all the parts for a combat system and a basic understanding of their functions. However the combat state machine as I have explained it so far is only a bare bones version. If you implemented a game with a combat system that followed only the current machine it probably wouldn’t be very interesting, and even might lack some functionality that the player is expecting. Now, everything I’ve talked about so far is important and I don’t recommended taking anything away from the core combat state machine. Adding to it though is highly recommended and is how we’re going to come up with some cool combat mechanics.

So let’s start adding.


First we’re going to add a transition from Attack to Hit React. This transition is used to handle any situation where a character gets hit while in the middle of an attack. This situation can come from any number of sources. It can happen when the player is attacking an enemy and a second enemy attacks the player from a different direction or from afar with a ranged attack. It can happen when an enemy attacks the player and the player reacts with a faster attack and hits the enemy first. The key point here is that once the character leaves the Attack state his attack is interrupted. This can become an interesting tactical decision for a player, whether or not to attempt to counter an enemy attack or simply defend against it. In the case where it is the enemy interrupting the player’s attack it can be an interesting obstacle that the player must strategize against. In any case this transition has become a mainstay in combat systems because the idea of a counter attack or beating your opponent to the punch, as the saying goes, is intuitive to players and opens the door for creating interesting options for the player when he chooses what kind of attack to execute.

As a side note this transition is one of the reasons that the combat state machine generally does not apply to shooter games. In those kinds of games instead of an animation length defining the attack state it’s the flight of the bullet to the enemy that would be considered the attack. It is usually not desired that if you are hit in a shooter that you not be allowed to fire back (attack) until a hit reaction finishes occurring. Also because the firing of a bullet is near instantaneous the idea of interrupting an attack does not make sense from a player expectation stand point. Though, it is my firm belief that if approached the right way, using the combat state machine in a shooter setting can produce some unique gameplay experiences.

Alright let’s keep going through some of the more interesting and common additions to the combat state machine.


I have now added two new transitions. One that goes from attack to itself and, because where ever there is a successful attack there is a corresponding Hit Reaction somewhere, one from Hit Reaction to itself. If you are confused by the idea of a transition that goes from a state back into the same state then you can think about it by hitting the reset button on the state. Normally after you have spent some time in a state a good part of its logic has run. If you want to start that logic all over again then exiting and reentering that state is a method to do that. Though it is not difficult to create logic that returns to its beginning, I personally find using states that transition into themselves useful. It keeps your thought process consistent and it allows you to represent the concept graphically.

The transition from Attack to Attack means that while you are attacking you may execute another attack before the first attack ends. Correspondingly for the transition that goes from Hit Reaction to itself means that before you finish hit reacting another hit lands and you begin a brand new hit reaction. Passing through these transitions is what is usually referred to as a combo. Now right away it is easy to see the potential for an imbalance or an exploit, and it’s important to understand that, unlike the Idle to Attack transition, these transitions need to follow strict rules. What these rules are will often times define the main difference between combat game X and combat game Y. I will go over some examples of these in a later section.

<< Prev Home Next >>