Hi,
I use A* Pathfinding for the first time so buckle up for noobish questions.
I’m trying to create a point graph where there are just lines between points and not areas. I just need to find path between my nodes and I don’t need a navigating agent.
My nodes are created and connected at runtime. The graph is pretty simple for now but I can’t manage to find path. I think there is something about unit conversion that I didn’t grasp.
Here is the code I use (A game object containing a Astar Path component with no graph in it is on the scene).
private void Start()
{
AstarData data = AstarPath.active.astarData;
pg = data.AddGraph(typeof(PointGraph)) as PointGraph;
pg.raycast = false;
pg.autoLinkNodes = false;
pg.recursive = false;
pg.drawGizmos = true;
AstarPath.RegisterSafeUpdate(UpdateGraph);
}
private void Update()
{
Vector3 mouse_pos = Input.mousePosition;
mouse_pos = Camera.main.ScreenToWorldPoint(mouse_pos);
_mouse_pos = new Vector3(mouse_pos.x, 0.0f, mouse_pos.y);
FindPath(new Vector3(0,0,0), _mouse_pos);
}
private void UpdateGraph()
{
PointNode A = pg.AddNode(new Int3(0,0,0));
PointNode B = pg.AddNode(new Int3(1, 0, 1));
PointNode C = pg.AddNode(new Int3(1, 0, -1));
PointNode D = pg.AddNode(new Int3(2, 0, 2));
PointNode E = pg.AddNode(new Int3(2, 0, -1));
PointNode F = pg.AddNode(new Int3(-1, 0, 2));
A.AddConnection(B, 1);
A.AddConnection(C, 1);
A.AddConnection(F, 1);
B.AddConnection(C, 1);
B.AddConnection(A, 1);
B.AddConnection(E, 1);
C.AddConnection(A, 1);
C.AddConnection(B, 1);
C.AddConnection(E, 1);
D.AddConnection(E, 1);
D.AddConnection(F, 1);
E.AddConnection(D, 1);
E.AddConnection(C, 1);
E.AddConnection(B, 1);
F.AddConnection(A, 1);
F.AddConnection(D, 1);
}
private void FindPath(Vector3 pa, Vector3 pb)
{
AstarPath.StartPath(ABPath.Construct(pa, pb, OnPathComplete));
}
private void OnPathComplete(Path p)
{
_path = p;
}
Even if set drawGizmos
to true
I can’t see the graph in the Scene view. So I written my own Gizmos viewer and here is what I get.
As you can see the central node (B) is never found and the bottom node © is very hard to reach, and can’t be reached even if you are on it.
What do I do wrong?
Thanks for your help.