How to programmatically tag polygons and apply penalty

I may have been a bit to quick in accepting an answer in my last post.

I’m trying to dynamically tag areas of the map so that I can control the penalty when different types of units traverse the area.

I’ve created a generic GameObject and attached an AstarPath and a GraphUpdateScene to it. I then use those components to try and create tagged complex polygon areas (non-convex) that define my buildings. If I do the following…

`graphUpdateScene.points = polygonPoints;
graphUpdateScene.modifyTag = true;
graphUpdateScene.setTag = 1;
graphUpdateScene.penaltyDelta = 1000; 
graphUpdateScene.resetPenaltyOnPhysics = false;                    
graphUpdateScene.updatePhysics = false;
graphUpdateScene.modifyWalkability = true;
graphUpdateScene.setWalkability = false;
graphUpdateScene.convex = false;
graphUpdateScene.Apply(pathfinding);`

Then I can see in the Scene view that the regions are all marked with red dots and the default ‘ground’ is marked with blue dots (I’m using a Grid Graph with “Path Debug Mode” set to “Areas”).

It looks good at this point, except those areas seem to be permanently “unwalkable”, which makes sense considering setWalkability is set to false, but if I go setWalkability = true, then the entire world is just blue dots (“Path Debug Mode” set to “Areas”)) BUT I do notice that with “Path Debug Mode” set to “Tags” I do see that those areas are of a different colour. Although the blue is almost indistinguishable from the grey, it does look like the tagged regions is working. Unfortunately no penalty seems to exist and the units can just walk right through, despite a supposed penalty value of 1000 being applied to the nodes.

Assuming it did work, I’m still a little unsure how the seeker can apply a penalty to those regions. I’m guessing I would do something like

`seeker.tagPenalties[1] = walkThroughBuildingPenalty;
seeker.StartPath(transform.position, destinationPoint, OnPathComplete);`

where 1 is the index of the tag I’ve tried to apply to those regions.

I notice that a lot of the users on the forums are using a GraphUpdateObject but it seems to only take a Bounds object and not a complex polygon. I saw there was a GraphUpdateObject.shape but apply a GraphUpdateShape didn’t seem to work either.
e.g. I tried this to with no luck. I don’t even get the tagged areas to show up with “Path Debug Mode” set to “Tags”

`GraphUpdateShape gus = new GraphUpdateShape();
gus.convex = false;
gus.points = polygonPoints;
GraphUpdateObject guo = new GraphUpdateObject(new Bounds(verticesTemp[0], new Vector3(10.0f, 0.0f, 10.0f)));
GraphUpdateObject guo = new GraphUpdateObject();
guo.shape = gus;
guo.modifyTag = true;
guo.setTag = 1;
guo.addPenalty = 10000; 
guo.resetPenaltyOnPhysics = false;                    
guo.updatePhysics = false;
guo.modifyWalkability = true;
guo.setWalkability = false;
pathfinding.UpdateGraphs(guo);`

I’ve gotten myself thoroughly confused by this point. Any insight would be most welcome. I’ve tried to apply what’s mentioned in http://arongranberg.com/astar/docs/graph-updates.php and http://arongranberg.com/astar/docs/tags.php

Thanks.

Apologies, I’m going to reply to (answer) my own question.

What I have above with the GraphUpdateScene actually works but I had some other logic in the code which was querying the walkability of the graph and that was interfering with my drag-to-draw-a-path logic.

`bool IsWalkable(Vector2 point) {
...
NNInfo nnInfo = pathfinder.GetNearest( ray.GetPoint(hitDist) );
return nnInfo.node.Walkable;
}
`

That was good for when I was using layer masks for the basic pathfinding but no so good for what I now need.

In case anyone finds this useful, here’s my solution. I tell a seeker that it can’t traverse a certain tagged area using…
seeker.traversableTags = new TagMask( 65535 ^ 2, 1);
…where 65535 is all bits on and 2 turns the 2nd bit off (the area is tagged with the 2nd bit). This looks fairly ugly so it would be great to get better way of doing. Also, I have no idea what the second parameter is supposed to do.

I then test against this with something like…

`bool IsTraversable(Vector2 point) {
...
int result = (1 << (int)nnInfo.node.Tag) & seeker.traversableTags.tagsChange;
return result > 0;
}`

This seems to work so far.