Simple AI script, what is wrong with LookAt ?

Hey guys, I’m trying and make a simple AI script and what I got so far is working for now.

So I cast a ray and check if the player is within the range and move the enemy towards the player.
Then check if the distance from the player and if it’s over 10 then stop chasing the player

Everything is ok, but when I say to follow AND LookAt the player the enemy want stop when it’s to far from the player :S

Ok, here’s the code I got so far.

var Player : Transform;

var Follow = false;

var DistToGiveUp = 10.0;

function Update () {
 
   var Sight = transform.TransformDirection(Vector3.forward);
   
   var hit : RaycastHit;   
   
	// Draw the Ray in editor
   Debug.DrawRay(transform.position, Sight * 20, Color.green);
 
	// Raycast to see if the player is in sight
   if(Physics.Raycast(transform.position, Sight, hit, 20)){
      Debug.Log("Hit");    
      if(hit.collider.gameObject.tag == "Player"){
          Follow = true;
      }
   }
   
   // When Follow = true, call Attack Function
	if(Follow)
		Attack();
	
	// When we follow the Player, Call the CheckDistance Function
	if(Follow)
		CheckDistance();
	
	
}

// The Attack Function
function Attack () {
	transform.LookAt(Player);
	transform.position = Vector3.Lerp
	(transform.position, Player.position, Time.deltaTime*0.5);
}

function CheckDistance () {
	var dist = Vector3.Distance(Player.position, transform.position);
    print ("Distance to other: " + dist);
	
	if(dist>10.0)
		Follow = false;
}

So if i comment out the LookAt(Player) the code works fine.

But when have the LookAt the enemy never stop chasing.

And to say, I’m not coder so any advice to get a better script is great :wink:

what about LookRotation instead of LookAt? did you try this?
you could also work with triggers OnEnterTrigger → chase… OnTriggerExit → !chase
…well, it´s just one possibility of many others… it could looks like this:

// aggrocollider.js   ....or call it what ever you want - in my case this is a big sphere collider
private var linked; 
var Enemy_Controller:GameObject;
function Start(){
	linked = Enemy_Controller.GetComponent("Enemy_Controller"); // gets the Enemy_Controller.js 
}
function OnTriggerEnter(other : Collider){
	if(other.gameObject.tag == "Player"){
		linkedscript.chase = true;
		//linkedscript.seek = false;
		Debug.Log("enter aggrorange");
	}
}
function OnTriggerExit(other : Collider){
	if(other.gameObject.tag == "Player"){
		linkedscript.chase = false;
		//linkedscript.seek = true;
		Debug.Log("left aggrorange");
	}
}

//////////
somewhere in your enemy_controller script:

    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Player");
	thePlayer = gos[0];
	distToPlayer = Vector3.Distance(thePlayer.transform.position,transform.position);
	if(chase){
	      //  if(seeDistance >=30){
			var rotateAroundPlayer = Quaternion.LookRotation(thePlayer.transform.position - transform.position);
			transform.rotation = Quaternion.Slerp(transform.rotation, rotateAroundPlayer, Time.deltaTime /smooth);
		//} // in my case it´s a spaceship... so this is an optional part for you!
		//else if (seeDistance < 30){
		//	transform.RotateAround (thePlayer.transform.position, Vector3.up, 10 * Time.deltaTime);
		//}
	}

after the player exit the aggrorange the enemy stops chasing… but if your enemy still should chase your player after leaving the aggrorange you could use a timer, a coroutine with WaitForSeconds or save the position of the enemy and check these values. if the amount is bigger than …25 or so… the enemy stops chasing and runs back to the saved position i´m not sure, but i guess that´s what they do in games like wow. (…yeah…i know…f*cking world of noobcraft;)
well, it´s up to you… i use a timer and this system works well… and at this time there´s no need of using a raycast
…and as is said already…this i just one way to achiev this enemy behavior.

Yeah thanks for the help.

But I need raycast because the player could be hiding behind walls ans such :wink:

But I could maybe use a trigger to check the distance after the raycast ?

I just wanted to know why it worked perfectly without LookAt :S

I think its because after Follow gets set to false, the code for raycasting then sets Follow to true since he can still “see” the player within 20 units. Try changing your raycast distance from 20 to 10 or lower.

Ahh maaaan, I feel so dumb right now :smile:

Yeah thats exactly what it is.

But how would you guys do to make him chase withing 20 units but stop within 10 ?

Thanks guys.

I’m confused, in your initial post you want the enemy to stop chasing if he is over 10, now you say you want the enemy to stop chasing if he is within 10.

Do you want that the enemy will only chase the player if the player is between 10-20 of the enemy? Is that what you want?

While I would prefer to code it in a different way, I tried not to change too much of your code. Try this:

var Player : Transform;

var Follow = false;

var DistToGiveUp = 10.0;

function Update () {

   var Sight = transform.TransformDirection(Vector3.forward);
   
   var hit : RaycastHit;   
   
	// Draw the Ray in editor
   Debug.DrawRay(transform.position, Sight * 20, Color.green);
   
	var dist = Vector3.Distance(Player.position, transform.position);
    //print ("Distance to player: " + dist);
	
	// Raycast to see if the player is in sight
   if((dist>10.0)  Physics.Raycast(transform.position, Sight, hit, 20)){
      Debug.Log("Hit");    
      if(hit.collider.gameObject.tag == "Player"){
          Follow = true;
      }
   }
   
	// When Follow = true, call Attack Function
	if(Follow){
		Attack();
		if(dist<=10.0){
			Follow = false;
		}
	}
}

// The Attack Function
function Attack () {
	transform.LookAt(Player);
	transform.position = Vector3.Lerp
	(transform.position, Player.position, Time.deltaTime*0.5);
}