Pathfinding and Cost

Hi,
I am using GraphGrid on terrain. I would add a cost into pathfinding related to terrain height… is it possibile out of box ?

Hi

Yes that is possible in the pro version.
Take a look at Grid Graph Settings -> Advanced -> Penalty Modifications -> Position Penalty.
You can also apply penalty based on a texture.

Excellent … I am gonna by the pro version …
One more question … can I create my grid from runtime … I mean without have it in a object in edit. I see that you can update in runtime, but for some specific need I have even to create
Thanks

I’m not quite sure what you mean, but for grid graphs anything that can be done in the editor can be done during runtime as well.

Ok… so it shoudl work.
I mean declare and instanciate the class from C#, without having the object in edit mode into inspector

Yes, you can create graphs from scripts.
You can do something like this

     var graph = AstarPath.active.astarData.AddGraph(typeof(GridGraph)) as GridGraph;
     graph.width = 10;
     graph.depth = 10;
     graph.center = Vector3.zero;
     graph.UpdateSizeFromWidthDepth();
     AstarPath.active.Scan();

yes… thanks a lot for your prompt reply

It works really smoothly …and perfomance are nice …
How can I activate “Show Graph” … I want to show the grid in the scene during the game ( only for my debug )

Hi

Nice!
‘Show Graphs’ is implemented using Unity gizmos (https://docs.unity3d.com/ScriptReference/Gizmos.html) you can enable it in the game view by clicking the ‘Gizmos’ button in the top right corner of the view, however note that this will not show up in a standalone build, it will only show up in the editor.

Yes … I know that … but my problem is that when I create the grid from the scrip ( var graph = AstarPath.active.astarData.AddGraph(typeof(GridGraph)) as GridGraph; ) I cannot get how enable “ShowGraph”
I figure out other parameters to setup the grid, but not that one :flushed:

Ah. You can use

AstarPath.showNavGraphs = true;

Ok … thanks…
one more question …
I have in my requirement the capabilty to calculate the path between two points and show the result
from edit mode. I mean without playing, usign a custom inspector

How can I call the pathfidning in edit mode ? Is there any example ?

Hi

Sorry, pathfinding is currently not possible in edit mode.
Is it not possible to do this in play mode instead?

Hi,
that’s requirement is really on the top of list.
I am opent to work on that, if you think that is something doable changing or creating new class.
Thanks

Antonio

Hi

You can try to call

var as = GetComponent<AstarPath>();
as.Awake();
// Run pathfinding calls here

but I cannot guarantee that it will work well. You might have to remove some if(Application.isPlaying) checks inside that method to get it to work.

Hi Aron,
grid creation by code and scan works from edit. I followed your input ( even few Application.isPaying() to comment )
Now I cannot get any result from pathfinding. I am following the example with the class WaypointPath. But I am not getting any path.

You might have to call AstarPath.Update (potentially several times) or you might have to use AstarPath.WaitForPath to force the paths to be calculated immediately.