Ai attack player only when in range help.

The problem im having is when i start the game in unity the zombie ai will find me from all the way across the map, i only want the zombies to find me when i get within 10ft of them, And when im not within 10ft of them i want the zombies to stand idle, If someone could help me out with that it would be a Hugh help. Heres my current script.

var myTransform : Transform; //current transform data of this enemy var isNotDead : boolean = true; var health : float = 100; function Awake() { myTransform = transform; //cache transform data for easy access/performance }

function Start() { target = GameObject.FindWithTag(“Player”).transform; //target the player

}

function Update () {

if(health < 1){
 
isNotDead = false;
animation.Play("die");
Destroy(gameObject, 1);
}
 
if(isNotDead){
 
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
 
 
var distance = Vector3.Distance(target.position, myTransform.position);
if (distance < 3.0f) {
animation.Play("attack1");
}
else{
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
animation.Play("walk1");
}
 
}

}

function ApplyDamage(dmg : float){

health -= dmg;

}

The code only checks if the distance to the player is < 3 meters, and if so attack… otherwise it moves forward (which would be towards the player). So just check the distance.

var distance = Vector3.Distance(target.position, myTransform.position);

if (distance < 3.0f) {
  animation.Play("attack1");
}
else
if(distance < 10.0f) {  // <<<<< CHECK IF CLOSE ENOUGH TO START MOVING
  //move towards the player
  myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
  animation.Play("walk1");
}

Sample uses 10 units (aka meters) rather than feet but you can use whatever distance works for you.