- Home /
A* Pathfinding questions
I am making a tycoon game and i have chosen to use A* Pathfinding (http://arongranberg.com/astar/) for my AI as i found it so difficult making my own AI system. My tycoon game allows you to place down buildings and paths during runtime, so of course the A* Pathfinding grid has to be updated/scanned. So i added this piece of code "AstarPath.active.Scan();" to re scan every time i place down a path or building etc. The Pathfinding grid updated correctly, but the frame rate crumbles everytime it re-scans. Is there another way to re-scan which would hit my frame rate less, maybe scan in the general area?
A* is generally used to find an efficient route between two nodes in a network - if you explain a little more why/how you are using it in your game we might be able to suggest alternatives (it's not obvious, to me at least, what part of a tycoon game needs graph analysis)
Well the tycoon i am making is a Roller Coaster Tycoon inspired one. So you are able to place down rides, paths and trees.
The above picture shows paths which i have placed down in during runtime. But because i placed down a new path the Pathfinding grid will have to be re-scanned to detect the newly added paths. I would want the Pathfinding grid to update maybe pretty frequently. Sorry if i wan't clear in my answer, posted that about midnight;)
Answer by dsada · Aug 09, 2014 at 08:44 AM
http://arongranberg.com/astar/docs/graph-updates.php
On this link you can see basic examples of GraphUpdateObject that you need. For example if you just want to put in a new obstacle that you dont want to go through write something like this:
GraphUpdateObject guo = new GraphUpdateObject(obstacle.bounds);
guo.modifyWalkability = true;
guo.setWalkability = false;
Astarpath.active.UpdateGraphs(guo);
This is much more performance friendly than scanning over the whole graph again.
Thanks for the reply. I saw that yesterday but i didn't really understand it. So the "GraphUpdateObject" is used to only update the object? Then after you have assigned the new GraphUpdateObject i guess "Astarpath.active.UpdateGraphs(guo);" just tells the grid to update the object and nothing else? I think i might understand it, i'll have a go in a $$anonymous$$ute. $$anonymous$$aybe i was oo tired yesterday or something and that's why i didn't understand it;) Thanks :D
I might as well just post a section of my code as it might help explain a bit easier.
var PathSpawned = Instantiate(StonePath,PathSpawn.transform.position, Quaternion.identity); yield WaitForSeconds(0.5); var PathObj = new GraphUpdateObject(PathSpawned); AstarPath.active.UpdateGraphs(PathObj);
One thing i should say is that i write in Javascript, but it so far A* Pathfinding has worked with it so far. As you can see I Instantiate a stone path on the position of the selection tool, the bit i'm getting a error with is this line "var PathObj = new GraphUpdateObject(PathSpawned);" Where exactly did you get "obstacle.bounds" from? As i tried putting PathSpawned.bounds but it still gave me a error. Would i have to put the script on the path it's self and then do the UpdateGraphs there? Or isn't the variable supposed to be different?
Well for example your meshrenderer has bounds or your collider has bouns. So for example
var PathObj = new GraphUpdateObject(PathSpawned.renderer.bounds);
AstarPath.active.UpdateGraphs(PathObj);
But you can use your collider as well if you have one I just wrote an example.
Dont forget to set those walkability properties that i did because this way you tell nothing to the graph. The nodes are still going to be walkable.
Thanks:D Got it all working and now ins$$anonymous$$d of dropping to about 20 frames per a second it only drops by about 1:D Thanks again for you help, i appreciate it.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How can i stop my game from laging during instantiation? C# 2 Answers
foreach code causes lag spike C# 2 Answers
Mac 10.6.8 lagging with Unity 4.3.2f1 0 Answers
Reduce lag... in Unity Indie? 3 Answers