- Home /
How can I make AI follow waypoints and choose random ones?
Hello I am making a 2d Isometric management game. When customers enter the restaurant I want them to either head towards the front registers or go to the vending machine. I am able to make the customers walk to the door but I do not know how to make them seek out the next way point. I am also unable to get them to navigate around objects or choose between a random point that I set. Its kind of difficult to explain so here's some visual aid.
But you will have to show us what you started with. Show us your sample script. Where are you stuck how are you moving him in the first place etc...
Since you're using a tiled map, I'd suggest looking up how to implement A* pathfinding (also known as AStar). It takes a bunch of nodes (your tiles) that are linked / blocked from one another in whatever way, and does a quick search to find a path from A (eg your door) to B (eg your registers or vending machine).
The Unity asset store has a free A* package, but I'm not sure that it'd fit your needs. It'd be good to understand how A* works, so google a little and see what you can find :)
Thanks ahaykal and nesis. To make the customer seek out a point i just have a speed variable that translates based on Time.deltaTime. Then if the customer reaches a point I increment the currentPoint variable by 1. The problem with this is that the customers wont avoid objects and its difficult to randomize their paths. I've looked into A* and it seems pretty robust. I managed to generate a graph of my map that analyses what areas are walk able but so far I can only get the customer to seek out a single point or object. I could still use some help.
Answer by Tyrebear · Feb 02, 2014 at 12:07 AM
var waypoints : Transform[];
var waypoint : Transform;
var speed : int;
var currentWaypoint : int;
var agent : NavMeshAgent;
var isMoving : boolean = false;
var customer : GameObject;
function Update () {
waypoint = waypoints[currentWaypoint];
if (isMoving == true){ // sets the active waypoint to the next currentwaypoint
agent.SetDestination(waypoint.position);
}
}
function OnTriggerEnter (other : Collider) {
if (other.tag == "Waypoint"){
currentWaypoint++;
}
if (currentWaypoint == 5){
Destroy(gameObject);
}
if (other.tag == "Waypoint" && currentWaypoint == 2){
currentWaypoint = Random.Range(2, 4);
}
}
function WaitAtPoint(){
isMoving = false;
yield WaitForSeconds(5);
}
After not being able to really find a definitive answer online I created my own script.
Thanks, your one line of code :
currentWaypoint = Random.Range(2, 4);
solved my problem!
Your answer
Follow this Question
Related Questions
AI Pathfinding 1 Answer
Wandering enemies 1 Answer
Incredibly high speed tweening/enemy movement 0 Answers
Enemy detection while pathfinding through the map 1 Answer
Why isn't a Ai Waypoint Spawning??? 0 Answers