- Home /
              This question was 
             closed May 09, 2019 at 11:01 AM by 
             adovehv for the following reason: 
             
 
            The question is answered, right answer was accepted
 
               Question by 
               adovehv · May 06, 2019 at 07:19 PM · 
                coroutineenemy aiattackcoroutine errors  
              
 
              Enemy attack constantly with a coroutine
Hi! I have a enemy with a remote attack, I want the enemy attack while I am in their attack range, with x seconds between attacks. I have this code but it only works once, while I'm in range it does not work, I have to go out and enter its range to make it work. What I can do? Thanks :)


               Comment
              
 
               
              Answer by MichaI · May 06, 2019 at 07:45 PM
You should change currentState after attack to EnemyState.idle or EnemyState.walk in order to enter " if (currentState == EnemyState.idle || currentState == EnemyState.walk && currentState != EnemyState.stagger) " statement again.
You can add this in your IEnumerator like this:
    IEnumerator Attack(float seconds)
    {
      attacking = true;
      if (target && projectile != null)
      {
        Instantiate(projectile, transform.position + (transform.forward * 1.5f)
        + (transform.up * 0.3f), Quaternion.identity, transform);
        yield return new WaitForSeconds(seconds);
      }
      attacking = false;
      ChangeState(EnemyState.idle);
    }
 
                   koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                
