in my current script i have an ai that uses navmesh to avoid wallls and attacks the player when it gets near, my problem is i cant make it only attack once, so it instantly kills you when it gets near you.
any idea on how to stop this?
vartarget : Transform;
varisChasing : boolean = true;
varisAttacking : boolean = false;
varNavComponent : NavMeshAgent;
varthisObject : Transform;
varstopDistance : int;
varisInTeleporter : boolean = false;
varminSpeedWalk : int;
varmaxSpeedWalk : int;;
varminSpeedRun : int;
varmaxSpeedRun : int;
varminSpeedAttack : int;
varmaxSpeedAttack : int;
varisRunner : boolean;
varcurSpeed : int;
varmoveAnimName : String;
varwalkAnimName : String;
varrunAnimName : String;
varattackAnimName : String;
varidleAnimName : String;
vartimeToAttack : int;
varmaximumHitPoints = 5.0;
functionAwake(){
thisObject = transform;
}
functionStart(){
target = GameObject.FindWithTag("Player").transform;
NavComponent = thisObject.transform.GetComponent(NavMeshAgent);
if (isRunner){
curSpeed = Random.Range(minSpeedRun, maxSpeedRun);
moveAnimName = runAnimName;
}
if (!isRunner){
curSpeed = Random.Range(minSpeedWalk, maxSpeedWalk);
moveAnimName = walkAnimName;
}
NavComponent.speed = curSpeed;
}
functionUpdate(){
varcurDistance = Vector3.Distance(target.position, thisObject.position);
NavComponent.stoppingDistance = stopDistance;
if (curDistance <= stopDistance){
isChasing = false;
isAttacking = true;
}
if (curDistance > stopDistance){
isChasing = true;
isAttacking = false;
}
if (isChasing == true){
NavComponent.SetDestination(target.position);
NavComponent.Resume();
thisObject.animation.CrossFade(moveAnimName);
}
if (isChasing == false){
NavComponent.Stop();
if (isAttacking == false){
thisObject.animation.CrossFade(idleAnimName);
}
}
if (isAttacking == true){
//target.SendMessage( "PlayerDamage"), maximumHitPoints);
//target.GetComponent("PlayerDamageNew").PlayerDamage("5");
thisObject.animation.CrossFade(attackAnimName);
waitFunction();
}
}
functionwaitFunction(){
yieldWaitForSeconds (timeToAttack);
//target.GetComponent("PlayerDamageNew").PlayerDamage(5);
}
functionPlayerDamage(damage : float){
print ("PlayerReceives" + damage + "Damage!");
}