- Home /
NPCs follow player in a line
Hi, I have a script which instantiates NPCs once the player passes a house. The NPCs is a simple prefab so I created the script below to let the NPC follow the Player, which works good so far, what I would like to do now is to get multiple NPCs to folow the Player in a clean line, (similar to a snake). I guess the best Approach would be something like the first NPCs follows the Player, the second NPC should follow the first NPC and so on. But I have no clue how to set this up.
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);
}
}
Answer by joedartjr · May 23, 2014 at 01:25 AM
If you want your AI to follow the exact path of the player, and not a direct path to the player from their current position, you should make the player instantiate waypoints every second, naming them with a corresponding numerable suffix, then change the AI script to target the waypoints instead of the player. Each time the AI reaches a specific distance from the waypoint (using the "if(distance<=range && distance>stop)" portion of your script above, add a method that changes the target waypoint by 1.
The idea would be that the AI will follow your player, but they will track the exact path of the player by following waypoint's your character will be dropping as it moves. You can even add a public integer value to your player, and then tell your AI to check that integer value when they are instantiated so they know what waypoint to start at.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Make player not be seen by AI, when player in foilage and shadows. 1 Answer
AI enemy scripting help 1 Answer
How do I make ai play a certain sound, and then make the other ai reply? 0 Answers
My Script keeps destroying all enemies instead of just one. 3 Answers