ive tried multiple times to add the ability for my ai (a zombie) to attack and to be able to recieve damage and die when it’s health hits zero, below is my ai script allowing it to use a navmesh to track and follow the player and play animations (walking, running, attacking, etc), any help would be vastly appreciated as i despise working with AI and i dont want it to cause the end of my game -.-
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;
functionAwake(){
thisObject = transform;
}
functionStart(){
target = GameObject.FindWithTag(“Player”).transform;
NavComponent = thisObject.transform.GetComponent(NavMeshAgent);
if (isRunner == true){
curSpeed = Random.Range(minSpeedRun, maxSpeedRun);
moveAnimName = runAnimName;
}
if (isRunner == false){
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”);
thisObject.animation.CrossFade(attackAnimName);
waitFunction();
BroadcastMessage (“StopAttacking”);
}
}
functionwaitFunction(){
yieldWaitForSeconds (timeToAttack);
}