Support Forum

How change the position of A* Gridgraph?

Hello. First, your asset is very helpful for me. Thanks!

I am developing Real-time RPG game, so I make A* Gridgrapgh on scene.
But, map on game is very huge, then can`t cover all map at one A* Gridgrapgh.

So, I thought that ‘I change A* Gridgrapgh position and rescan in runtime’, but I can`t.

I read http://arongranberg.com/vanillaforums/discussion/1180/rescale-grid-graph-during-run-time/p1, and http://arongranberg.com/astar/docs/class_pathfinding_1_1_grid_graph.php. But, scanned graph in runtime is empty.

Below is my script.

GameObject A;
AstarPath astar;
Pathfinding.AstarData data;
Pathfinding.GridGraph gg;

// Use this for initialization
void Awake () {
	GameObject A = GameObject.Find ("A*");
	astar = A.GetComponent("AstarPath") as AstarPath;
	data = AstarPath.active.astarData;
	gg = data.AddGraph(typeof(Pathfinding.GridGraph)) as Pathfinding.GridGraph;

	
	gg.width = 10;
	gg.depth = 10;
	gg.nodeSize = 0.25f;
	gg.rotation = new Vector3 (90f, 0f, 0f);
	gg.UpdateSizeFromWidthDepth();

}

void Rescan () {
	gg.center = transform.position; //  transform.position is player transform.position.
	AstarPath.active.Scan();

}

Thanks. :wink:

Hi

Don’t do that in Awake.
The AstarPath script initializes itself during Awake, so depending on the execution order, the AstarPath script might override your changes.
Configure it during Start instead.

Also, getting it AstarPath component is simple:
AstarPath astar = AstarPath.active

Thanks for your answer!~ Success!!!

Below is my Test script.

`
AstarPath astar;
Pathfinding.AstarData data;
Pathfinding.GridGraph gg;

// Use this for initialization
void Start () {

	astar = AstarPath.active;

	Pathfinding.AstarData data = astar.astarData;

	gg = data.gridGraph;

	gg.width = 10;
	gg.depth = 10;
	gg.nodeSize = 0.25f;
	gg.rotation = new Vector3 (90f, 0f, 0f);

	gg.UpdateSizeFromWidthDepth();

}

// Update is called once per frame
void Update () {
	gg.center = transform.position;
	astar.Scan ();
	Debug.Log ("Scan" + transform.position+ " Grid center"+ gg.center);
}

`

2 Likes

Dude ty SO MUCH! I was searching for this forever! i was about to pull my hair out. Working on this for hours!

@Jacob_Gallow

For more information you might also be interested in this page: https://arongranberg.com/astar/docs/runtimegraphs.html