How to add penalties / move cost?

I’ve read through the documentation & looked through the AI bot example, but still don’t really understand how to give a node a movement penalty.

I do not see anywhere in the example, where Restricted Area 1 has a movement pentalty while Restricted Area 2 doesn’t. The settings are identical in the Inspector, except for the Tag Value.

However, what script in the example tells A* what tag has what penalty?

Am I missing the obvious document section which describes all of this? This is, to my blind eyes, almost as if it’s not even documented beyond a few hints which leave me guessing.

Hi

The good thing about tags is that their penalties can be agent specific, so you set them on the Seeker component:

Perhaps I should do a better job of explaining all this in the docs.

Just to be clear, to give one of my hexagon tiles a Move Cost, I will need to

  1. Add a GraphUpdateScene script
  2. Set the Tag
  3. Update the Player’s Seeker with each tag’s penalty.

This should be enough help :slight_smile:

It would definitely be great if the documentation was a bit more clear on the steps required to add a move cost.

Thanks for always answering my questions so quickly. You are awesome Aron!

I’m still having some trouble getting the pathfinding to determine the best path by calculating different move costs.

The example scene works great & I understand it now. I changed the Tag Penalty numbers, and it works as intended.

However in my own scene, it isn’t working as it should. The cost of every hex still seems to be the same. I’ve tried scanning/rescanning the graph to make sure it’s always updated, especially after any changes. (Changing the walkability using GraphUpdateScene will block movement on the tile, so that works at least).

But despite higher move costs, the generated paths will move in a straight line through higher cost terrain (rather than going around it).

Code to create Path
seeker.StartPath(currentPathOrigin, lastTileHit.transform.position, OnPathComplete);

Settings for AstarPath, Tiles (GraphUpdateScene), and Player (Seeker)

If I change a Tag Penalty to something outrageous, like 250,000 then it will finally work, but not really (Multiplying by a very large number still doesn’t calculate the right path when there’s multiple terrain types). I only need double digit costs. My hex’s actual game move costs are anywhere from 1 to 100.

Can I not do something simple like,

  • Hill = 25
  • Grass = 1
  • Forest = 3
  • River = 10

And have it correctly generate a path (for example, that would mostly generate paths through Grass, almost always avoiding Hills/River)?

I’d prefer not to have to do something like

  • Hill = 25,000
  • Grass = 1,000
  • Forest = 3,000
  • River = 10,000

Yes that works.
However if it is just one of your hexagon tiles, it is probably easier for you to do it through code.
For example:

    // Add a work item which will be executed when all pathfinding threads have been paused
    AstarPath.active.AddWorkItem(() => {
        // Safe to update graphs here
        var node = AstarPath.active.GetNearest(transform.position).node;
        node.Penalty = 20000;
        // Or you could set a tag
        // node.Tag = 3;
    });

There is also a cost for moving between nodes (as the penalties of all nodes are initially zero) which is essentially the distance between the nodes in millimeters (i.e multiplied by 1000) as the system uses integers for costs internally. I see that you are using a node size of 53.9 which means that the cost of moving between two hexagons will be on the order of 50k That’s why the agents still do not seem to avoid your tiles properly.
It seems that in your game you really don’t want to care about the cost of moving between tiles, just the cost of the tiles themselves. If so you could make one simple modification to the GridGraph to get this behavior: open the GridGenerator.cs file and find the line

 uint straightCost = (uint)Mathf.RoundToInt(nodeSize*Int3.Precision);

and change it to

uint straightCost = 0;

And for pathfinding to still work you will need to change the A* Inspector -> Settings -> Heuristic to None.

When this is done then you could have your costs as e.g 25, 1, 3, 10 as you wanted to.

1 Like

Perfect! Thank you very much again :slight_smile:

Best support EVER! Hahaha!

1 Like

I know this is years later, but I was having this same exact issue and after days of searching and finding NOTHING helpful about why it was generating paths straight through my higher-cost terrain, this was exactly what I needed.

1 Like