Now that the animation is all set-up, the next thing I needed to implement was the attack state for the player. For my game, I'm currently going to have a combo system where the player can press the attack button multiple times to chain a melee attack. In the future, I might look at having different melee attacks such as strong attacks and special attacks, but for now I'll focus on the standard melee attacks.
First, I needed to add a new state class that the player can enter for attacking. I added multiple variables to this class:
Next, I overrode the OnMessageReceived method, which is where the bulk of the logic will be as the state will handle messages from the animation - this method looks like the following:
First, I needed to add a new state class that the player can enter for attacking. I added multiple variables to this class:
- The animation instance - used to trigger the attack animation montage
- An int32 for the current combo count - this is to keep track of how many attacks the player has completed
- An int32 for the maximum combo count - used to prevent too many attacks
- A bool for whether or not a combo has been chained - this prevents spamming of the attack button
- A bool for whether or not the player is in the "combo area" - this way, the player can only chain attacks between certain frames of the animation
Next, I overrode the OnMessageReceived method, which is where the bulk of the logic will be as the state will handle messages from the animation - this method looks like the following:
In the OnAttackStart and OnAttackEnd, I will eventually activate the weapon however for now I have activated & deactivated the combo area. The OnAttackComplete message resets the playNextAttack boolean so that the player can trigger another combo attack. Finally, the OnAnimationComplete message will change the state to the Idle state.
Next I have overridden the the OnActionInputReceived method, where I check if the input is the Attack input, and if it is the player can attempt to combo an attack. The method looks like the following:
Next I have overridden the the OnActionInputReceived method, where I check if the input is the Attack input, and if it is the player can attempt to combo an attack. The method looks like the following:
Finally, I overrode the OnExit method to stop the attack on the animation instance.
I had to create a subclass of the animation instance so I could add my own custom methods for performing the attacks. I added a variable for the animation montage of the attack, and 3 methods - StartAttack, ChangeAttack and StopAttack:
I had to create a subclass of the animation instance so I could add my own custom methods for performing the attacks. I added a variable for the animation montage of the attack, and 3 methods - StartAttack, ChangeAttack and StopAttack:
In the ChangeAttack method, I form the section names for the montage & tell the montage to set the next section. The StartAttack method simply starts the montage, and the StopAttack method stops the montage. Once this class was complete I re-parented the animation blueprint of the player to my new class.
The last step was to add the state to the PlayerController. Now, the player can attack:
The last step was to add the state to the PlayerController. Now, the player can attack:
In the next post I will discuss how I implemented the hitbox & the particle effect for the attack.