WaitForSeconds doesn’t work if it’s not in a coroutine. As you’ve written it, it does nothing. You’re actually just declaring a value and not assigning it to a variable. I’ll leave it up to you to look up coroutines if you aren’t familiar with them.
However, I think you’d be better rolling your own timer. When a player enters the trigger, reset the initial timer value to 0. Then while in the stay, keep adding to eat. (Usually just adding Time.DeltaTime). If that value goes above a number like 2, trigger the attack. If not, just keep adding. If an attack is triggered, reset the value to 0.
If the player leaves the trigger, you could reset it there instead. Just whatever feels better for you.
Oh, @kdgalla posted this on another post about waitForSeconds and I feel like it’s a good explanation here also.
alternatively you could also start a coroutine on trigger enter that yields a waitForSeconds in a loop and verify if conditions are still met after given time
private void OnTriggerEnter2D(Collider2D collision)
{
StartCoroutine(Attack(.5f));
}
private IEnumerator Attack(float interval)
{
// Or any more specific condition
while (true)
{
if (ConditionNotMet)
yield break;
DoAttack();
yield return new WaitForSeconds(interval);
}
}
What I meant with my post is that if farrico decided to go for WaitForSeconds, coroutine was one way of handling it.
You are right to add a CAUTION note, I didn’t dive into too much detail but checking that the coroutine isn’t already running is indeed crucial.
I however don’t think coroutines are necessarily a bad idea for this use case as long as they’re used right. A timer is also another way to go