I made an AI that uses checkpoints to move, they use the GetNearest function to calculate paths while in Editor Mode. I use a ‘‘try, catch’’ function that triggers a scan when it doesn’t detect an active graph.
I was wondering how to do this, because the “AstarPath.active.Scan();” doesn’t seem to work, and I also tried using a method of creating a graph from scratch (which was on the forum), but this also doesn’t seem to work.
I have also tried “AstarPath.active.graphs.Length”, which does return a graph. But I am not able to scan it with code during Editor Mode and get the same result I get when I press the “Scan” button in the Inspector.
if (_cachedCheckpointList.Count > 0) { for (int i = 0; i < _cachedCheckpointList.Count; i++) { try { if (AstarPath.active.GetNearest(_cachedCheckpointList[i]).node.Walkable) { Gizmos.color = Color.green; Gizmos.DrawSphere(_cachedCheckpointList[i], 0.25f); if (i != _cachedCheckpointList.Count - 1) { Debug.DrawLine(_cachedCheckpointList[i], _cachedCheckpointList[i + 1], Color.green); } else { Debug.DrawLine(_cachedCheckpointList[i], _cachedCheckpointList[0], Color.green); } } else { Vector2 nearestWalkablePosition = AstarPath.active.GetNearest(_cachedCheckpointList[i], NNConstraint.Default).position; Gizmos.DrawSphere(nearestWalkablePosition, 0.25f); if (i != _cachedCheckpointList.Count - 1) { Debug.DrawLine(_cachedCheckpointList[i], _cachedCheckpointList[i - 1], Color.red); Debug.DrawLine(_cachedCheckpointList[i], _cachedCheckpointList[i + 1], Color.red); Debug.DrawLine(_cachedCheckpointList[i - 1], nearestWalkablePosition, Color.green); Debug.DrawLine(_cachedCheckpointList[i + 1], nearestWalkablePosition, Color.green); } else { Debug.DrawLine(_cachedCheckpointList[i], _cachedCheckpointList[i - 1], Color.red); Debug.DrawLine(_cachedCheckpointList[i], _cachedCheckpointList[0], Color.red); Debug.DrawLine(_cachedCheckpointList[i - 1], nearestWalkablePosition, Color.green); Debug.DrawLine(_cachedCheckpointList[0], nearestWalkablePosition, Color.green); Gizmos.color = Color.red; Gizmos.DrawSphere(_cachedCheckpointList[i], 0.25f); } _hasBeenScanned = false; } } catch { if (!_hasBeenScanned) { // This holds all graph data AstarData data = AstarPath.active.data; // This creates a Grid Graph GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph; // Setup a grid graph with some values int width = 150; int depth = 150; float nodeSize = 1; // For making it a grid graph gg.neighbours = NumNeighbours.Six; gg.uniformEdgeCosts = true; // This illustrates why it's usually easier to configure the settings in the unity inspector and then create the nodes from script gg.isometricAngle = 90 - Mathf.Atan(1 / Mathf.Sqrt(2)) * Mathf.Rad2Deg; gg.inspectorGridMode = InspectorGridMode.Hexagonal; gg.center = new Vector3(10, 0, 0); // Updates internal size from the above values gg.SetDimensions(width, depth, nodeSize); // Scans all graphs AstarPath.active.Scan(); _hasBeenScanned = true; } } } }