Capsule wander after player is lost.

public var target : Transform;
public var moveSpeed : float = 3.0;
public var attackRange : float = 10.0;
public var stopRange : float = 2.0;

function Start(){
	var LastSeen = transform.position;
	var wander = Vector3(LastSeen.x,transform.position.y,LastSeen.x) + Vector3(0,Random.Range(1,15),Random.Range(1,15));
	while(true){
		if( AttackRangeCheck() ){ // target object is within range
			if( !StopRangeCheck() ){
				LastSeen = target.position;
				transform.LookAt(target);
				transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
			}else{ Debug.Log("ATTACK!");
			yield WaitForSeconds(1);}
		}else{
		if((Vector3.Distance(transform.position,LastSeen) < 15) || (Vector3.Distance(transform.position,wander) < 2)){
		transform.LookAt(LastSeen);
		transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
		}else{
		if ((Vector3.Distance(transform.position,wander) < 3)) {
		wander = Vector3(LastSeen.x,transform.position.y,LastSeen.x) + Vector3(0,Random.Range(1,15),Random.Range(1,15));}
		transform.LookAt(wander);
		transform.Translate(Vector3.forward*Time.deltaTime*(moveSpeed/2));
		}}
		yield;
	}
}
function AttackRangeCheck() : boolean{
	var distanceToTarget = Vector3.Distance(transform.position, target.position);
	if(distanceToTarget <= attackRange)
		return true;
	else
		return false;	
}

function StopRangeCheck() : boolean{
	var distanceToTarget = Vector3.Distance(transform.position, target.position);
	if(distanceToTarget <= stopRange)
		return true;
	else
		return false;	
}

For some reason when you get to the wandering part after it loses the player it doesn’t wander it just kinda flips around and spins. Just put the script in a capsule with a rigidbody and you’ll see what I mean.

try enabling freeze rotation

Nope didn’t work. I’m uploading a youtube video of it right now.

EDIT: here it is:

I can’t be sure, but it looks like the only time the AI ever translates toward the “wander” position is inside:

if ((Vector3.Distance(transform.position,wander) < 3)) {

But if the AI never started moving toward the original wander in the first place, how can it ever satisfy that condition?

Wander is a modified version of the LastSeen value, it changes when the AI gets close.

Hmm… if you do a Debug.Log inside:

if ((Vector3.Distance(transform.position,wander) < 3)) {

Does it ever actually get in there?

It does, because the position its moving to changes causing it to flip.

It looks like the position that its picking for wander is way above or below the plane you and the enemy are standing on…

When you are adding your random values to the lastSeen position to create the wander var, you are adding a random value to the y and z coordinates:

var wander = Vector3(LastSeen.x,transform.position.y,LastSeen.x) + Vector3(0,Random.Range(1,15),Random.Range(1,15));

Which I would think is making the y value of the wander Vector3 something way above or below both you and the enemy… maybe that’s it? Try only adding a random value to the x and z.

I’ve been expirementing with that, for some reason the z and y works the same as x and z. :frowning:

Did you try it like this?

var wander = Vector3(LastSeen.x,transform.position.y,LastSeen.x ) + Vector3(Random.Range(1,15),0,Random.Range(1,15));

Incredibly worse now it doesn’t even move just flops around.