title: Trying to get enemy to deal damage every set seconds (Unity)
body: <p>I am creating a 3D Shooter and I am trying to make an enemy deal damage to the player every set seconds. I have made the enemy deal damage with a raycast but it deals the damage way too fast. </p>
  
  <p>I thought using yield return new WaitForSeconds(2) would take 1 damage away from the player every 2 seconds but it deals damage to the player a lot faster.</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class EnemyMove : MonoBehaviour
  {
      public Transform target;
      public Transform player;
      public float enemySpeed;
      public int moveTrigger = 1;
      public int isAttacking;
  
      public float distanceFromPlayer;
  
      void Update()
      {
          distanceFromPlayer = Vector3.Distance(target.transform.position, player.transform.position);
  
          if (distanceFromPlayer &lt;= 10 &amp;&amp; moveTrigger == 1)
          {
              transform.LookAt(target);
              StartCoroutine(EnemyDamage());
          }
          if (distanceFromPlayer &lt;10 &amp;&amp; moveTrigger == 1 &amp;&amp; distanceFromPlayer &gt;3)
          {
              transform.Translate(Vector3.forward * enemySpeed * Time.deltaTime);
          }
      }
  
      IEnumerator EnemyDamage()
      {
          RaycastHit PlayerHit;
          if (Physics.Raycast(target.transform.position, target.transform.forward, out PlayerHit))
          {
              Debug.Log(PlayerHit.transform.name);
              Target target = PlayerHit.transform.GetComponent&lt;Target&gt;();
              if (target != null)
              {
                  yield return new WaitForSeconds(2);
                  GlobalHealth.playerHealth -= 1;
                  yield return null;
              }
          }
      }
  }
  </code></pre>