How to create a basic follow AI

I don't know much about scripting and was wondering if anyone knew a basic script to get an enemy to follow the player. I've tried using the one on the tutorial, but when putting it into my own game, it doesn't work. Help would be very much appreciated.

The code below should get an enemy to follow your player. No triggers are set up so the enemy will follow the player no matter how far it is away. This should be a good starting point. Read all the comments.

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning

var myTransform : Transform; //current transform data of this enemy

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
     target = GameObject.FindWithTag("Player").transform; //target the player

}

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

}

All you need to do is create a new script and copy the above code into it. You need to tag your Player with the "Player" tag. Then just drag the script onto the enemy you want to follow the player.

This should be a good starting point for you to script more complex behaviour.

If you haven't already I would check out the Lerpz Platformer tutorial in the Unity3d site. It will take you through a lot of basic concepts and comes with a lot of scripts you can reuse.

Just a small update on this old post made it simple so if will look at you from a certain distance and if you get closer then a certain distance it follows. i made range and range 2 so its easier to use ok :smiley:

edit added a stopping distance so the enemy wont keep going though you and circling

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float=10f;
var range2 : float=10f;
var stop : float=0;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}
 
function Start()
{
     target = GameObject.FindWithTag("Player").transform; //target the player
 
}
 
function Update () {
    //rotate to look at the player
    var distance = Vector3.Distance(myTransform.position, target.position);
    if (distance<=range2 &&  distance>=range){
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    }
  

    else if(distance<=range && distance>stop){

    //move towards the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    }
    else if (distance<=stop) {
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    }
    
 
}