First, let’s resize the HitCollision, which is currently too large.
Also, since we’re using a Mega Man–style system, the player’s body itself doesn’t need an attack hitbox. (If we were creating melee attacks, we would keep it, but for this tutorial we won’t be adding melee combat.)
So, we’ll go ahead and remove the AttackArea2D entirely.
Select HitCollision.
Drag its corners to resize it so it’s just slightly larger than the wall collision box.
When the player is hit by an enemy’s attack, they should transition to a “Take Damage” state.
In this state, the player will be unable to move for a short time, be knocked back slightly (opposite to their facing direction), and then, after a short delay, return to their previous state.
The question is: from which states should this transition occur?
Since the Player already has seven states, creating links from every single one would be tedious.
This is where the AnyState box comes in handy.
As the name suggests, AnyState means “from any state.”
Links created from AnyState will transition if their conditions are met, regardless of the current state.
Additionally, there is a special execution action called “TransferToAction”, which allows you to return to the state you were in before the transition, ignoring normal links.
This makes AnyState ideal for handling reactions like taking damage.
Creating the “Take Damage” State
In the “Take Damage” state, we’ll configure:
Movement is disabled.
Facing direction cannot be changed.
The player moves backward (opposite the facing direction).
After 0.2 seconds, they return to the previous state.
Right-click the space to the right of AnyState and select Add State.
Rename the new State001 to Take Damage.
In the Select Animation field, select DamageTaken.
Repeated practice in object creation by making various types of game objects
The concepts of Collision Layers and Collision Masks
Taking damage and invincibility
How to use AnyState
However, while we now have working attack and damage systems, the game still has some shortcomings: the screen feels small, there’s no sound, and there’s no game over.
As it stands, it doesn’t quite feel like a complete game.
In Chapter 4, we’ll work on making it function properly as a real game.