大家好，欢迎回来。 Hello and welcome back.
这节课的目的是看看指标或非玩家角色，以及为什么我们需要执行 The purpose of this lecture is to look at indices or non player characters and why we need to implement
寻路。 pathfinding for them.
现在我有了一个更完整的游戏版本我要先玩一玩，然后再讲一遍 So here I've got a more completed version of the game and I'm going to play it and then talk through
我们想要执行寻径的各个方面以及为什么需要执行它们。 various aspects of pathfinding that we want to implement and why we need to implement them.
所以让我们点击“播放”，你就可以看到我们的一个NPC得分上升，然后我们再等一会儿。 So let's just click on play and straightaway you can see one of our NPC score up and we wait a few moments.
取心器开始移动。 Corer will start moving.
所以她从农场的场景移到了小屋的场景你可以看到我们可以跟着她 So she moves from the farm scene into the cabin scene and you can see that we can follow her as part
我们的运动。 of our movement.
我们可以看到她要去哪里，然后跟踪她。云桥网络 yunqiaowang.cn 获取更多精品资源 We can actually see where she's going and follow her.
如果我只是去收集一些工具然后去现场现场。 And if I just go over and collect a few tools and then go over to the field scene.
走到这里，你可以看到另一个NPC布奇，他又开始动了，所以他 And walk over here, you can see we've got another NPC, Butch, and again, he started movin, so he's
从现场出发。 moving from the field scene.
我可以跟着他，如果我一直跟着他，你看，我们的过渡相当顺利 I can follow him, and if I keep following him, you see, we got quite a smooth sort of transition
这样我就可以在两个场景之间切换，他也可以在两个场景之间切换。 here so I can move between scenes and he's moving between scenes.
他在向目的地移动注意他是怎么走的，怎么用这些步骤 And he's moving towards his destination and notice how he actually goes and uses these steps here to
到门廊上去。 get onto the porch.
如果我去看看小木屋，里面还有人。 And if I have a look in the cabin, we've still got caller in there.
现在她又开始走动了，这样我们就能跟踪珂拉了。 And now she started moving again so we can follow Kaura.
注意到她走在台阶上的时候。 And notice she sort of move in his walk over the steps here.
现在她正走在农家院里。 Now she's walking along the the farmyard scene here.
你会注意到她实际上是沿着这条路走的，这是我们实现的另一个东西。 You'll notice she's actually walking along this path here, which is another thing that we've implemented.
你会注意到我们可以随着乐曲的移动而移动。 So you will notice that we can follow the pieces as they move.
所以，又一次，choros有了一条路径并且她正在朝着那条路径前进。 So, again, choros got a path sets and she's moving towards that path.
把这棵树砍倒，我们就能看到她了。 Chop down this tree so we can see her.
所以你可以看到，一款优秀的RPG的特点之一就是看起来很聪明 So you can see that so one of the features of a good RPG is the pieces appear to be intelligence and
有自己的行动和能力。 have their own actions and abilities.
通过移动和行走，这是大使馆拥有的核心属性之一 And by moving and walking, that is one of the sort of core sort of attributes the embassies have got
。 to have.
所以核心又断开了。 So core is off again.
所以我可以跟着她，所以请注意，当我不在一个场景时，戈尔仍然在动，然后她 So I can follow her, so just notice that when I'm not in a scene, Gore is still moving and then she
出现在场景中。 appears into the scene.
再一次，她有一个路径集。 So again, she's got a path set.
所以核心现在从野外场景移到了农场场景。 So Core has now moved from the field scene into the farmyard scene.
如果你现在看，她马上就要搬进小木屋了，所以她刚刚看过三季， And if you look now, she's going to move into the cabin soon, so she's just traversed the three seasons,
从田间开始，搬到农家院子，现在她在小木屋里。 started in the field, move into the farmyard and now she's in the cabin.
在3D游戏中也是如此。 So in 3D games.
Unity内置了寻径功能，所以在3D游戏中，你可以定义哪些削弱措施 Unity has built in capabilities for pathfinding, so in 3D games, you can define Nerf measures which
指定游戏的哪个区域可以训练步行动物。 specify which areas of the game will train a walkable.
对于您的员工，您可以实现或添加神经毒剂组件，以使这些员工 And for your employees, you can implement or add nerve agent components to enable those employees to
在2D游戏中传递、寻找和移动。 pass, find and move around in 2D games.
Unity不提供这个功能。 Unity doesn't provide this functionality.
所以我们要做的是我们需要自己建立这些功能。 So what we've got to do is we need to build that functionality ourselves.
你可以看到，我们的员工现在一起行动了，有了这种行动能力 So you can see again, our employees have now moved together, so by having this movement ability and
这种寻路能力，真的给了员工一种自主感和智慧感。 this pathfinding ability, it really gives the employees a sense of their own autonomy and sort of intelligence.
但实际上，它们所做的基本上就是在两点之间移动它们移动到 But actually, what they're doing is basically just moving between two points and they're moving to
还有时间表。 a schedule as well.
我们跟着来。 So let's just follow.
但你可以看到他的意图。 But you can see where he is going.
所以对于寻径来说，有许多不同的算法或模式能够帮助我们创造寻径 So for pathfinding, there are many different algorithms or patterns available to help build a pathfinding
解决方案。 solution.
我们要用的这个叫做星形，它可能是最流行和最有效的一种 The one that we're going to use is called a star, and it's probably one of the most popular and efficient
生成两点之间最短路径的方法。 ways of generating the shortest path between two points.
如果你注意到了，布奇，他在这两点之间移动，但他实际上 So if you just noticed, Butch, what he's done here is he's moving between two points, but he's actually
在寻径算法中更倾向于使用木质路径。 favored this wooden path here as part of the pathfinding algorithm.
你可以用星号这样你就可以找到严格意义上的最短路径或者你几乎可以增加优先级 And you can do that with a star so you can find strictly the shortest path or you can almost add priority
某些广场。 to certain squares.
这就是我们对这条路径所做的，它有一个运动优先级，所以即使在那里 And that's what we've done with this path, is given it's a movement priority so that even though there
可能会选择一条更短的路径，这在一定程度上是因为 might have been a shorter path of which could have taken is actually chosen, this would in part because
它有优先权。 it's got priority.
所以在我们的游戏中，我们已经使用了收费地图和网格属性网格，所以我们已经在向右移动了 So in our game, we already use toll maps and grid property grids, so we're already going in the right
能够执行star寻径解决方案的方向。 direction to be able to implement an Istar pathfinding solution.
正如你所看到的，以及使用Istar来计算两点之间的路径。 So as you can see, as well as using Istar to calculate paths between two points.
我们也会让员工在游戏中沿着既定的路径移动。 We also animate our employees as they move along the defined path in the game.
你可以看到这丛灌木沿着这条路走现在它离开了这条路。 So you can see the bush is basically following this path and then now he's off the path.
他只是用了一个基本的点。 He's just using a basic point point there.
总是再次选择路径。 Always picks up path again.
维多利亚还在下面 Victoria is still down here.
你可以看到，但是，他遵循了这条路你会注意到，而不是 You can see that, but again, he's followed the path and he you will notice here, rather than just
他试图从这栋楼的一侧走过去，实际上他在这些台阶之间来回移动。 trying to walk up the side of this building here, he actually moved between and through these steps.
这是因为我们已经将地图上的某些区域设置为无声区域。 And that's because we've actually set certain areas of the map to be non-vocal areas.
建筑前面的这些正方形被设置为非本地区域，所以很容易。 So these squares here at the front of the building were set as nonlocal areas and so with ease.
所以布奇所做的一部分就是他只在可步行的地方行走。 So what Butch does as part of the path is he only walks on walkable areas.
所以他的目标是在这里他不能穿过这些区域。 So his target was set here and he couldn't walk through these areas here.
所以他真的走了几步。 So he actually walked through the steps.
所以通过不使用Mokpo区域，Istar算法以及地图不同部分的优先级， So by using no Mokpo areas, the Istar algorithm and sort of priorities for different parts of the map,
你可以在NPC行为中创造一些非常聪明的外观。 you can produce some quite intelligent look in NPC behavior.
所以除了我们将要实现的寻路，你会注意到，正如我们一直在做的 So as well as the pathfinding that we're going to implement, you will notice, as we've been following
这些员工，他们按照时间表做事。 these employees around, that they actually do things on a schedule.
这可能是显而易见的，但当时间到达某一特定时间时，它们实际上会触发一个 It might have been obvious, but when the time reaches a certain time, they actually trigger off one
他们的行为。 of their actions.
你可以看到现在是5点，两个员工的时间表都在5点触发。 So you can see now it's five o'clock and both of the employees have schedules that trigger at 5:00.
这些时间表基本上是让他们搬进船舱。 And those schedules basically get them to move into the cabin.
所以让我停止游戏，让我给你们快速看一下时间表。 So let me just stop the game and I'll just give you a quick look at the schedules.
在我们的资产中，我们定义了一些MPAC计划，事件脚本，账单对象。 So in our assets here, we've defined some MPAC schedule, event scripts, bill objets.
我只需要点击它。 I just click on that.
你可以做的是看一看，我们已经指定了许多不同的时间表，这些 What you can do is have a look to see that we've got a number of different schedules specified and these
是布奇遵循的时间表。 are the schedules that Butches follow in.
你可以看到每个时间表都定义了很多不同的参数，比如时间 And for each schedule where you can see it defines a number of different parameters, like the the hour
和分钟。 and the minutes.
它是否应该发生在特定的一天，是否应该发生，是否应该发生在特定的天气 Whether it should happen on a particular day, whether it should happen, whether a particular weather
是活跃的还是一个特定的季节活跃，它指定了NPC应该移动到的场景 is active or a particular season is active and it specifies the scene that the NPC should move to and
网格坐标。 the grid coordinates.
然后当MBC到达那个点时，它也允许你指定大使馆的方向 And then when the MBC reaches that point, it also allows you to specify a direction that the embassy
是面向，也是动画。 is facing and also an animation.
你可以看到当布什走进门廊时，他在动画中有一根烟被触发了。 So you saw with Bush when he walked onto the porch, he had a smoke in animation that was triggered.
这就是课程表。 So those are the schedules.
如果我们向下滚动，我们可以看到这里有一个5点的时间表。 And if we just scroll down, we can see there was a schedule here for five o'clock.
我们的计划是让布什去小屋现场坐标为0的地方 Um, what our schedule did is we got Bush to move to the cabin scene and to the grid coordinates zero
零。 zero.
当他走到那里的时候，他开始抽他的烟斗。 And then when he reached that, he started smoking his pipe.
所以时间表给出了。 So schedules give a.
有很多功能可以指定主题是如何移动的，以及它们看起来是怎样的 A lot of functionality in being able to specify how topics move and how they almost then appear to have
情报。 intelligence.
我还将向你展示我们如何指定正常区域和优先区域的寻径，所以如果 I'll also show you now how we specify the normal areas and the priority areas for pathfinding, so if
我们只是导航到我们的一个场景，所以我将打开农场场景。 we just navigate to one of our scenes, so I'll open up the farm scene.
我们来载入场景然后到地图网格然后到网格属性 So let's just load scene and we go down to the tall map grid and we go to the grid properties and then
只要启用它，然后展开它。 just enable that and then expand it.
你可以看到我们有网格属性。 You can see we've got to grid property.
托马斯称球路径，然后球和PC障碍。 Tomas called ball path and then ball and PC obstacle.
如果我们去场景视图。 And if we just go to the scene view.
看看NPC障碍，这就是我们现在看到的。 And have a look at NPC obstacle, so that is shown at the moment.
你可以看到所有这些蓝色区域都被标记为NPC障碍。 You can see that all of these blue areas are marked as NPC obstacles.
所以NPC不能移动到地图之外。 So the NPC can't move beyond the map.
它不能在玩家不能行走的区域移动。 It can't move in areas that the player can't walk.
你可以看到在前面，建筑的侧面被标记为障碍物。 And you can see here at the front, the sides of the building are marked as obstacles.
我们有一条小路穿过前面这是一个可步行的区域。 And we've got a little path through the front here that is a walkable area.
所以这就是寻径算法发送NPC的地方，这也是它发送Bertsch的地方 So that is where the pathfinding algorithm sends the NPC and that's where it sent Bertsch when he was
他一边往门廊走去，一边盘算着该往哪里走。 calculating where he should walk as he walked to the porch.
我们只需要在我们定义的网格属性和功能中使用 So we just use in our grid properties and functionality that we defines and we're extending that to
能够为女皇定义这些障碍。 be able to define these obstacles for empresses.
如果我把它隐藏起来，然后让NPC路径可见，你可以再次看到我们在使用这个 Many if I just hide that and then just make the NPC paths visible, you can see again we're using this
标准的抓地力功能，使我们能够定义我们有大使馆的特定路径。 standard grip properties functionality to enable us to define where we have specific paths for the embassy's.
这些路径的工作原理取决于寻径算法是否在计算路径之间的点 And how these paths work is if the pathfinding algorithm is calculating the point between what the path
两点之间，如果有一条路径在附近，它会把这些方块看作是 between two points, if there's a path that is in the vicinity, it will consider these squares as being
优先级的广场。 priority's squares.
所以我们显然可以利用这一点让NPC沿着路径行走，而不是随机行走 So we can obviously use this to get the NPC to walk along paths rather than just walk randomly through
字段，即使它可能是最短路径。 fields, even though it might be the shortest path.
以上就是我们如何使用网格属性来控制NPC路径和障碍的简单介绍。 So that's a quick look of how we use in the grid properties to control NPC paths and obstacles.
在这节课之后，我们会更详细地看一下星型算法 So following on from this lecture, we're going to take a more detailed look at the star algorithm and
它是如何工作的，然后在我们的游戏中编写代码以及在其他功能中编写代码 how it works and then move on to code within our game and also code in the other functionality that
我们刚刚看了，包括在NPC动画和NPC时间表。 we've just had a look at, include in the NPC animations and the NPC schedules.
好的，我期待在那里见到你。 OK, so I look forward to seeing you there.