Triggering Attacks with Key Combinations in Unity
In fighting games, we often see special combos performed by pressing a few keys in quick succession. I'll show you how to implement this in Unity.
First, you'll need a character asset. You can download one from here: (https://smoulderblock.itch.io/human-male-model)
Select a combo animation. Drag and drop the character into the scene. This will automatically create an Animator and animation files. Go to the Animator file and make the necessary changes.
We'll add two states: one for the combo animation and another for an idle/waiting animation. Also, create a boolean parameter named "Combo".
Create a transition from the idle state to the combo state, conditioned on the "Combo" boolean being true. Create another transition in the opposite direction, from the combo state back to the idle state, with the "Combo" boolean set to false.
Now let's move onto the coding. Open a new script on the GameObject containing your character.
We'll need three variables:
private float comboTimer;
private string Combos;
[SerializeField] private Animator animator;
The comboTimer variable will track time, resetting after a certain duration. This is necessary because if the combo isn’t entered within a specific timeframe, the attack won’t trigger.
The Combos string will store the key combination that has been pressed.
The animator variable needs to be assigned in the editor. It allows us to control the character’s animation.
Inside the script, create a function called input() to capture the input for the combo:
private void input()
{
if (Input.GetKeyDown(KeyCode.B))
Combos += "B";
else if (Input.GetKeyDown(KeyCode.A))
Combos += "A";
}
Each key has its corresponding letter. This letter is appended to the Combos string, passing the information to the next step.
Next, create a function called ComboAttack. This function will trigger our combo when the 'B' and 'A' keys are pressed in sequence:
private async Awaitable ComboSlideAttackFunc()
{
if (Combos == "BA") // Check for the "BA" sequence.
{
Combos = ""; // Reset the combo string so it can work again.
animator.SetBool("Combo", true); // Start the animation.
await Awaitable.WaitForSecondsAsync(0.1f); // Wait for the animation to play.
animator.SetBool("Combo", false); // Stop the animation from repeating.
}
}
The combo should now be working, but if you press 'B' and wait for 5 minutes before pressing 'A', the combo will still trigger. This isn't the behavior we want. We need to add a timer to ensure the keys are pressed within a certain timeframe.
Let’s create a ComboReset function.
private async Awaitable ComboReset()
{
if (Combos.Length > 0) // If any key has been pressed
{
// Count the time
float time = Time.deltaTime;
comboTimer += time;
// If 0.25 seconds have passed, reset the combo
if (comboTimer > 0.25f)
{
comboTimer = 0f;
Combos = "";
}
}
else // If no key has been pressed, reset the timer.
comboTimer = 0f;
}
Finally, call all the functions in the Update() function.
protected void Update()
{
input();
ComboReset();
ComboAttack();
}
Save the script and test the combo. I hope this tutorial has been helpful!