I know that this has been discussed like a hundred times, but I really need some help. I have an enemy AI script that allows the enemy to duck behind cover, and chase you while avoiding objects. All my enemy does though, is walk in the opposite direction, then off the edge of the map. If anyone knows what’s wrong with my script, or knows better ways to get the things done which are done in this code, it would help a lot, thanks.
var follower : Transform;
// This is the speed with which the follower will pursue
var speed : float = 2.0;
// This is the range at which to pursue
var chaseRange : float = 10.0;
var enemyDead : float = 2.0;
var minDistance= 4;
var calledBefore : boolean = false;
var leader : Transform;
var deadEnemy : GameObject;
var EnemyController : GameObject;
var Enemy : GameObject;
var coverBoxes : GameObject[];
var goBehindCover : boolean = false;
function Start(){
Enemy = this.gameObject;
EnemyController = GameObject.FindWithTag("EnemyController");
leader = GameObject.FindWithTag("Player").transform;
deadEnemy = GameObject.FindWithTag("SpawnControl");
Enemy.transform.parent = EnemyController.transform;
animation.wrapMode = WrapMode.Loop;
animation["death"].wrapMode = WrapMode.Once;
animation["idle"].wrapMode = WrapMode.Once;
animation["attack"].wrapMode = WrapMode.Once;
animation["Walk"].layer = 0;
animation["idle"].layer = 0;
animation["Attack"].layer = 1;
animation["death"].layer = 2;
animation.Stop();
coverBoxes = GameObject.FindGameObjectsWithTag("CoverBox");
}
// This is used to store the distance between the two objects.
private var range : float;
function Update(){
var dir = (leader.position - transform.position).normalized;
var rot = Quaternion.LookRotation(dir);
var leftR = transform.position;
var rightR = transform.position;
leftR.x -= 2;
rightR.x += 2;
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 20)){
if(hit.transform != transform){
Debug.DrawLine(transform.position, hit.point, Color.red);
dir += hit.normal * 50;
}
}
else if(Physics.Raycast(leftR, transform.forward, hit, 20)){
if(hit.transform != transform){
Debug.DrawLine(leftR, hit.point, Color.red);
dir += hit.normal * 50;
}
}
else if(Physics.Raycast(rightR, transform.forward, hit, 20)){
if(hit.transform != transform){
Debug.DrawLine(rightR, hit.point, Color.red);
dir += hit.normal * 50;
}
}
if(goBehindCover){
var target : Vector3;
for(var i : int = 0; i < coverBoxes.length; i++){
if(Vector3.Distance(coverBoxes[i].transform.position, transform.position)<20){
target = coverBoxes[i].transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
follower.Translate(follower.forward * speed * Time.deltaTime);
animation.CrossFade("Walk");
}
}
}
else if (range <= chaseRange ){
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
follower.Translate(follower.forward * speed * Time.deltaTime);
animation.CrossFade("Walk");
}
// Calculate the distance between the follower and the leader.
range = Vector3.Distance( follower.position,leader.position );
if (Vector3.Distance(transform.position, leader.position) < minDistance){
follower.Translate(Vector3.zero);
animation.CrossFade("Attack");
return;
}
else{
animation.CrossFade("idle");
}
if(hitPoints <= 0){
follower.Translate(Vector3.zero);
speed = 0.0;
Invoke("isDead", 1.95);
}
else {
// The follower is out of range. Do nothing.
return;
}
//End else (if ( range <= chaseRange ))
} // End function Update()
var hitPoints = 1.0;
function ApplyDamage (damage : float)
{
hitPoints = hitPoints - damage;
if (hitPoints <= 0.0)
{
follower.Translate(Vector3.zero);
animation.CrossFade("death");
if(calledBefore == true){
return;
}
else{
EnemyisDead();
Kill();
}
}
}
function Kill(){
Destroy(this.gameObject);
}
function EnemyisDead(){
var spawnAnother : EnemySpawn = deadEnemy.GetComponent(EnemySpawn);
spawnAnother.enemyHasDied();
}