I am making an RTS game and I would like a* to do some of the work for me to avoid idle units so that the local avoidance has to do less work.
I was hoping to achieve that with this bit of code:
Bounds b = unit.collider.bounds;
b.size = b.size * 1.2f;
GraphUpdateObject guo = new GraphUpdateObject(b);
//guo.addPenalty = 10000000;
guo.requiresFloodFill = false;
guo.updateErosion = false;
Now it works really smoothly when updating the graph without adding a penalty. Unfortunately i cant do that because the units are actually mobile, I just want to discourage other units to drive directly at them so adding a penalty is exactly fulfilling my needs.
The problem is that when I add a penalty the game becomes extremely laggy because I need to apply the bounding box of every unit on the map that is currently not moving to it.
Is there a workaround to efficiently add a penalty to multiple areas(boundingbox locations) at once?
Try to set
guo.updatePhysics = false;
During normal updates, a lot of stuff is recalculated, like the node’s position and other things. When disabling “updatePhysics” you disable those checks and it will run much faster.
You can also update the node penalties manually, but I do not recommend that. It would be faster though.
GridGraph gg = AstarPath.active.astarData.gridGraph; // Update penalty of node at index [5,23] int x = 5; int z = 23; gg.nodes[z*gg.width +x].penalty = 100000;
Even when setting the updatePhysics to false it lagged. But its not that weird since its actually doing like 25+ updates per second and I suppose the graphs just quite simply arent made for that kind of stuff.
The last method however works great, I have not noticed any FPS spikes at all. Do you think issues will occur if i update the penalty of several hundred nodes every second?
Several hundred nodes shouldn’t be much of a problem.
Actually it is not safe to update the penalties like that, because the pathfinding is running at the same time.
However since it is only penalties, it is usually fine, if you would have updated walkability then you might have run in to problems.