Help with Enemy AI

Hi there I’m trying to find a simple enemy AI script so that the enemy follows the player and stops when it reaches a certain distance when close to the player, any help would be nice thanks. :slight_smile:

var target : Transform; //Set this to the player in the inspector
var distance = 5.0;
var speed = 5.0;

   function Update() {
        var direction = (target.position - transform.position);
        //If you don't want the follower to move in a particular axis set it to 0 here
        //e.g. direction.y = 0;
        if(direction.magnitude > distance) {
            transform.position = transform.position + (direction.normalized * speed * Time.deltaTime);