Hello,
I am working on my enemy AI. Basically what I am trying to do is subtract my player’s health over time when in range with the enemy. I am able to subtract health from the player when within range, but because I am checking the distance between them in the Update function, my health is being subtracted very fast. How can I make it so that the health slowly decreases? Here is my code:
THANKS!
var player : Transform;
var minTargetDist = 10.0;
var minAttackDist = 5.0;
var rotationDamping = 6.0;
private var attacking = false;
function Update () {
var distBetween = Vector3.Distance(player.position, transform.position);
if (distBetween <= minTargetDist) {
var rotation = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
else {
return;
}
if (distBetween <= minAttackDist) {
if (attacking) {
Attack();
}
else {
attacking = true;
}
}
else {
return;
}
}
function Attack() {
var damage = 2 * Time.time;
player.GetComponent(PlayerHealth).health -= damage;
}