enemy AI follows exact path

how to make enemy AI which will follow the exact path of a player in FPS game…
it will be great if you tell with example! thanks in advance! :slight_smile:

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

I’ve not done this before but i did come up with a soloution for this in my mind a while ago…if ur player has a rigidbody(if not the only thing we need is to know it’s speed!)we can handle things like this:

Step #1:When Ever Player’s speed has changed it means he’s going somewhere!

Step #2:We’ll create a pathpoint for EnemyAI at player current position which will tell AI change ur speed to this!

Step #3:if player stopped somewhere for a few seconds then what?!?!?! Answer:If This PathPoints Speed is zero also save how much time did our player spent standing here!

Step #4:There’s no Step#4! :stuck_out_tongue:

So Let me clear this with an example:
-Player Starts moving with velocity.x=10 Make a pathpoint here with it’s speed property(A vector 3) as x=10!
-Player goes forward with that speed a bit then stops changes his speed to velocity.z=10 So we make a pathpoint here with this as SpeedProperty(X=0,Y=0,Z=10).

Now our AI Gets in the First PathPoint And Sees wow i must go forward with velocity.x = 10 speed so it goes forward reaches the second point and starts with this new speed that this new path point is giving him(X=0,Y=0,Z=10)!
So like this the enemy is going the exact same way the player went but if player drops some obstacles while he’s going i’m sure our AI would be in trouble but if player has to avoid some obstacles in his way the AI would do the exact same thing!
Unfortunately i’m way too lazy to write the whole code at the moment but if u dont get it and need the code i’ll do it no worries! :smiley: