# AI how to know which node player is at

Okay so finally i am going to implement a way-point AI system that isn’t dumb AI (dumb = no path-finding). But there is one piece of logic i cannot fit to my puzzle.

I know how a finite state machine works and how to implement the changing states
I know how to add way-points and make AI move between them

But… (its a pretty big but)

How do you know which node the player is closed to and which nodes the AI should follow to get there i thought about having a trigger or something but being able to navigate to the node too any one get any references or a logical idea they can share of how to solve this issue?

Just loop through all the points (put them in an array or list), and use Vector3.Distance to find the closest (and maybe a raycast as well)

What squared55 said will work!

However if you have a lot of nodes, i suggest you segment your map into pieces and find the one your in before using .Distance as it can be a bit heavy.

For example, lets say you have a map that is 1000x1000, then you first piece it into 100x100’s. then in each 100x100 you piece it into 10x10’s, which will hold waypoints within this area you then search for.

So: Check x and z coord and find your 100x100 → search a 10x10 inside of this one based on x and z → search waypoints with in the 10x10.

Okay i think i know what you mean but in the array of nodes logically how do you determine the best path to the player position would you :

Check the players distance to the npc then use that in the array to produce a list of nodes starting from the closest to the npc to the closest to the player and discard any further away than the npc? only issue here is what if two nods are opposite each other on different ends of the map but slightly a head of each other the AI will walk zig zag cross the map.

Do you think there is anyway to fix that issue or am i think about this wrong?

Edit: Do you think its better to mix way points with cells this im guessing this is similar to BFGames solution and the way point system combined.

So you have cells-and within them is way point you detect the cell the player is in and what way-point is in that cell then you make the AI perform a calculation to list the number of way-points it would go through to get to the players cell the one with the least way-points to traverse to the player position is the one that’s used.I think this solves the .distances issue BFGames mentioned but is also simple enough to do it would also be similar to a* kind of but in waypoints what do you think?

What i mentioned is only for easy and fast look ups for the closests nodes. You will still need to implement an A* algorithm to find the shortest way. Not really a good alternative to that.

could you break the waypoints into paths. Have a start and end path node which lists what path it is and what paths it connects to when a player is in a cell it say what paththe AI should navigate to and what node in that particular path?

or will i have to just figure out A* calculations (heuristic, G-score, F-score)if so how would that work in paths (way points) thought it had to be boxes or hundreds of nodes kind of like a custom navmesh?

You could have separate paths defined by way-point’s. But then your system is extremely static, and it can become a problem if they need to move from one path to another.

I would suggest having each waypoint connected to all “visible” waypoints as neighbors (all waypoints should be connected in the same system) and then implement A* for the system. (Thats how my waypoint system works in my asset)