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.

Cool! Great question :P

2 Answers

2

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.

wow, thats a cool script, the enemy actually follows my player! and i have a seperate script for turret shotting and dying, so its cool to have a script for following my player.

Great!But why the enemy clipping the walls ?(sry for my english)

The enemy is clipping the walls because this goes at your player in a straight line, no matter if there is anything in the way or not. As stated, it's a very basic AI.

excellent script!! it works perfectly :D 100 points! :P

Just wanted to send a quick thanks - I just implemented this code since the LookAt() command has no smoothing parameters ha ha

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);
    }
    
 
}

Thanks so much! super helpful for my project.

is there anyway you can make it so when you look at the enemy it will stop then when you look away it will follow you?

My enemy won't follow unless I have a rotation on, I can't put this on 0 otherwise it won't work. I tried deleting it from the Javascript but that didn't work either. (I'm using this for a 2D game so as a sprite it shouldn't rotate.)

I used your script and it works perfectly, but now I want to make the stop variable into a random integer. How exactly could I do that? Thank you.

Everytime I try to play the game with this script attached to my enemy, I keep getting the same Error NullReferenceException: Object reference not set to an instance of an object Follow.Start () (at Assets/Standard Assets/Scripts/Follow.js:15) What do I do?