well I have a enemy following my Player till the enemy gets close enougn.
then it does a attack animation and after that my player should take 1 hit and then after the animation played again 1 hit again till the players hits it can take variable hits 0 and then it should debug PLayer Died! the log
but what happens is that when the enemy gets close enough the hits he takes start going up like crazy and then after that the log starts spamming the player died like 300 times and even when I'm not in the attackrange anymore he just keeps building up does anyone have a goos solution for this?
first script
var Target : Transform;
var Speed : float;
var Hit = 1;
var HitDelay : float = 1;
function Update () {
Target = GameObject.FindWithTag("Player").transform;
var distance = Vector3.Distance(gameObject.transform.position, Target.transform.position);
if(distance > 2) {
GetComponent.().Play("walk");
var Step = Speed * Time.deltaTime;
var lookPos = Target.position - transform.position;
lookPos.y = 0;
var Rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, Rotation, 1);
transform.position = Vector3.MoveTowards(transform.position, Target.position, Step);
}
else if(distance < 2) {
Attack();
}
}
function Attack() {
GetComponent.().Play("attack");
yield WaitForSeconds(HitDelay);
Target.transform.SendMessage("PlayerHitted", Hit, SendMessageOptions.DontRequireReceiver);
}
and the script that reacts to it
var PlayerCanTake = 5;
function Update () {
if(PlayerCanTake <= 0){
Debug.Log("Player died!");
//Application.LoadLevel(Application.LoadedLevel);
}
}
function PlayerHitted(Hit : int) {
Debug.Log("took hit");
yield WaitForSeconds(1);
PlayerCanTake -= Hit;
}
↧