[关闭]
@sssunny 2024-04-03T03:54:31.000000Z 字数 1279 阅读 135

AStarPathFinding 学习备忘

AStarPathFinding TiledMap


关键类

  • AStarPath 每一个场景都需要添加一个。这个脚本负责生成,读取场景的寻路网格数据 开始寻路
  • Seeker 负责寻路,挂到需要寻路的对象上
  • GridGraph 生成 tiled 格子的网格地图数据 访问图形数据
  • RecastGraph Navmesh 自动扫描

AStarPathFinding 官网
AStarPathFinding 文档


TiledMap 数据转 GridGraph

目前方向:

  • 方案一:修改 GridGraph 生成
  • 方案二:tiledMap 数据格式生成Prefab,再生成A*网格数据(暂不启用,可以研究)

方案一:修改 GridGraph 生成

GridGraph.ScanInternal();

  1. // 关键代码
  2. for (int z = 0; z < depth; z++) {
  3. // Yield with a progress value at most every N nodes
  4. if (progressCounter >= YieldEveryNNodes) {
  5. progressCounter = 0;
  6. yield return new Progress(Mathf.Lerp(0.1f, 0.7f, z/(float)depth), "Calculating positions");
  7. }
  8. progressCounter += width;
  9. for (int x = 0; x < width; x++) {
  10. // Updates the position of the node 更新节点的位置
  11. // and a bunch of other things 还有其他一些事情
  12. RecalculateCell(x, z);
  13. // Apply texture data if necessary
  14. textureData.Apply(nodes[z*width + x], x, z);
  15. }
  16. }

动态障碍

  1. // Bounds of the collider the last time the graphs were updated
  2. Bounds prevBounds;
  3. var guo = new GraphUpdateObject(prevBounds);
  4. AstarPath.active.UpdateGraphs(guo);

地图上的点判断

  1. private NavmeshBase _recastGraph;
  2. _recastGraph = AStar.data.recastGraph;
  3. // 将包含最接近的节点和该节点曲面上最接近的点
  4. // 如果要检查所有图形,则可以使用 AstarPath.GetNearest
  5. // AStar.GetNearest(pos);
  6. pos = _recastGraph.GetNearest(pos).position;
  1. // 查找包含位置的第一个节点。
  2. // “Contains”定义为从上方观察时位于三角形节点内部的位置。
  3. // 在多层环境的情况下,将返回包含该点的最近节点。
  4. // 如果没有包含该点的节点,则返回null。这起到了快速
  5. // 检查“这一点是否在导航网格上”。
  6. var node = _recastGraph.PointOnNavmesh(pos, NNConstraint.None);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注