今天主要讲了脚本:
DynamicGridObstacle.cs
它是挂在障碍物上的,可以用此脚本实现动态更新路点;
主要讲了2个方法:
1.OnDestroy方法:它会声明一个guo变量,当障碍被清除,guo变量仍然使得障碍阻挡的路点,继续阻挡住;
这时AstarPath.active.UpdateGraphs(guo),变量用guo
此效果,可以考虑用于移动中,留下脚印或印记
2.DoUpdateGraphs方法:
它也和动态更新路点有关,讲师改写了一下,就获得了这样的效果:
拖动障碍物,障碍物经过的区域,都变成不可通过的路点了;
if (!coll.enabled) {
AstarPath.active.UpdateGraphs(prevBounds);
} else {
Bounds newBounds = coll.bounds;
var guo = new GraphUpdateObject(newBounds);
guo.modifyWalkability = true;
guo.setWalkability = false;
Bounds merged = newBounds;
merged.Encapsulate(prevBounds);
if (BoundsVolume(merged) < BoundsVolume(newBounds) + BoundsVolume(prevBounds)) {
guo.bounds = merged;
AstarPath.active.UpdateGraphs(guo);
} else {
guo.bounds = prevBounds;
AstarPath.active.UpdateGraphs(guo);
guo.bounds = newBounds;
AstarPath.active.UpdateGraphs(guo);
}
这篇博客重点介绍了如何使用Unity中的A*寻路系统动态更新障碍物。通过挂载'DynamicGridObstacle.cs'脚本在障碍物上,实现障碍物移除后路径的正确更新。OnDestroy方法在障碍物销毁后依然保持其对路径的影响,而DoUpdateGraphs方法则允许在障碍物移动过程中实时更新其经过区域的可达性。

348

被折叠的 条评论
为什么被折叠?



