- Home /
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! :)
The only way for an enemy to follow an exact path is to create a path (array of Vector3 positions) as the player moves along for the enemy to follow. Even then, it won't be absolutely exact unless you do the player movement and the path point saving in FixedUpdate and move the enemy from point to point defined by the path. $$anonymous$$ost of the time for this kind of thing, the path is treated as a series of waypoints, so the enemy can land between the Vector3 points saved by the player.
In some situations you could store the sequence of inputs a player makes, and use it to drive the AI with the same inputs at a time delay. It does rely on the AI starting in the same state as the player started in.
This is probably not worth doing in most situations.
@Bored$$anonymous$$ormon - cool alternative that I did not consider. Would likely not work if any physics was involved though.
guys, can you give some code like how to take players Vector3 positions in array and then make enemy to follow them. that would be great! cheers!
You might get lucky, but generally UA frowns on requests to write scripts.
A lot is contextual, how your player and enemy move is a key consideration.
Answer by Bilelmnasser · Aug 01, 2014 at 06:58 AM
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);
}
}
This is just a standard follow script. Not sure how it helps the OP.
@Bored$$anonymous$$ormon nope man! not this one! Still thanks for ur time! :)
Due to this script enemies start moving upward don't know why? I am using root motion and animations from $$anonymous$$ixamo . help needed
Answer by behzad.robot · Aug 01, 2014 at 09:01 AM
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! :P
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! :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613155513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to refer to enemy health 2 Answers
Enemy AI problems 2 Answers
Vision cone with obstacles? 2 Answers
How do I make a 2d enemy change direction upon hitting a wall? 1 Answer
Looping Between Waypoints 1 Answer