When clicking LMB, the Attack function below is called. However, in its current implementation, it sends the ApplyDamage message instantly and I would like it to wait half a second so that the damage can be applied in sync with the animation. Code below:
void Attack()
{
if (Time.time > nextAttack)
{
Collider[] colliders = Physics.OverlapSphere(transform.position, AttackRange);
foreach (Collider hit in colliders)
{
if (hit.tag == "Enemy" && Vector3.Distance(hit.transform.position, transform.position) <= AttackRange)
{
hit.SendMessage("ApplyDamage", AttackDamage);
}
}
nextAttack = Time.time + AttackRate;
}
}
I would have liked to use Invoke but I cannot pass the hit Collider through Invoke. I also thought about making a global variable but by the time the Invoked function could execute, the global variable might have changed as the for loop searched for enemies. It looks like my only option is to make a global array of Colliders, add a Collider to the array when it detects and enemy and after searching, send the ApplyDamage message to all Colliders in the list. Is there a better way to do this?