Trying to move to node nearest to target


#1

As the title says I’m trying to make my player stop moving at the node closest to an object I click on, in my case an enemy. I have everything set up properly for movement, but right now the player moves ontop of the target.

I found and added this little gem:
GraphNode node = AstarPath.active.GetNearest (transform.position).node;

But that returned an error. I took to the Unity forums and quickly found an answer:
Int3 a = new Int3 ();
Vector3 b = (Vector3)a;

Which actually originated from these forums. Now I have a very odd error where when I use this code my player always navigates to the original spawn point. Any ideas why this is?


#2

I got it to work almost perfectly now. Just needed to change it to:

Int3 a = new Int3(focus.transform.position);
Vector3 b = (Vector3)a;

ai.destination = b;

And that made it work. The only problem I have now is that this code grabs all 8 nodes surrounding the target, but in most cases I will just want the 4 nodes that are up, down, left, and right of the target. I don’t beed the corner nodes.

Any ideas on how to just get the 4?


#3

Hi

If you want your agents to move to the center of the node instead of the precise destination then you can simply set the Seeker -> Start End Modifier -> End Point setting to ‘Node Center’ (or in earlier versions it is called ‘SnapToNode’).

I’m not quite sure how your code made anything work. The only thing that your code would do is to round the position to the nearest millimeter.


#4

I actually alreafy have it set to SnapToNode so that part is working perfectly. As far as I can tell, the first line finds the nearest node to my target and the second batch of code sets the Int3 to a Vector3 so I can move to that location. It seems to be working properly but I think I need to include some sort of distance check. Something more like:
GraphNode node = AstarPath.active.GetNearestNodeWithinDistance.
But that sort of code does not appear to exist. Is there a eay to grab the nearest node within 1 unit? Rather than the current code which finds all 8 of the nearest nodes


#5

This code

Int3 a = new Int3 ();
Vector3 b = (Vector3)a;

Should basically set b = Vector3.zero.

while this code

Int3 a = new Int3(focus.transform.position);
Vector3 b = (Vector3)a;

is pretty much equivalent to

Vector3 a = foacus.transform.position;
Vector3 b = new Vector3(0.001f*System.Math.Round(a.x*1000), 0.001f*System.Math.Round(a.y*1000), 0.001f*System.Math.Round(a.z*1000));

Regarding your last question. Where are you saying it finds all 8 of the nearest nodes?
Are you trying to make your agent move to a node that is not walkable?


#6

Ah interesting. So converting from In3 to Vector3 is basically just a bunch of rounding.

I don’t have any codes or readouts that specifically show that it is grabbing all 8 nodes, but when I click on an object I want to move to (with all the current code in place) if a diagonal node is closer than an orthagonal node my player will navigate to the diagonal node. I want to eliminate nodes that are diagonal from the target when it searches for the nearest node, if that is possible. If its not it isn’t a massive deal breaker, but would be nice to have for certain systems

Also, forgot to answer your last question, I’m trying to navigate to the nearest walkable node


#7

Almost. Converting from a Vector3 to an Int3 is a bunch of rounding. Converting from an Int3 to a Vector3 is basically a multiplication by 0.001. An Int3 represents a coordinate in millimeters (0.001 world units) using integers.

Ah I see. Currently that particular behavior is hard coded. It’s a bit of a hack because a lot of people asked for it. However you can easily change it.
Open the ABPath.cs script and find the EndPointGridGraphSpecialCase method. Find the line which says

switch (gridGraph.neighbours) {

and change that to

switch (NumNeighbours.Four) {

Also find the line that says

int mxnum = gridGraph.neighbours == NumNeighbours.Four ? 4 : (gridGraph.neighbours == NumNeighbours.Eight ? 8 : 6);

and change that to

int mxnum = 4;

#8

That makes sense. At least its an easy conversion.

Perfect! That should do the trick, I’ll give it a test later but just from looking at it I think it will work. I may even be brave enough to attempt both ways, never know if you might need it, but we will see if my coding skills are up to the challenge.

Thanks for all the help! Your asset is awesome and I love using it!