Enemy Movement Problem

var target1 : Transform;
var moveSpeed : int;
var rotationSpeed : int;
var maxDistance : int;
var minDistance : int;

    private var myTransform : Transform;
    
    function Awake() {
    	myTransform = transform;
    }
    
    
    function Update() 
    {
    	if(target1) {
    		var dist = Vector3.Distance(target1.position, transform.position);
    	}
    	if (dist <= minDistance) {
    		print ("I found you"+ dist);
    		myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
                                           Quaternion.LookRotation(target1.position 
                                           - myTransform.position) , rotationSpeed 
                                           * Time.deltaTime);
			//if enemy is farther than 2 units away
			if (Vector3.Distance(target1.position, myTransform.position) > maxDistance) {
		// Move towards target
	                     myTransform.position += myTransform.forward 
                                                    * moveSpeed * Time.deltaTime;
		
		}
		
		if (dist >= minDistance){
			 Idle();	
			}
		}
	}
   var walkSpeed = 3.0;
var rotateSpeed = 30.0;
var directionTraveltime = 2.0;
var idleTime = 1.5;

//------------------------------------------------------------------
private var timeToNewDirection = 0.0;

var target : Transform;

// Cache the controller
private var characterController : CharacterController;
characterController = GetComponent(CharacterController);


//------------------------------------------------------------------
function Start ()
{
	var go = GameObject.FindWithTag ("Player");
	target1 = go.transform;
	maxDistance = 2;
	
	if (!target)
		target = GameObject.FindWithTag("Player").transform;
	yield WaitForSeconds(idleTime);
	
	while (true)	
	{
		// Idle around and wait for the player
		yield Idle();
		
	}
}


function Idle ()
{
	// Walk around and pause in random directions unless the player is within range
	while (true)
	{
		// Find a new direction to move
		if(Time.time > timeToNewDirection)
		{
			yield WaitForSeconds(idleTime);
			
			if(Random.value > 0.5)
				transform.Rotate(Vector3(0,5,0), rotateSpeed);
			else
				transform.Rotate(Vector3(0,-5,0),rotateSpeed);
				
			timeToNewDirection = Time.time + directionTraveltime;
		}
		
		var walkForward = transform.TransformDirection(Vector3.forward);
		characterController.SimpleMove(walkForward * walkSpeed);
					
		yield;
	}
}

this is currently my enemy ai script. My enemy will randomly walk around and when the player gets within a certain “min” distance of the enemy he will start to chase the player until you move out of his “min” distance. The problem is when ever the enemy gets within 2 units of you hes not supposed to come any close. Currently with this script the enemy will walk around randomly like hes supposed to and chase the player correctly. However, when he gets close he will come collider to collider and start to circle the player. Any tips on where I went wrong. and how i could fix this?

Idle is a looping Coroutine, so once you start it, you are permanently wandering around – even when you get too close, Idle is still running, pushing you forward. Each time you call Idle() again, you make one more copy running (so you should be speeding up?) You also seem to check for greater than MAX_DIST inside the if for less than MIN_DIST, so it will never trigger.

A hack if you really want idle as its own Coroutine is to start it once, in Start, but set bool isIdling = false;. Then wrap everything in idle’s while(true) in a big if(isIdling). In your AI section (in Update,) set isIdling to true/false to idle or not.

You had big problems in your original script caused by the yield instructions. The Idle function was called many times in Update and in the loop initiated at Start, and it caused lots of idleTime delays being generated before others ended. It produced several out of phase delays and direction changes running in parallel, which were not stopped by chase or attack modes. I changed your script a lot, but now it does what you intended to do:

var target1 : Transform;
var moveSpeed : int = 6;  // chase speed
var rotationSpeed : int = 1;  // speed to turn to the player
var maxDistance : int = 2;  // attack distance
var minDistance : int = 20;  // detection distance

private var myTransform : Transform;

function Awake() {
    myTransform = transform;
}

// Cache the controller 
private var characterController : CharacterController; 
characterController = GetComponent(CharacterController);

function Start () { 

	target1 = GameObject.FindWithTag ("Player").transform; 
	maxDistance = 2;
}

//------------------------------------------------------------------ 

function Update(){

    if (target1) {
        var dist = Vector3.Distance(target1.position, transform.position);
		if (dist > minDistance){  // if dist > minDistance: enters idle mode
			Idle(); 
		}
		else
		if (dist <= maxDistance) {  // if dist <= maxDistance: stop and attack
			print ("Attack!");
		} 
		else {  // if maxDistance < dist < minDistance: chase the player
			print ("I found you "+ dist);
			myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
										   Quaternion.LookRotation(target1.position - myTransform.position) , 
										   rotationSpeed * Time.deltaTime);
			// Move towards target
			characterController.Move(myTransform.forward * moveSpeed * Time.deltaTime);
		}
    }
}

//------------------------------------------------------------------ 

var walkSpeed = 3.0; 
var directionTraveltime = 2.0; 
var idleTime = 1.5;
var rndAngle = 45;  // enemy will turn +/- rndAngle

private var timeToNewDirection = 0.0;
private var turningTime = 0.0;
private var turn: float;

function Idle () { 
	// Walk around and pause in random directions unless the player is within range 
	if (Time.time > timeToNewDirection) { // time to change direction?
		if(Random.value > 0.5)  // choose new direction
			turn = rndAngle;
		else
			turn = -rndAngle;
		turningTime = Time.time + idleTime; // will stop and turn during idleTime...
		timeToNewDirection = turningTime + directionTraveltime;  // and travel during directionTraveltime 
	}
	if (Time.time < turningTime){ // rotate during idleTime...
		transform.Rotate(0, turn*Time.deltaTime/idleTime, 0);
	} else {  // and travel until timeToNewDirection
		characterController.SimpleMove(transform.forward * walkSpeed);
	}
}