I have two functions , I want harmPlayer() should be called after every 5 seconds from Update() function. But the it is getting executed several hundred times from update() functions. I understand the Update() function is getting executed on each frame and is calling everytime the harmPlayer(), then how can i implement wait for 5 seconds'
IEnumerator HarmPlayer()
{
Debug.Log("Inside Harm Player");
yield return new WaitForSeconds(5);
Debug.Log("Player Health is the issue");
}
Here is my Update() function
void Update () {
transform.LookAt(target);
float step = speed * Time.deltaTime;
distance = (transform.position - target.position).magnitude;
if (distance < 3.5)
{
animator.SetFloat("Attack", 0.2f);
StartCoroutine("HarmPlayer");
}
}