- Home /
Found a different solution.
2D One, Two, Three Attack Combo
Hello, I need some help with this melee/combat script of mine. I want to make a 3 hit combo attack, but at the moment the two attack animations I have in the code are playing at the same time when you press the listed button. I don't know whether I've made a mistake with the timer or done something wrong in the animator.
private float attackTimer = 0f;
private float cooldown = 1.3f;
private int combo = 0;
private bool attack1;
private bool attack2;
private void MeleeAttack()
{
if (Input.GetButtonDown("Attack1") && !attack1 && attackTimer <= 0 && combo == 0)
{
attack1 = true;
attackTimer = cooldown;
playerAttackAnim.SetBool("isAttacking", attack1);
combo++;
}
if (attack1)
{
if (attackTimer > 0)
{
attackTimer -= Time.deltaTime;
if (Input.GetButtonDown("Attack1") && !attack2 && attackTimer > 0 && combo == 1)
{
TransitionToAttack2();
}
}
else
{
if (attackTimer < 0)
{
attackTimer = 0;
combo = 0;
attack1 = false;
attack2 = false;
playerAttackAnim.SetBool("isAttacking", false);
playerAttackAnim.SetBool("isAttacking2", false);
}
}
}
}
private void TransitionToAttack2()
{
attack2 = true;
attackTimer = cooldown;
playerAttackAnim.SetBool("isAttacking2", attack2);
combo++;
}
@GBA92 After perfor$$anonymous$$g attack1
, you instantly set attackTimer
to cooldown
, which is 1.3f and increase the combo
to 1. You thereafter immediately check if attackTimer > 0
(which it would be, probably even after subtracting Time.deltaTime
) and combo == 1
, which it also would be, to transition into attack2
. So it looks like your code logic does exactly that -- always do both attacks at once?
@JPhilipp Yes, but I don't want both attacks perfor$$anonymous$$g at once. I want the timer and combo to follow into the next attack, if not, reset the timer and combo and only play the first attack.
@GBA92 Exactly, I know -- I just explained to you what you were doing, which is perfor$$anonymous$$g both at once, in case you didn't understand your bug.
Answer by Firox_ · Mar 07, 2020 at 03:05 PM
First I think getting input in function is a very bad idea as the input should be get at each frame.
An other problem is that attackTimer > 0 is always true as it's only when this condition is true you decrement this timer. So for now the condition if (attackTimer > 0) is useless. The decrementation should occured the line before the condition. It would allows you to remove the same condition in the condition with the Input.GetKeyDown.
You should also set the animator bool "isAttacking1" to false in your transition function. I don't think the problem comes from here but it's cleaner since when you are in the 2nd attack the 1st should end.
Otherwise what are you calling "are playing at the same time" ? The 2 animations are occured at same time ? (which should be impossible as 2 states can't be avctive at same time so I don't think it's what you mean) Or the 2 bools are set to true together so just the 2nd animation is playing ?
Answer by GBA92 · Mar 07, 2020 at 07:37 PM
Closing this thread. I found a different solution elsewhere to fix my problem. It works a lot better than the code I was using before.
private void MeleeAttack()
{
if (timeStamp < Time.time && Input.GetButtonDown("Attack1") && combo < maxCombo)
{
combo++;
Debug.Log("Attack " + combo);
timeStamp = Time.time + cooldown;
}
if ((Time.time - timeStamp) > cooldown)
{
combo = 0;
playerAttackAnim.SetBool("isAttacking", false);
playerAttackAnim.SetBool("isAttacking2", false);
playerAttackAnim.SetBool("isAttacking3", false);
}
if (combo == 1)
{
playerAttackAnim.SetBool("isAttacking", true);
}
if (combo == 2)
{
playerAttackAnim.SetBool("isAttacking2", true);
playerAttackAnim.SetBool("isAttacking", false);
}
if (combo == 3)
{
playerAttackAnim.SetBool("isAttacking3", true);
playerAttackAnim.SetBool("isAttacking2", false);
}