Draw connections in game


#1

hi i have some nodes (around 5~6 ) scattred around on ground plane . which are used to generate pointgarph .
is there any way that i can draw connections with line renderer or vectrosity to show the connections .

i know its already there in debug mode and you can see it via enabling gizmos , but i dont know how that is achieved in debug mode … can you please guide me how to go about it ?

Thank you


#2

Hi

You can access all nodes like this (this works for any graph type, there are more specialized approaches for some graph types).

 var graph = AstarPath.active.astarData.graphs[0];
 graph.GetNodes(node => {
     node.GetConnections(other => {
         Debug.DrawLine((Vector3)node.position, (Vector3)other.position, Color.red);
     }
     return true;
 });

This will draw a line for all connections in the graph (actually it will draw each connection twice, it will draw it in both directions). To draw it in game you will have to change the Debug.DrawLine call to use something like Vectrosity.


#3

Thanks will give it a go , Thank you Very much


#4

is it possible to get the direction of the connection …say top, bot, left, right …?


#5

Ok , the update: the above mentioned way is not quiteusefull as it gives you all random and unpredictable data

for example consider three nodes:-
A------B------C
now i am getting links between A-----B , B-------C and there reverse , which is all ok , but in some cases i am also getting links like A------------C

so its really not helpful.

Any Way to achieve proper behaviour?


#6

If it says there is a connection between A and C, then there is apparently a connection between A and C (and would also show up in the scene view). You will have to tweak the point graph settings, maybe by reducing the maximum connection distance.

A connection can go in any direction, not just four fixed ones, but you can use some code to figure out which direction is the closest. Something like this:

 var connectionDir = ((Vector3)other.position - (Vector3)node.position).normalized;
 var dirs = new Vector3[] { Vector3.left, Vector3.right, Vector3.up, Vector3.down };
 var closest = 0;
 for (int i = 0; i < dirs.Length; i++) {
      if (Vector3.Dot(dirs[i], connectionDir) > Vector3.Dot(dirs[closest], connectionDir)) {
           closest = i;
      }
 }
 Debug.Log("The connection goes approximately in the " + dirs[closest] + " direction");

#7

no there is no connection between a-c i cant move from a to c without passing from b


#8

If the nodes are on a line (which I assume is the case here?) the graph could still contain a connection from A to C that just happens to go though B.