Question by
adavidliu · Nov 01, 2015 at 10:54 PM ·
pathfindingmultiple objectspatrol
Multiple patrolling enemies
I'm trying to have multiple enemies patrolling different zones of my map. I'm using the code sample from the "Making an Agent Patrol Between a Set of Points" Unity tutorial but whenever I put 2 or more enemy GameObjects the original one is the only one moving. The other ones start moving only after another enemy of the player has entered its "aggro zone". Here's the code, I only modified the if statement for the aggro zone:
public class agentScript : MonoBehaviour {
public Transform[] points;
private int destPoint = 0;
public NavMeshAgent agent;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
GotoNextPoint();
}
public void GotoNextPoint() {
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points [destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
// Update is called once per frame
void Update () {
// Choose the next destination point when the agent gets
// close to the current one.
if (agent.remainingDistance < 0.5f && !transform.FindChild("enemy").GetComponent<enemyMovement>().aggro && !transform.FindChild("enemy").GetComponent<enemyMovement>().aggroEnemigo) {
GotoNextPoint ();
}
}
Comment