Enemy AI monster problem

hi, I dont speak english really well but i can understand.
I have a javascript code to make follow an object to another:

var leader : Transform;

var follower : Transform;

var speed : float =5; // The speed of the follower

function Update(){

follower.LookAt(leader);

follower.Translate(speedVector3.forwardTime.deltaTime);

animation.Play(“Walk”);

}

I use it for make an enemy
but there is 2 problems : My enemy walk trough walls and can follow me from any distance, i want to controll it, please, can you help me ? [sry bad english] Bye :slight_smile:

You don’t want to use Transform.Translate() because that overrides the collision engine. It doesn’t take much work to fix fortunately. Just add a character controller to your enemy. Then your code doesn’t need many changes.

var leader : Transform;
var follower : Transform;
var followerCC : CharacterController;

var speed : float =5; // The speed of the follower

function Start () {
     followerCC = follower.GetComponent.<CharacterController>();
}

function Update(){

    follower.LookAt(leader);
    followerCC.SimpleMove( follower.forward * speed );
    //See character controller.SimpleMove().  Moves the object in that direction.
    //automatically multiplies by Time.deltaTime;
    //And responds to collisions
    //ignores vertical component of vector and uses built-in gravity instead.

    animation.Play("Walk");

}

I assume your code is attached to the monster so you don’t have to cache the follower transform, but it doesn’t hurt. In fact it will probably speed your code up some.

And you will have to explain to me the latter part of your question. I take it as you don’t want the enemy following you forever i.e. it has a zone that it stays in. In that case, just create a large trigger around the area the enemy is allowed in. When the player enters the trigger, attack, and when he/she leaves, stop.

Translate doesn’t check collisions - that’s why the enemy pass through things. Usually we add a CharacterController to the enemy (menu Component/Physics/CharacterController), and move it using Move or SimpleMove. You should also check the distance from the target (leader, in your case), and stop following it when it’s greater than the detect range:

var leader : Transform;
var follower : Transform;
var detectRange = 30.0;
var speed : float = 5; // The speed of the follower

function Update(){
  var dist = Vector3.Distance(leader.position, follower.position);
  if (dist <= detectRange){
    follower.LookAt(leader);
    // SimpleMove uses gravity, and moves with the velocity vector specified:
    follower.GetComponent(CharacterController).SimpleMove(speed*follower.forward);
    animation.Play("Walk");
  }
}

This is a simple adaptation of your original script. You should use a strictly horizontal velocity to move the character, or it would get tilted when the target is at a different height. A better approach would be:

var leader : Transform;
var follower : Transform;
var detectRange = 30.0;
var speed : float = 5; // The speed of the follower
private var character: CharacterController;

function Update(){
  // save the CharacterController in a variable to improve performance:
  if (!character) character = GetComponent(CharacterController);
  var dir = leader.position - follower.position; // find the leader direction
  dir.y = 0; // keep the direction in the horizontal plane
  if (dir.magnitude <= detectRange){ // dir.magnitude is the distance
    follower.forward = dir; // turn the enemy to the target
    character.SimpleMove(speed*dir); // move in the target direction
    animation.Play("Walk");
  }
}

Ok, thank you, it works Really well