I've created a basic attack combo system that chains 3 attacks together.
When the player clicks the mouse button, it enables a boxCollider2D attached to the player.
The boxCollider is a trigger and will cause damage to a valid target using OnTriggerEnter2D.
This doesn't seem to be very responsive, and often will not work if the target is stationary. I've thought about trying Raycasts as an alternative but am looking for advice on how to handle this properly.
public class Attack : MonoBehaviour
{
private int comboIndex;
public float lastAttack;
private Animator _animator;
private string[] attackName;
private float comboResetTimer;
private BoxCollider2D _hitbox;
private void Awake()
{
attackName = new[] {"Attack1", "Attack2", "Attack3"};
_animator = GetComponent<Animator>();
_hitbox = GetComponent<BoxCollider2D>();
}
private void Update()
{
var canAttack = Input.GetMouseButtonDown(0);
if (canAttack && comboIndex < attackName.Length)
{
_animator.SetTrigger(attackName[comboIndex]);
_hitbox.enabled = true;
comboIndex++;
comboResetTimer = 0f;
}
if (comboIndex > 0)
{
comboResetTimer += Time.deltaTime;
if (comboResetTimer > lastAttack)
{
_animator.SetTrigger("Reset");
comboIndex = 0;
_hitbox.enabled = false;
}
}
}
}