Enemy AI problems

Well I got my slime enemy moving toward the character but the problem is that he keeps moving. I want to make it so that when he enters a certain distance from the character he will stop moving and start attacking the character. script is:

 var attackanimation: AnimationClip;
var walkanimation: AnimationClip;
var dieanimation: AnimationClip;
private var walkspeed: float = -3.0;
private var stop: float = 0;
var range : float = .5;
var character : CharacterController; 
var target : CharacterController;
var direction : float = -1;
function Update () {
	distance = Vector3.Distance(transform.position, target.transform.position); 
if(distance >= 1)    
{   
    character.Move(Vector3.forward * walkspeed * direction * Time.deltaTime);
    animation.Play(walkanimation.name);
}
if(distance <= 1){
	animation.Play(attackanimation.name);
	character.Move(Vector3.forward * stop * direction * Time.deltaTime) ;
}
}

You nearly have it.

The main problem i can see is in this bit:

if(distance <= 1){
	animation.Play(attackanimation.name);
	character.Move(Vector3.forward * stop * direction * Time.deltaTime) ;
}

Just for starters, a distance of 1 is tiny, i’m not sure of the scale your using in your game but you might want something slightly bigger.

Now, we know we do not want to move the enemy if its in range, so lets delete the character.Move bit and just play the animation, then run the attack function:

var attackanimation: AnimationClip;
var walkanimation: AnimationClip;
var dieanimation: AnimationClip;

private var walkspeed: float = 3.0; //Don't understand why this was negative.

var range : float = .5;
var character : CharacterController; 
var target : Transform; // lets cache the transform, rather than use a lookup.
var direction : float = -1; //why is this here?

function Awake()
{
target = GameObject.Find("your target").GetComponent(Transform);
}

function Update () {
distance = Vector3.Distance(transform.position, target); //no more lookup for target. 
var forward : Vector3 = transform.TransformDirection(Vector3.forward);

    if(distance >= range)//you have a range variable, lets use it!    
    {   
        character.SimpleMove(forward * speed); //SimpleMove is simpler :).
        animation.CrossFade(walkanimation.name); //let's make the anims look better.
    }

    if(distance <= range)//you have a range variable, lets use it!
    {
	    animation.CrossFade(attackanimation.name); //crossfading again.
	    DamageorAttackFunction();
    }

I hope the comments in the code help you to understand it. Might be 1 or 2 errors as i haven’t tested it but nothing you shouldn’t be able to solve.

Let me know how it goes!

Ok hey guys I got him working with the help of Timsk, but now Im trying to figure out how to make him rotate around and chase me after I dodge him. I cant seem to figure it out. Im looking through the scripting guide to find it, but I could use some help. Thanks for the Help. Heres the script

var attackanimation: AnimationClip;

var walkanimation: AnimationClip;

var dieanimation: AnimationClip;

var distance;





private var walkspeed: float = -3.0; //Don't understand why this was negative.



var range : float = .5;

var character : CharacterController; 

var target : Transform; // lets cache the transform, rather than use a lookup.

var direction : float = -1; //why is this here?

function Awake()

{

target = GameObject.Find("your target").GetComponent(Transform);

}



function Update () {

	distance = Vector3.Distance(transform.position, target.transform.position); 

	}



function LateUpdate () {

var forward : Vector3 = transform.TransformDirection(Vector3.forward);

	if(distance >= range)//you have a range variable, lets use it!    

    {   

        character.SimpleMove(forward * walkspeed); //SimpleMove is simpler :).

        animation.CrossFade(walkanimation.name); //let's make the anims look better.

    }



    if(distance <= range)//you have a range variable, lets use it!

    {

        animation.CrossFade(attackanimation.name);

    }

}