In the hexagonal map, I want to change the AIPath’s maxSpeed according to the type of tile in Tilemap.
for now, I have to calculate this information every frame.
Do not like to calculate it too frequently, Is there any callback that is invoked when the AIPath enters a new GridNode or a variable represents the current GridNode which the AIPath is in for me to compare so that I will do calculation only when the current GridNode is changed
Hi
There’s not. However, you can call AstarPath.active.GetNearest every frame to check which node the agent is in. If you want better performance, you can cache the last result and during the next frame check node.ContainsPoint to see if the agent is inside the same node as last frame (this method only exists for grid nodes in the beta version, though).
for now. I store the last GridNode and every frame calculate the current GridNode which the agent is in and update only when there two GridNodes are different like the code below
private GridNode last;
void TryUpdateNode(){
var now = AstarPath.active.GetNearest(...);
if( now != last){
// do something
last = now;
}
}
as for your suggestion of node.ContainsPoint, I thought the time complexity of both AstarPath.active.GetNearest and node.ContainsPoint is O(1) ?
I did not test the node.ContainsPoint, if it is better than AstarPath.active.GetNearest, i will change it
to node.ContainsPoint
Both have the same time complexity, yes. But ContainsPoint is likely at least 10 times faster since it just does a lot less work.
But you’ll still need to fall back to GetNearest if contains point returns false.
Yeah, the ContainsPoint is faster. but when the agent walks between the border of two hexagonal tiles. It will return false so I need to call AstarPath.active.GetNearest(), the problem is when I call ContainsPoint for the node returned by the AstarPath.active.GetNearest(), which always returns false when the agent is at the border (check the code below). so my agent needs to call these two functions until he has walked into another tile, it is 5 - 10 frames in the meantime.
var pos = transform.position; // the position of the agent
if(!last.ContainsPoint(pos)){
GridNode node = AStarPathUtil.GetNearest(pos);
Assert.IsTrue(node.ContainsPoint(pos)); // failed when it is in border
Assert.IsTrue(node != last); // failed when it is in border
}
but when I use my previous method which calls the AstarPath.active.GetNearest every frame. it is no problem while agent’s passing the border