Hi, I am working on a AI. It follows the player etc. I am however stuck on getting the AI to damage the player. I seem to be able to get it to actually do the damage, however it does it all in a split second. What I mean is I have set the coroutine to pause every x amount of time, however it seems to just pause for x amount of time than automatically damage the player/kill the player all at once in a second. Here is a snippet of my code maybe someone can explain what is wrong? Thanks in advance.
public class AI : MonoBehaviour
{
//Variables
public Vector3 targetPosition;
private Vector3 targetDistance;
public int attackRange;
private bool ableToAttack;
public int attackDamage;
public float attackPause;
private bool alwaysLoop = true;
public void Start()
{
StartCoroutine(attackWait());
}
void Update()
{
if (targetDistance.sqrMagnitude < attackRange)
{
animation.Stop("run");
animation.Play("attack01");
ableToAttack = true;
}
else
{
ableToAttack = false;
}
}
IEnumerator attackWait()
{
while (alwaysLoop == true)
{
if (ableToAttack == true) // checks if it is able to attack the player
{
GameObject.Find("Player").SendMessage("Damage", attackDamage, SendMessageOptions.DontRequireReceiver);
//I am currently using a UFPS player, where this is how the damage is sent to the player
yield return new WaitForSeconds(attackPause);
//this pauses the ai (not sure if this is working properly)
}
yield return new WaitForSeconds(attackPause);
}
}
}