I know this is kind of a noob question but I really need some quick help, I have this simple AI script that allows an object to follow the player on a navmesh, but for the life of me I cant figure out how to get the object to only follow me when I am in a certain distance of it, and when I am out of reach, the object will stop following the player. The javascript i have is below.
var target : Transform;
var navComponent : NavMeshAgent;
var anim : Animator;
function Start () {
target = GameObject.FindWithTag("Player").transform;
navComponent = this.transform.GetComponent(NavMeshAgent);
anim = GetComponent("Animator");
}
function Update () {
if(target) {
navComponent.SetDestination(target.position);
anim.SetFloat("speed",1);
}
}