Make tile in GridGraph unwalkable without modifying height or placing obstacle

Hello,

First of all, thank you for a great asset.

Before I state my question, I want to mention that I did go through the forum and documentation, but I could not find an answer to my question (it happens sometimes with new plugins) :slight_smile:

Question 1:
So, my question is as follows. I have a terrain, all flat, and a Seeker going towards the target. During runtime, I have a flattened cube that follows the mouse on the terrain. The entire terrain is walkable and what I am trying to do, is when I click the mouse, I would like the area beneath the cube following the mouse to become unwalkable.

Here is code sample :

`
if (Input.GetMouseButtonDown (1)) {
Debug.Log (“Clicked Right Mouse”);
Bounds bounds = cube.collider.bounds;

Debug.Log ("bounds = "+bounds);

GraphUpdateObject guo = new GraphUpdateObject(bounds);
guo.modifyWalkability = false;
//guo.updateErosion = false;
//guo.updatePhysics = true;
AstarPath.active.UpdateGraphs(guo);

}
public void OnEnable () {
AstarPath.OnGraphsUpdated += RecalculatePath;
}

public void OnDisable () {
AstarPath.OnGraphsUpdated -= RecalculatePath;
}

public void RecalculatePath (AstarPath astar) {
Seeker sk = creep.GetComponent ();
sk.StartPath (creep.transform.position, target.transform.position, OnPathRecalculated);
}

public void OnPathRecalculated (Path p) {
Debug.Log (“Path Recalculated”);
}
`

I get the “bounds” & “Path Recalculated” message, but the path itself does not change. The only way i got it to change/work is by actually raising the terrain during runtime and then triggering the recalculate.

How can I accomplish this without modifying the terrain beforehand ?

What I am trying to achive in game is for the user to model the terrain, scan the terrain(create grid) and then activate(make walkable) or deactivate(make unwalkable) certain tiles.

Question 2 : Also I have another Question. Can I display during runtime different color tiles for the walkable & unwalkable tiles ? I would like the user to see which ones are marked as walkable / unwalkable.

Best regards,

Hi

Don’t use “guo.modifyWalkability = false;” as that will mark all nodes inside the bounds as unwalkable.
You need to make sure that the collider is in a layer which is included in the Grid Graph -> Collision Testing -> Mask variable, otherwise it will just ignore it.

If it still doesn’t work, take a look at the ObjectPlacer.cs example script which is added to most of the example scenes.

  1. There is nothing like that included out of the box, but you can code it.
    If you have the pro version, check out the PathTypes example scene with the FloodPath.
    You can access node data like
    var nodes = AstarPath.active.astarData.gridGraph.nodes;

Hello,

So after a bit of trial and error, I have progress. I get the red boxes now on the grid under the cube that I use to select the area with.

What I did is create a new Layer ( called PathBlocker) and assigned that layer to the cube that I use the bounds of to modify the Grid. Also in the main AStarPath script, for the created Grid changed the “Collision Testing” Mask to the new layer.

That was creating a new path, but my test unit was still following the old path. In order to fix this I had to update the path of the Unit to the new path.

Modified the “OnPathRecalculated” method like so :

public void OnPathRecalculated (Path p) { Debug.Log ("Path Recalculated"); AIPather aiP = creep.GetComponent<AIPather> (); aiP.SetNewPath (p); }

where AIPather is a game specific script ( followed the instructions to setup A*Star from here : http://www.youtube.com/watch?v=bkCLqPpopvE) :slight_smile:

public void SetNewPath(Path p){ if (!p.error) { path = p; currentWaypoint = 0; Debug.Log ("New Path path.vectorPath.Count = ["+path.vectorPath.Count+"]"); } else { Debug.Log ("New Path error : " + p.error); } }

It seems we posted at the same time :slight_smile:
Thank you for the answer, will try it out and post back. (probably with more questions :slight_smile: )

Hello,

So I looked at the ObjectPlacer.cs file. I am currently using this code :

`
Bounds b = cube.collider.bounds;
GraphUpdateObject guo = new GraphUpdateObject(b);
AstarPath.active.UpdateGraphs (guo);

// onTheSpotUpdate - Flush Graph Updates directly after placing. Slower, but updates are applied immidiately
bool onTheSpotUpdate = true;
if (onTheSpotUpdate) {
AstarPath.active.FlushGraphUpdates();
}
`

Now I do have a problem that persists. I can not seem to make “unwalkable” nodes that are next to each other. As soon as I click to deactivate (make unwalkable) a node, if any adjacent node is deactivated, it gets reactivated.

To show an example, please see this video (notice starting with 0:30) :

How do I fix this, so that I can toggle each node off or on ?

Aron, any ideas as to why this would happen ?

Hi

Since the graph update object recalculated nodes inside those bounds and with a small margin (necessary to make sure all cases are handled), if you later remove the collider and recalculate those nodes again, they will be made walkable (because now there is no collider there).

So one option is to keep the collider in the world when you have updated the graph (I assumed you were doing this before), or option 2 is to use:
guo.updatePhysics = false; guo.modifyWalkability = true; gup.setWalkability = false;
Which will force it to set the nodes to unwalkable (it will not take the collider into account, just the bounds).

Aron,

Thank you very much. This worked perfectly. Just what I was looking for.
Tested and all is great.