- Home /
What is the best way to do a patrolling AI?
I want to set up an enemy AI such that they patrol some area and upon registering the player in front of them, switch to some other action. I know there is some sort of built in pathfinding stuff but I don't know how to set up patrol points with it.
Answer by JNetRocks1316 · Mar 09, 2014 at 07:20 AM
Short Version:
Create an enemy game object
Attach a Nav Mesh Agent
Create your nav mesh (Navigation window)
Create conditions for the enemy to follow
Example of a simple "wandering" enemy.
using UnityEngine;
using System.Collections;
public class PatrollingEnemy : MonoBehaviour {
private Vector3 startPosition; //Give it a startPosition so it knows where it's 'home' location is.
private bool wandering = true; //Set a bool or state so it knows if it's wandering or chasing a player
private bool chasing = false;
private float wanderSpeed = 0.5f; //Give it the movement speeds
private GameObject target; //The target you want it to chase
//When the enemy is spawned via script or if it's pre-placed in the world we want it to first
//Get it's location and store it so it knows where it's 'home' is
//We also want ti set it's speed and then start wandering
void Awake(){
//Get the NavMeshAgent so we can send it directions and set start position to the initial location
agent = GetComponent("NavMeshAgent") as NavMeshAgent;
agent.speed = wanderSpeed;
startPosition = this.transform.position;
//Start Wandering
InvokeRepeating("Wander", 1f, 5f);
}
//When we wander we essentially want to pick a random point and then send the agent there
//Random.Range is perfect for this.
//If you're working on a hilly terrain you may want to change your y to a higher point and then
//Use a raycast down to hit the 'terrain' point, rather than keeping y at 0.
//y at 0 would only work if you have a completely flat floor.
void Wander(){
//Pick a random location within wander-range of the start position and send the agent there
Vector3 destination = startPosition + new Vector3(Random.Range (-wanderRange, wanderRange),
0,
Random.Range (-wanderRange, wanderRange));
NewDestination(destination);
}
//Creating this as it's own method so we can send it directions other when it's just wandering.
public void NewDestination(Vector3 targetPoint){
//Sets the agents new target destination to the position passed in
agent.SetDestination (targetPoint);
}
}
As far as detecting the player you could do that through a trigger or a ray-cast to see if they are in visual range. For example, you could have an Update loop that does a raycast out from the enemy's face and if the enemy 'sees' the player (the ray hits the player), then trigger another method that changes the 'state' from wandering to 'chasing' or 'running' or whatever behavior you want it to do!
I'm pretty new to programming so there might be a more efficient way, but it does work!
If InvokeRepeating("Wander", 1f, 5f); is running in awake and you set it to chase player then how do you set it back to wandering once it chased the player for too long?
Answer by fafase · Mar 09, 2014 at 08:11 AM
http://unitygems.com/basic-ai-character/
This may help since it shoes how to get an AI to patrol or attack when the player is close enough, within line of sight and not hidden behind a wall.
Your answer
Follow this Question
Related Questions
waypoint and navmesh 1 Answer
Argument Out of Range On a List 2 Answers
NullRefException in RaycastAll 2 Answers