﻿1
00:00:00,030 --> 00:00:04,149
该视频是由广告中的广告组合赞助的，我们将做一些
this video is sponsored by ad mix in this video we're going to make some

2
00:00:04,349 --> 00:00:08,319
简单的塔防AI，我们将拥有可以放置的塔
simple tower defense AI we're going to have our towers that we can place and as

3
00:00:08,519 --> 00:00:11,919
在塔将瞄准的攻击范围内产生敌人
enemies get spawned an approach within the attack range the tower will aim and

4
00:00:12,119 --> 00:00:16,570
射击塔也可以通过扩展范围和损坏来升级
shoot the towers are also upgradable with expandable range and damage let's

5
00:00:16,769 --> 00:00:20,828
开始 
begin [Music]

6
00:00:21,820 --> 00:00:26,429
好吧，这就是我们要在这里做的是将我的塔放在
okay so here is what we're trying to make over here is my tower placed in the

7
00:00:26,629 --> 00:00:31,890
地图，当我将鼠标悬停在上面时，可以看到该塔的攻击范围
map and when I mouse over I could see the attack range of this tower and by

8
00:00:32,090 --> 00:00:36,689
按下一个按钮，我就可以发动敌人的波浪，使敌人开始产生
pressing a button I can start the enemy wave so the enemy start being spawned

9
00:00:36,890 --> 00:00:40,318
他们将沿着自己的道路前进，并进入
and they're going to follow their own path and as they get within the range of

10
00:00:40,518 --> 00:00:44,669
塔塔开始攻击并发射箭，箭击中
the tower the tower starts attacking and firing arrows and the arrows hit the

11
00:00:44,869 --> 00:00:49,649
敌人，他们受到足够的打击而受到伤害，敌人死了，所以我有一座塔
enemies and they get damaged with enough hits the enemies die so I have one tower

12
00:00:49,850 --> 00:00:53,699
但我也可以轻松地生成更多塔楼，并且生成
but I can also easily spawn a whole bunch more towers and the spawning is

13
00:00:53,899 --> 00:00:57,178
正确放置在网格上，因此我们使用的是在其中创建的网格系统
correctly placed on a grid so we're using the grid system that we created in

14
00:00:57,378 --> 00:01:01,168
以前的视频，我们可以通过启用Gizmos将其可视化，这里是
a previous video and we can visualize it by enabling gizmos there it is here's

15
00:01:01,368 --> 00:01:04,950
我们的网格，所以这里是另一座塔，但是现在你走了，我们
our grid so it's one another tower right down here and yet there you go now we

16
00:01:05,150 --> 00:01:09,539
有两个塔，现在我们可以产生更多的敌人，他们来了， 
have our two towers and now we can spawn some more enemies and here they come and

17
00:01:09,739 --> 00:01:13,349
如您所见，这些塔也可以升级，因此在左侧
as you see the towers are also upgradable so here on the left side we

18
00:01:13,549 --> 00:01:17,849
可以增加射程，在右侧我们可以增加伤害，所以它
can increase the range and on the right side we can increase the damage so it

19
00:01:18,049 --> 00:01:21,450
催生了更多的敌人，让我一见就知道
spawned a bunch more enemies and let's see and yep as soon as I get within

20
00:01:21,650 --> 00:01:26,219
他们开始受到打击，现在受到4548点伤害
range they start getting hit and now they're taking 4548 damage the damage

21
00:01:26,420 --> 00:01:30,418
我们在上一个视频中也弹出了弹出窗口，所以请检查一下，就像这样
pop ups we're also made in a previous video so check it out and just like this

22
00:01:30,618 --> 00:01:34,228
我们有一些非常简单的塔防AI工作，因此您可以右键单击以
we have some very simple tower defense AI working so you can right-click to

23
00:01:34,429 --> 00:01:39,808
再放一堆塔楼，然后产生一堆敌人，他们
place a whole bunch more towers and now spawn a bunch more enemies and they

24
00:01:40,009 --> 00:01:42,959
当它们进入任何一个塔的范围内时，它们就会开始出现
start coming as they get within range of any of the towers the towers start

25
00:01:43,159 --> 00:01:47,548
打他们，如果我想我可以升级他们，等等等等，以便
hitting them and if I want I can upgrade them and so on and so on in order to

26
00:01:47,748 --> 00:01:50,819
从长远来看，继续开发游戏，您需要考虑营利
continue building your games in the long run you need to think about monetization

27
00:01:51,019 --> 00:01:54,838
AB混合是一个很好的工具，可以帮助您解决AD混合问题
AB mix is a great tool to help you solve that with AD mix you can place

28
00:01:55,039 --> 00:01:58,918
游戏中的非侵入式集成广告，而不是难看的横幅
unintrusive integrated ads inside of your game so instead of an ugly banner

29
00:01:59,118 --> 00:02:02,819
广告在您的游戏内部拥有一个广告牌，这对于
ad you have a billboard right inside your game this is especially useful for

30
00:02:03,019 --> 00:02:07,048
通过AR和VR内容获利而又不影响玩家的沉浸感，并且
monetizing AR and VR content without breaking player immersion and since the

31
00:02:07,248 --> 00:02:10,529
广告的干扰性较小，可以带来更好的播放器体验， 
ads are less intrusive that leads to a better player experience which leads to

32
00:02:10,729 --> 00:02:13,920
较高的保留率，可让您赚更多的钱，因此您可以继续建房
higher retention which allows you to make more money so you can keep building

33
00:02:14,120 --> 00:02:17,100
您的游戏真棒，该工具非常易于使用
your awesome game the tool is extremely easy to use

34
00:02:17,300 --> 00:02:21,118
只需下载unity插件并导入您的场景，您将看到此
just download the unity plugin and import into your scene you'll see this

35
00:02:21,318 --> 00:02:24,420
窗口，您可以在其中选择各种尺寸并拖放
window where you can select from a variety of sizes and drag and drop the

36
00:02:24,620 --> 00:02:28,649
将对象混合到您的场景中，该对象具有很好的适应性，因此可以适合任何对象
ad mix object into your scene the object is very adaptable so it can fit any

37
00:02:28,848 --> 00:02:31,349
像平坦的东西或将其放在周围
surface like something flat or put it around

38
00:02:31,549 --> 00:02:35,219
如您所见，它非常易于使用，并且拥有游戏内广告
as you can see it's extremely simple to use and having in-game ads is a lot

39
00:02:35,419 --> 00:02:38,969
设置完所有广告后，比烦人的横幅广告要好
better than annoying banner ads once you've set up all your ads click this

40
00:02:39,169 --> 00:02:43,110
按钮，以在您不开始生成时验证您的应用
button to your website to validate your app once you do don't start generating

41
00:02:43,310 --> 00:02:48,179
不到24小时即可获得收入，因此广告组合是一种非常易于使用的工具， 
revenue in under 24 hours so ad mix is an extremely easy to use tool that works

42
00:02:48,378 --> 00:02:52,140
比标准广告更好，除非您立即开始赚钱
better than standard ads unless you start earning revenue immediately go to

43
00:02:52,340 --> 00:02:56,069
广告混入开始并下载n mix unity插件以开始赚钱
ad mix start in and download the n mix unity plugin to start earning revenue

44
00:02:56,269 --> 00:03:00,330
今天感谢您添加组合，也感谢这些出色的支持者
today thank you to add mix and also thank you to these awesome supporters

45
00:03:00,530 --> 00:03:04,170
有关使此视频成为可能的信息，请访问patreon.com/scishow 
for making this video possible go to patreon.com/scishow

46
00:03:04,370 --> 00:03:07,439
编码猴子以获得一些好处，并帮助所有人免费观看视频
code monkey to get some perks and help keep the videos free for everyone

47
00:03:07,639 --> 00:03:12,390
好吧，这个圈子，让我们开始吧，好了，这就是我们的主演
alright so this circle let's get to it okay so here we are in our starring

48
00:03:12,590 --> 00:03:17,310
我们所拥有的场景就是基本的地图设置，因此您可以看到它们的路径
scene all we have is the basic map setup so as you can see the path where they

49
00:03:17,509 --> 00:03:21,209
按下按钮不会拿走我可以生出一些敌人，然后你就去了
won't take by pressing a button I can spawn some enemies and there you go they

50
00:03:21,408 --> 00:03:25,259
在那里的角落产生，他们终于开始移动敌人
get spawned on the corner there and they finally start moving the enemies go

51
00:03:25,459 --> 00:03:28,500
穿过那里的力量，穿过那里，在那里我
through the power through there there and out through there also in here I

52
00:03:28,699 --> 00:03:32,580
有一个网格设置，所以如果我们启用gizmos，您可以看到所有
have a grid setup so if we enable gizmos there you go we can see all of the

53
00:03:32,780 --> 00:03:36,480
在上一个视频中完全制作了网格系统的各个网格位置，因此
various grid positions the grid system was fully made in a previous video so

54
00:03:36,680 --> 00:03:40,200
检查一下，在这种情况下，我们将使用网格来
check that out and in this case we're just going to use the grid in order to

55
00:03:40,400 --> 00:03:43,110
将塔盖印到其位置，例如，我们将能够放置
stamp the towers into their position so for example we'll be able to place a

56
00:03:43,310 --> 00:03:47,429
塔这里一个这里一个这里等等等等，所以让我们开始做
tower here one here one here and so on alright so let's begin by making the

57
00:03:47,628 --> 00:03:51,539
在项目文件中可以看到塔的视觉效果，例如，我有一些纹理
tower visual here in the project files I have some textures for example this one

58
00:03:51,739 --> 00:03:56,310
用塔将其拖到场景上，现在让我们制作一个空游戏
with a tower let's drag it onto the scene and now let's make a empty game

59
00:03:56,509 --> 00:04:02,670
对象，这将是我们的主塔，我们将塔精灵拖入我们的内部
object this will be our main tower and we drag the tower sprite inside of our

60
00:04:02,870 --> 00:04:06,990
塔游戏对象，这样我们现在可以轻松地定位精灵，以便
tower game object so this way we can now easily locate the sprite in order to

61
00:04:07,189 --> 00:04:10,710
确保它与网格位置匹配现在我们还要添加另一个
make sure that it matches with the grid position now let's also add another

62
00:04:10,909 --> 00:04:14,909
空通道游戏对象，所以是一个空的，我们称之为
empty channel game object so an empty one yep and let's call this the

63
00:04:15,109 --> 00:04:20,310
弹药适合的位置，所以这是弹丸将要放置的位置
projectile suit from position so this is the position where the projectiles will

64
00:04:20,509 --> 00:04:25,050
被解雇，所以在这种情况下，我们只需将其定位在
be fired from so in this case let's just locate it right up there at the top of

65
00:04:25,250 --> 00:04:29,968
塔就好了，就像这样，我们有了一个非常基本的视觉设置，现在让我们
the tower ok so just like this we have a very basic visual setup now let's make

66
00:04:30,168 --> 00:04:33,718
要处理的脚本，所以让我们创建一个新的C Sharp脚本并附带
the script to handle so let's create a new C sharp script and come with just

67
00:04:33,918 --> 00:04:39,329
我们的塔让我们将其拖到游戏对象上，然后打开确定，首先让我们抓取
our tower let's drag it on to the game object and open ok now first let's grab

68
00:04:39,529 --> 00:04:43,299
参考我们拍摄前的位置
reference to our shoot front position

69
00:04:45,550 --> 00:04:49,319
所以这里就是我们要做的一个转换文件，我们得到了那个位置
so there it is we just do a transform file and we get the position of that

70
00:04:49,519 --> 00:04:53,310
游戏对象，这样我们可以在视觉上找到它，然后获得位置
game object so this way we can visually locate it and then we get the position

71
00:04:53,509 --> 00:04:58,439
通过代码，现在在这里我们还捕获鼠标单击，所以我们做一个
through code and now here let's also capture the mouse click so let's do a

72
00:04:58,639 --> 00:05:03,000
完美男孩更新，在我们的更新内，您只需输入一个简单的鼠标即可
perfect boy update inside of our update you just do a simple input get mouse

73
00:05:03,199 --> 00:05:08,670
按下鼠标左键，仅用于测试，当我们单击时使用
button down left mouse button and just for testing when we click let's use a

74
00:05:08,870 --> 00:05:14,759
只要您可以下载实用程序中的功能，就可以弹出一个弹出窗口
function from the utilities to spawn a pop up as long as you can download the

75
00:05:14,959 --> 00:05:18,540
从统一代码免费获得的实用工具猴子平静了，就这样，让我们​​来看看
utilities for free from unity code monkey calm so just like this let's see

76
00:05:18,740 --> 00:05:22,379
如果可以的话，我们在这里，那里是塔楼，然后单击，然后就走了，我们
if it's working okay here we are there's the tower and click and there you go we

77
00:05:22,579 --> 00:05:25,560
让我们的点击正确运行，以便正确运行塔脚本
have our clicks correctly working all right so the tower script is correctly

78
00:05:25,759 --> 00:05:29,790
运行，现在我们有了塔，让我们先发射弹丸，让我们
running so now that we have our tower let's fire off a projectile first let's

79
00:05:29,990 --> 00:05:34,980
创建我们的射弹，所以在这里，我有一个简单的箭头精灵， 
create our projectile so over here I have a simple sprite of a arrow so just

80
00:05:35,180 --> 00:05:38,939
将其拖动到场景上，是的，现在有投射箭头，让我们
drag it onto the scene and yep there's the projectile arrow now let's make a

81
00:05:39,139 --> 00:05:43,860
脚本来控制它，因此我们为射箭创建了新的c-sharp脚本
script to control it so we create the new c-sharp script for projectile arrow

82
00:05:44,060 --> 00:05:48,990
并将脚本直接拖到它上面好吧，现在我们想在执行期间生成箭头
and drag the script straight onto it ok now we want to spawn our arrows during

83
00:05:49,189 --> 00:05:53,610
运行时，为了做到这一点，让我们先将箭头打入预制
runtime so in order to do that let's make our arrow into a prefab so first

84
00:05:53,810 --> 00:05:58,139
让我们创建一个文件夹来构成我们的预制件，我们只需拖动箭头
let's create a folder to form our prefabs and we simply drag the arrow

85
00:05:58,339 --> 00:06:04,650
到目前为止，我们将其重命名为PF射弹箭头
onto it let's rename it PF projectile arrow alright so far so good we have a

86
00:06:04,850 --> 00:06:09,720
预制射箭的预制件现在我们要在射击时生成箭头
prefab for the projectile arrow now we want to spawn arrows when we shoot so

87
00:06:09,920 --> 00:06:14,340
让我们打开射箭类，在这里让我们做一个简单的静态
let's open up the projectile arrow class and in here let's make a simple static

88
00:06:14,540 --> 00:06:21,329
函数以生成箭头，因此称为create and in的私有静态void 
function in order to spawn a arrow so private static void called create and in

89
00:06:21,529 --> 00:06:26,220
在这里我们要实例化您将要实例化我们的PF弹射箭头
here we want to instantiate you're going to instantiate our PF projectile arrow

90
00:06:26,420 --> 00:06:31,230
因此，在这里自然需要我们对预制件的参考，在这里
so naturally in here we need a reference to our prefab and for that over here in

91
00:06:31,430 --> 00:06:35,250
现场我已经有了一个非常家庭电话游戏资产类，它是在
the scene I already have a very home phone game assets class this was made in

92
00:06:35,449 --> 00:06:38,850
上一个视频，因此您可以看到该对象具有一堆字段
a previous video so this object as you can see it has a bunch of fields a bunch

93
00:06:39,050 --> 00:06:43,439
参考，它有这个脚本，这个脚本的作用是我们有一个
of references and it has this script and this script what it does is we have a

94
00:06:43,639 --> 00:06:48,420
静态实例，然后我们有一堆公共字段，所以在这里我可以
static instance and then we have a whole bunch of public fields so in here I can

95
00:06:48,620 --> 00:06:52,980
只需为PF抛射箭头添加小马变换
simply add a pony transform for the PF projectile arrow

96
00:06:53,180 --> 00:06:59,050
现在回到这里的箭头脚本中，我现在可以访问游戏资产访问权限
and now back in the arrow script in here I can now access the game assets access

97
00:06:59,250 --> 00:07:03,370
这些静态实例和重力射弹箭头再次涵盖了游戏
these static instance and gravity projectile arrow again I cover the game

98
00:07:03,569 --> 00:07:07,720
资产脚本在其他视频中有详细说明，因此请查看说明中的链接，以便
asset script in detail in another video so check the link in the description so

99
00:07:07,920 --> 00:07:11,470
在这里我们可以生成一个箭头，我们还可以接收到一个生成的向量340 
here we can spawn an arrow and let's also receive a vector 340 spawned

100
00:07:11,670 --> 00:07:17,110
位置，所以我们以四元数身份在那里生成
position so we spawn in there with quaternion identity all right so this

101
00:07:17,310 --> 00:07:21,670
应该产生一个弹箭，以便进行测试，让我们尝试在
should spawn a projectile arrow so for testing let's try to spawn it on the

102
00:07:21,870 --> 00:07:25,600
鼠标的位置，所以首先要公开而不是私有，现在让它公开
mouse position so first instead of private let's make this public and now

103
00:07:25,800 --> 00:07:29,410
进入塔楼，然后在这里按下鼠标左键，我们开始吧
go into the tower and then here when you press on your left mouse button let's go

104
00:07:29,610 --> 00:07:33,610
进入项目向下箭头脚本，以创建一个新的箭头和一个免费的
into the project down arrow script in order to create a new arrow and a free

105
00:07:33,810 --> 00:07:39,850
位置让我们使用鼠标位置，所以这里再次使用另一个功能
position let's use the mouse position so here again using another function from

106
00:07:40,050 --> 00:07:43,990
该实用程序是为了使鼠标在此处很少位置
the utilities in order to get the mouse little position here is the function in

107
00:07:44,189 --> 00:07:47,139
如果您想自己制作，可以看到非常简单，只需抓住世界
case you want to make it yourself as you can see very simple just grab the world

108
00:07:47,339 --> 00:07:50,980
相机并在屏幕位置上做一个屏幕旋转点，所以让我们
camera and do a screen twirl point on the screen position all right so let's

109
00:07:51,180 --> 00:07:54,490
测试一下，看看您何时按向左键，箭头是否在Senshi中
test and see if we see an arrow being in Senshi ated when are you pressing left

110
00:07:54,689 --> 00:07:57,069
鼠标键我们需要做的最后一件事就是拍摄
mouse button the last thing we need is just to film

111
00:07:57,269 --> 00:08:00,910
有了这个参考，所以我们只是将预制件拖到那里好吧，让我们测试一下
with this reference so we just drag the prefab right in there okay let's test

112
00:08:01,110 --> 00:08:04,960
好的，我们在这里，然后单击，然后您就可以找到正确产生的箭头
okay here we are and click and there you go there's the arrow correctly spawned

113
00:08:05,160 --> 00:08:09,370
太棒了，所以我们正确地产生了箭头预制件
right awesome so we have the arrow prefab being correctly spawned in the

114
00:08:09,569 --> 00:08:13,180
正确的鼠标位置，现在我们不想在鼠标位置上生成它，但是
correct Mouse position now we don't want to spawn it on the mouse position but

115
00:08:13,379 --> 00:08:16,780
而是我们想在塔上生成它并朝某个位置射击，所以
rather we want to spawn it on the tower and shoot towards a certain position so

116
00:08:16,980 --> 00:08:20,770
在这种情况下，朝着鼠标的位置射击，因此返回箭头脚本
in this case shoot towards the mouse position so back in the arrow script

117
00:08:20,970 --> 00:08:25,170
让我们接收一个生成位置，然后是目标位置的vector3 
let's receive a spawn position and then also a vector3 for the target position

118
00:08:25,370 --> 00:08:33,370
现在在我们的对象中，让我们创建一个设置函数，以便我们可以
and now here in our object let's make a setup function just so that we can

119
00:08:33,570 --> 00:08:40,209
接收目标位置的向量三，当我们创建
receive our vector three for the target position and when we create to

120
00:08:40,409 --> 00:08:47,259
实例化并获取箭头变换，然后获取该类型的组件
instantiate and grab the arrow transform then we do get component of this type

121
00:08:47,460 --> 00:08:54,839
投射箭头，然后我们可以调用安装程序并传递到目标位置
projectile arrow and then we can call setup and pass in our target position

122
00:08:55,039 --> 00:08:58,329
好吧，现在当我们调用此函数时，我们将传递
all right so now when we call this function we're going to pass in the

123
00:08:58,529 --> 00:09:02,409
生成和Atari位置，现在我们有了这个位置，我们需要做的就是
spawn and Atari position and now that we have this position all we need to do is

124
00:09:02,610 --> 00:09:05,979
确保我们的项目我们的箭头朝着它前进，所以现在在这里
make sure that our project our arrow moves towards it so now here

125
00:09:06,179 --> 00:09:10,209
我们需要问一个问题，即我们应如何实质上移动投影机
we need to ask a question which is how should we move the projector essentially

126
00:09:10,409 --> 00:09:14,229
我们有两个选择，我们可以手动移动它，也可以使用物理
we have two options we can move it manually or we can use the physics

127
00:09:14,429 --> 00:09:17,828
系统，这将取决于您的设计目标和您要尝试的目标
system so that's going to depend on your design goals and what you're trying to

128
00:09:18,028 --> 00:09:21,549
在这里处理您的游戏，我们将以最简单的方式进行
do with your game here we're going to do the simplest way possible so just

129
00:09:21,750 --> 00:09:26,139
无需物理操作即可手动移动变换，因此仅在我们的私人男孩上
manually moving the transform with no physics so just on our private boy

130
00:09:26,339 --> 00:09:33,399
更新我们计算朝向目标位置的方向，这样我们就可以
update we calculate the direction towards the target position so we have

131
00:09:33,600 --> 00:09:40,240
移动方向我们以一定的移动速度向移动方向移动
the move direction we move towards the move direction by a certain move speed

132
00:09:40,440 --> 00:09:48,429
然后我们检查一下距离，以便我们是否足够接近
and after we do let's just check the distance so if we are close enough to

133
00:09:48,629 --> 00:09:51,609
目标位置，然后我们就可以摧毁这个游戏对象
the target position then we simply destroy this game object all right so

134
00:09:51,809 --> 00:09:55,508
那里有一些非常简单，非常基本的运动代码，现在我们可以回到
there it is some very simple very basic movement code now we can go back into

135
00:09:55,708 --> 00:09:59,769
我们的塔脚本，在这里，我们在函数中有两个参数，第一个
our tower script and in here now we have two parameters in our function the first

136
00:09:59,970 --> 00:10:03,399
一个是生成位置，所以我们将使用从
one is a spawn position so we're going to use our projectile shoot from

137
00:10:03,600 --> 00:10:07,990
位置并作为目标，让我们向鼠标射击吧，让我们
position and as the target let's shoot towards the mouse all right so let's

138
00:10:08,190 --> 00:10:12,370
测试好吧，所以我们在这里，然后单击其中的任何一个，其中有箭头， 
test okay so here we are and click any of there you go there's the arrow and

139
00:10:12,570 --> 00:10:16,779
正确地进行了操作，因此，例如，单击此处并说达到了
correctly goes so let's say click in here and it says it reaches yep it gets

140
00:10:16,980 --> 00:10:21,399
罢工棒极了，现在我们需要固定速度和旋转
the strike all right awesome now we need to fix the speed as well as the rotation

141
00:10:21,600 --> 00:10:26,649
因此，为了提高速度，我们只需增加速度和旋转速度
so let's do that for the speed let's just increase it and for the rotation

142
00:10:26,850 --> 00:10:36,370
让我们正确地注油，所以我们使用的是
let's get the oiler angles all right so there it is for the angles we're using a

143
00:10:36,570 --> 00:10:41,109
函数将向量三转换为欧拉角，这是
function to transform a vector three into a Euler angle and here is the

144
00:10:41,309 --> 00:10:44,139
功能，以防您想自己动手，我们只需设置欧拉
function in case you want to make yourself and we just set the Euler

145
00:10:44,339 --> 00:10:49,149
角度，因为我们在2D模式下工作，所以我们只在DZ上设置好，让我们看看好
angles and since we were working in 2d we only set on D Z okay let's see okay

146
00:10:49,350 --> 00:10:53,828
并点击他们的任何一个哟，现在我们已经从
and click any of their yo now we have errors correctly being fired from the

147
00:10:54,028 --> 00:10:58,389
朝目标鼠标位置倾斜
tower and towards the target mouse position all right awesome

148
00:10:58,589 --> 00:11:02,198
好了，现在我们有了基本的塔式功能，让它正常工作
okay so we have our basic tower functionality now let's make it work

149
00:11:02,399 --> 00:11:06,309
如果我已经设置了一个简单的脚本以生成一些脚本，现在就与敌人
with enemies now if I'm already set up a simple script in order to spawn some

150
00:11:06,509 --> 00:11:11,109
敌人，因此只需按一个简单的键，敌人就会开始
enemies so by pressing a simple key there you go the enemies start being

151
00:11:11,309 --> 00:11:15,279
生成并开始显示，因此脚本在此游戏对象上
spawn and they start showing up so the script is on this game object here it is

152
00:11:15,480 --> 00:11:19,328
这是脚本，非常简单，我们要做的就是首先制作
here is the script it's very simple all we're doing is first of all making

153
00:11:19,528 --> 00:11:21,699
网格，所以我们有验证它的功能
the grid so we have the function to validate it

154
00:11:21,899 --> 00:11:25,569
这只是为了以后我们可以将塔架卡入位置，然后
this is just so we can later snap our towers into position and then we just

155
00:11:25,769 --> 00:11:29,859
在不同的难度下有三种不同类型的波浪
have three different types of waves in different difficulties and on each wave

156
00:11:30,059 --> 00:11:34,029
我们只是发现了一大堆敌人，就逻辑而言，他们所做的一切
we just find a whole bunch of enemies and in terms of logic all they're doing

157
00:11:34,230 --> 00:11:38,740
是盲目地跟随一个简单的Waypoint，所以现在非常简单
is blindly following a simple Waypoint so there it is very simple now the

158
00:11:38,940 --> 00:11:42,279
通过此功能可以创建敌人，这是我们正在做的漂亮
enemies are created through this function here it is we're doing pretty

159
00:11:42,480 --> 00:11:44,769
我们在弹箭上所做的几乎相同的事情
much the same thing that we're doing on the projectile arrow

160
00:11:44,970 --> 00:11:50,019
因此，我们对敌人的预制件进行了感应，然后我们进行了基本设置，现在
so we insensate the enemy prefab and then we do a basic set up and now as the

161
00:11:50,220 --> 00:11:54,339
醒来时会在此处创建敌人，并将它们添加到某个敌人列表中，因此
enemies are created here on awake they are added onto a certain enemy list so

162
00:11:54,539 --> 00:11:59,529
这是一个静态列表，其中包含所有在世敌人以及
this is a static list which contains all of the alive enemies and with that over

163
00:11:59,730 --> 00:12:04,179
在这里，我有一个简单的功能来取得最接近的敌人，它占据一个位置并
here I have a simple function to get the closest enemy it takes a position and a

164
00:12:04,379 --> 00:12:08,709
最大范围，并简单地循环遍历所有敌人，而忽略其中的任何一个
max range and simply cycles through all the enemies ignores if any of them are

165
00:12:08,909 --> 00:12:13,179
仍然死了并寻找范围内最近的敌人，所以此功能是
still dead and looks for the closest enemy within range so this function is

166
00:12:13,379 --> 00:12:16,870
为了寻找最接近我们塔的敌人，我们将使用什么
what we're going to use in order to look for the closest enemy to our tower so

167
00:12:17,070 --> 00:12:21,309
让我们进入我们的塔脚本，在这里让我们停止手动触发我们的
let's go into our tower script and here let's stop manually firing our

168
00:12:21,509 --> 00:12:27,729
弹丸，在这里，让我们做一个函数来获取最近的敌人
projectiles and in here and let's make a function to get the closest enemy now

169
00:12:27,929 --> 00:12:31,929
我们将在敌人类别上使用该函数，以便为
we're going to use the function on the enemy class so get closest enemy for the

170
00:12:32,129 --> 00:12:36,699
通过此变换飞镖位置的位置，现在对于最大范围，让我们
position passing this transform dart position and now for the max range let's

171
00:12:36,899 --> 00:12:39,709
将其存储在字段中
store it in a field

172
00:12:40,129 --> 00:12:46,719
所以就这样，所以现在在我们的更新中，我们可以尝试获取最接近的
so just like this all right so now in our update we can try to get the closest

173
00:12:46,919 --> 00:12:53,799
敌人，如果不知道，那么我们有一个有效的敌人，所以在这里，我们
enemy and if it is not know then we have a valid enemy so it's in here that we

174
00:12:54,000 --> 00:12:59,259
想要生成我们的射箭，它将从我们的射击位置朝着
want to spawn our projectile arrow going to spawn from our shoot position towards

175
00:12:59,460 --> 00:13:05,620
敌人的位置没问题，所以我们要像这样
the enemy position all right so there it is however like this we're going to have

176
00:13:05,820 --> 00:13:10,329
一个问题，但让我们尝试一下，看看现在还可以，到目前为止，看来
an issue but let's try it out just to see okay here we are and so far it seems

177
00:13:10,529 --> 00:13:14,889
好，但让我们产生一些敌人，他们来了，现在让我们看看
good but let's spawn some enemies and here they come and now let's see what

178
00:13:15,090 --> 00:13:19,479
当它们进入范围并发生时，它们就会发生，这是疯狂的
happens as they get within range and yep they're going with that that is insane

179
00:13:19,679 --> 00:13:23,139
好吧，所以在这里我们基本上看到一个箭头被发射
all right so here we are seeing essentially one arrow being fired by

180
00:13:23,340 --> 00:13:27,129
每个朋友，所以很明显我们不想射那么多箭
every single friend so obviously we don't want to shoot that many arrows so

181
00:13:27,330 --> 00:13:30,698
让我们添加一个简单的拍摄计时器，让我们
let's add a simple shoot timer so let's make a

182
00:13:30,899 --> 00:13:41,709
相位射击计时器可以，所以我们有一个非常基本的射击计时器
phasic shoot timer all right so we have a very basic shoot timer coming down

183
00:13:41,909 --> 00:13:45,849
用我们的增量时间，如果我们低于零，那么我们测试是否有敌人
with our delta time and if we go under zero then we test if we have an enemy

184
00:13:46,049 --> 00:13:50,529
我们重置了拍摄定时器，所以这很简单，让我们看看可以，所以在这里
and we reset our shoot timer so there it is very simple let's see okay so here we

185
00:13:50,730 --> 00:13:54,878
现在让我们产生一些敌人，他们来了，因为它们在里面
are now let's spawn some enemies and here they come and as they are within

186
00:13:55,078 --> 00:13:58,779
射程，然后塔就开始射击更容易控制的
range and there you go the tower start shooting a more manageable amount of

187
00:13:58,980 --> 00:14:02,828
到目前为止，所有箭头都很好，所以现在没有损坏，但逻辑是
arrows all right so far so good so right now there's no damage but the logic is

188
00:14:03,028 --> 00:14:06,428
实际起作用，所以让我们现在伤害我们的敌人
actually working so let's damage our enemies now the

189
00:14:06,629 --> 00:14:10,988
敌人已经具有此伤害功能，它将造成伤害
enemies already have this damage function it takes in an int for a damage

190
00:14:11,188 --> 00:14:15,219
并产生一些粒子和一堆东西，并减少
amount and simply spawn some particles and a bunch of things and reduces on the

191
00:14:15,419 --> 00:14:20,918
健康系统，让我们使用此功能让我们回到弹丸中
health system so let's use this function let's go back in our projectile and in

192
00:14:21,119 --> 00:14:25,029
当弹丸到达目标位置时我们就在这里，所以它在这里
here we have when the projectile reaches the target position so it's in here that

193
00:14:25,230 --> 00:14:30,368
我们实际上需要称呼敌人伤害，这意味着我们需要获得一个
we need to essentially call enemy damage so that means we need to receive a

194
00:14:30,568 --> 00:14:34,719
参考我们的目标敌人，所以让我们在这里，而不是我们的创造
reference to our target enemy so let's go up here and our create instead of

195
00:14:34,919 --> 00:14:39,519
接收目标位置让我们接收敌人，我们绕过每个目标
receiving a target position let's receive a enemy and we pass around each

196
00:14:39,720 --> 00:14:47,649
设置，这里我们将目标位置定义为敌人位置，现在
setup and here we define the target position as the enemy position and now

197
00:14:47,850 --> 00:14:51,608
在这里我们实际上可以调用我们的损伤函数，所以在这里我们需要损伤
here we can actually call our damage function so here we need the damage

198
00:14:51,808 --> 00:14:57,969
因此，我们也可以收到赔偿，因此我们也可以得到赔偿金额
amount so let's also receive that all right so we also get the damage amount

199
00:14:58,169 --> 00:15:03,188
在这里，我们可以使用它，现在应该回到塔中了
and in here we use it all right that should do it now back in the tower all

200
00:15:03,389 --> 00:15:07,599
我们需要的是当我们产生怪胎时敌人参考的人
we need is the person the enemy reference when spawning our freak out

201
00:15:07,799 --> 00:15:11,709
现在我们还需要损坏量，所以让我们像使用
and now we also need the damage amount so let's the final like we did with the

202
00:15:11,909 --> 00:15:19,209
范围好吧，我们有一个伤害值，这里只是为了好玩，让我们将其随机分配
range okay we have a damage amount and here just for fun let's randomize it a

203
00:15:19,409 --> 00:15:24,548
有点应该做的让我们在这里测试好吧，让我们
bit all right that should do it let's test okay here we are and let's

204
00:15:24,749 --> 00:15:29,198
产生一些敌人，然后它们开始出现，随着进近，他们
spawn some enemies and here they start to come and as the approach yep they

205
00:15:29,399 --> 00:15:32,678
开始被枪杀，然后就像他们都被枪杀一样
start getting shot and there you go just like that they're all being shot and

206
00:15:32,879 --> 00:15:35,469
其中一些实际上还活着，但是是的，我们去了
some of them are actually living but yep there we go

207
00:15:35,669 --> 00:15:39,758
我们的箭头很棒，所以在这里，我们有塔， 
we have the arrows working awesome okay so here we have our Tower and it's

208
00:15:39,958 --> 00:15:43,609
正确射击附近的敌人，现在让我们做一个简单的迷你
correctly shooting at nearby enemies now let's make a simple mini

209
00:15:43,809 --> 00:15:48,078
很好的系统，所以在塔中，我们已经为
great system alright so here in the tower we already defined a field for the

210
00:15:48,278 --> 00:15:51,828
范围和伤害的数量，所以它很容易操作
range and on for the damage amount so it's going to be very easy to manipulate

211
00:15:52,028 --> 00:15:59,929
这些让我们做一些功能来做到这一点，所以在这里让我们做
these let's make some functions to do that all right so here let's do very

212
00:16:00,129 --> 00:16:04,789
简单函数只是修改我们的简单字段，然后再次使用
simple functions just modifying our simple fields which again are then using

213
00:16:04,990 --> 00:16:08,929
此功能，然后在此功能中，现在进入编辑器
this function and up here in this function now let's go into the editor to

214
00:16:09,129 --> 00:16:14,389
让我们的升级看起来不错，所以在这里我们创建一个空的游戏对象， 
make our upgrade visual okay so in here let's create an empty game object let's

215
00:16:14,589 --> 00:16:18,559
公开称为升级，所以这是我们要放置的对象
call this the upgrade openly so this is the object that we're going to place on

216
00:16:18,759 --> 00:16:22,669
为了升级塔的顶部，在里面添加一个精灵
top of the tower in order to upgrade and inside let's add a sprite with this

217
00:16:22,870 --> 00:16:32,700
现在在此部分中，它做了两个按钮的升级和一个按钮的关闭
section here now it's made two buttons to upgrade and one button to close all

218
00:16:41,519 --> 00:16:46,339
正确，所以现在这里是按钮我正在使用按钮废料的地方
right so there it is now here for the buttons I'm using the button scrap

219
00:16:46,539 --> 00:16:50,449
脚本也是代码模拟实用程序的一部分，它只是一个简单的精灵
script which is also part of the code mock utilities it's just a simple sprite

220
00:16:50,649 --> 00:16:54,769
基本按钮以便捕获鼠标交互，因为这是一个精灵
base button in order to capture Mouse interactions since this is a sprite it's

221
00:16:54,970 --> 00:16:59,209
基于闯入，所以我们需要一个简单的对撞机，所以在这里
based on doing break-ins so we require a simple Collider alright so here we have

222
00:16:59,409 --> 00:17:02,659
我们的升级叠加层游戏对象实际上，我们将其放置在
our upgrade overlay game object we're essentially going to place this right on

223
00:17:02,860 --> 00:17:07,210
为了升级塔的顶部，现在让我们为此编写一个脚本
top of a tower in order to upgrade that tower now let's make a script for this

224
00:17:07,410 --> 00:17:13,579
所以一个新的脚本，只要附加好就可以了，现在先打开它，让我们做一些非常
so a new script and just attach it okay now open it first let's make some very

225
00:17:13,779 --> 00:17:20,388
基本的显示和隐藏功能，因此只需将游戏对象设置为true或false 
basic show and hide functions so just set the game object to true or false

226
00:17:20,588 --> 00:17:22,819
好吧，现在在节目中，我们也收到一个
okay and now on the show let's also receive a

227
00:17:23,019 --> 00:17:26,779
我们将要显示的塔，将其设置为true时，我们还将
tower that we're going to show on and when we set it to true we also set the

228
00:17:26,980 --> 00:17:31,399
将怀疑位置转换为塔式转换位置，使其位于顶部
transform doubt position to the tower transform position so it's right on top

229
00:17:31,599 --> 00:17:35,029
现在，为了使此脚本易于使用，让我们
of the tower and now in order to make this script easy to use and let's make a

230
00:17:35,230 --> 00:17:38,370
静态显示功能
static show function

231
00:17:43,130 --> 00:17:47,349
好的，所以这里是一个静态实例，将其设置为清醒状态， 
all right so here it is we just have a static instance we set it on awake and

232
00:17:47,549 --> 00:17:52,599
我们有我们的show static函数，可以在调用实例时使用-哦，这只是
we have our show static function which in calls the instance - oh this is just

233
00:17:52,799 --> 00:17:56,500
所以我们有一个非常容易使用的函数，现在我们需要做的是调用
so we have this function that is very easy to use now we need to do is call

234
00:17:56,700 --> 00:18:00,279
当我们将鼠标移到塔上时，返回编辑器
this when we pass the mouse over the tower so back in the editor

235
00:18:00,480 --> 00:18:05,740
让我们与塔楼不同，然后在上面套上一个对撞机
let's unlike the tower and apply a box Collider onto it all right there's the

236
00:18:05,940 --> 00:18:08,619
对撞机，现在我们可以进入塔脚本
collider and now we can go into the tower script

237
00:18:08,819 --> 00:18:15,129
在这里我们可以实现我们自己的鼠标回车功能
and in here we can implement our own mouse enter function this gets triggered

238
00:18:15,329 --> 00:18:19,480
当鼠标经过与该对象相连的对撞机时，在这里让我们
when the mouse passes over the collider attached to this object so in here let's

239
00:18:19,680 --> 00:18:23,859
只需进入升级覆盖图即可调用我们的show函数并通过
just go into the upgrade overlay in order to call our show function and pass

240
00:18:24,059 --> 00:18:27,730
在我们的塔中好吧，就像这样，它应该可以正常工作
in our tower alright so just like this it should be working

241
00:18:27,930 --> 00:18:31,930
让我们测试一下，在这里，因为我将鼠标移到了其中的任何一个上， 
let's test okay here we are as I passed the mouse over any of there you go the

242
00:18:32,130 --> 00:18:36,069
叠加层显示的很好，现在让我们实现
overlay shows up alright good now let's just implement the functions for the

243
00:18:36,269 --> 00:18:38,569
纽扣
buttons

244
00:18:50,069 --> 00:18:54,639
好的，所以就在这里，但是醒着之后，我们只需获取按钮精灵组件
all right so here it is but awake we simply get the button sprite component

245
00:18:54,839 --> 00:18:58,059
并且我们将绞痛功能设置为这些连接功能，因此隐藏原因
and we set the colic function to these connect functions so in the hide reason

246
00:18:58,259 --> 00:19:01,930
在升级范围的后面，您只需调用函数即可升级我们的塔
behind on the upgrade range you simply call the functions to upgrade our tower

247
00:19:02,130 --> 00:19:05,919
然后我们还有另一个功能或可以更新视觉效果，所以
and then we just have another function or to update the visuals all right so

248
00:19:06,119 --> 00:19:10,149
让我们测试一下，所以如果您在并且有塔，现在将鼠标放在
let's test okay so if you're and there's the tower and now place the mouse over

249
00:19:10,349 --> 00:19:14,049
是的，你现在去吧，我们可以看到很好的房屋范围，我们可以
yep there you go now we can see the home range all right very nice and we can

250
00:19:14,250 --> 00:19:17,680
也可以增加范围，因此通过单击按钮，您可以找到范围
also increase the range so by clicking the button and there you go the range

251
00:19:17,880 --> 00:19:22,149
确实会增加，这也会增加伤害，所以如果我产生一些
actually does increase and this one also increases damage so if I spawned some

252
00:19:22,349 --> 00:19:26,289
敌人现在，我们看着他们，在这里，他们来了
enemies right now and we look at them and here they come and as soon as they

253
00:19:26,490 --> 00:19:29,289
进入范围内，然后你就开始射击，他们需要大量的
get within range yep there you go they start getting shot and they take tons of

254
00:19:29,490 --> 00:19:33,879
伤害让我们继续增加，然后你会遭受更多的伤害
damage let's keep increasing and there you go a lot more damage and a much

255
00:19:34,079 --> 00:19:37,210
范围更大，好吧，就像这样，我们有了
bigger range all right so just like this we have our

256
00:19:37,410 --> 00:19:41,619
现在，铁塔完全可以正常运行，并且拥有自己的升级覆盖层
tower fully functioning with its own upgrade overlay awesome now finally

257
00:19:41,819 --> 00:19:45,490
现在让我们制作基本的塔式生成器，以便生成我们的塔
let's just make our basic tower spawner now in order to spawn our tower it's

258
00:19:45,690 --> 00:19:49,329
将会非常简单，我们已经有了塔，我们需要新的是转换
going to be very simple we already have our tower all we need new is convert

259
00:19:49,529 --> 00:19:53,230
将其放入预制件中，然后生成该预制件，以便使其成为预制件
this into a prefab and then spawn that prefab so to make it a prefab just drag

260
00:19:53,430 --> 00:19:56,950
将其放到prefabs文件夹中，然后转到脚本
it on to the prefabs folder yep there you go and now it's going to the script

261
00:19:57,150 --> 00:20:01,210
我们用来产生敌人的武器，所以在这里我们可以轻松地添加一个
that we're using to spawn our enemy so this one and in here we can easily add a

262
00:20:01,410 --> 00:20:06,879
生成Tower功能，所以首先让我们测试一下输入，将鼠标按钮按下
spawn Tower function so first let's test for the input get mouse button down

263
00:20:07,079 --> 00:20:10,639
鼠标右键是什么意思
what's this for the right mouse button

264
00:20:11,750 --> 00:20:15,920
我们产生了一个塔
and we spawn a tower

265
00:20:21,480 --> 00:20:25,729
好吧，就是这样，所以我们做的差不多
all right so there it is just like that so we're doing pretty much the same

266
00:20:25,929 --> 00:20:28,848
我们一直在做的事情，所以我们在San Shi a ting获取参考
thing that we're always doing so we're in San Shi a ting grabbing the reference

267
00:20:29,048 --> 00:20:32,869
通过游戏资产并在鼠标位置上做出响应
by the game assets and responding it on the mouse position which is being

268
00:20:33,069 --> 00:20:37,639
使用我们的握把验证过，这样非常简单，应该可以正常工作
validated using our grip okay so that's it very simple and it should be working

269
00:20:37,839 --> 00:20:42,019
让我们测试一下，好了，现在就到了，我们没有塔了，现在放鼠标
let's test okay so here we are and we have no towers now let's place the mouse

270
00:20:42,220 --> 00:20:45,769
在这里，右键单击其中的任何一个，我们就可以正确生成塔了
in here and right click any of there we go we have our tower correctly spawned

271
00:20:45,970 --> 00:20:51,229
如果我们产生一些敌人，您就不会看到该塔正在完全运作，所以这里
and if we spawn some enemies you don't see that tower is fully working so here

272
00:20:51,429 --> 00:20:54,919
他们来了，如果他们进入了范围之内，那么塔就开始射击
they come and as they get within range if there you go the tower starts firing

273
00:20:55,119 --> 00:20:57,858
然后你就可以打倒敌人了
and there you go taking down enemies all right great

274
00:20:58,058 --> 00:21:01,938
再一次，这是与我们漂亮的网格一起工作的，所以您去的很完美
and again this is working with our nice grid so there you go it's perfectly

275
00:21:02,138 --> 00:21:05,779
现在，我们在这里放置另一个塔，然后右键单击
located in there now let's place another tower right here so right-click and if

276
00:21:05,980 --> 00:21:08,889
现在你去那里，我们有两个塔，它们分别是
there you go now we have these two towers and they are individually

277
00:21:09,089 --> 00:21:15,619
可升级，所以让我们找到更多的敌人，我们隐藏了小发明
upgradeable so let's find some more enemies and we hide our gizmos and there

278
00:21:15,819 --> 00:21:19,338
你现在就去拿走那和我在我里面的东西，等等
you go now taking out that and me that in me and so on any of their going with

279
00:21:19,538 --> 00:21:23,629
很好，所以现在只有两座塔足以应付这些敌人
that very nice so now just two towers are enough to handle these enemies we

280
00:21:23,829 --> 00:21:28,490
可以产生更多敌人，然后我们可以尝试升级塔
can spawn a whole bunch more enemies and then we can try upgrading the tower so

281
00:21:28,690 --> 00:21:31,938
让我们增加这一范围，而这也增加伤害
let's increase the range on this one and this one as well increase damage by

282
00:21:32,138 --> 00:21:36,049
相当多，这些太多了，但是是的，你去了这里
quite a bit and these are way too many but yep there you go over here we have

283
00:21:36,250 --> 00:21:40,698
我们的系统正常工作，因此在这里我们有一个非常不错的简单塔防AI 
our system working so here we have a very nice simple tower defense AI

284
00:21:40,898 --> 00:21:45,049
工作，因此我们可以通过右键单击来放置塔，然后您就可以
working so we can place towers just by right-clicking and there you go we can

285
00:21:45,250 --> 00:21:49,159
放置一堆塔楼放置塔楼时不要在里面寻找敌人
place a whole bunch of towers the towers get placed don't look for enemies within

286
00:21:49,359 --> 00:21:53,178
范围，如果有的话，我们只是攻击他们，塔楼也有
range and if there are some then we simply attack them the towers also have

287
00:21:53,378 --> 00:21:57,409
一个非常好的迷你升级系统，以增加范围以及
a very nice mini upgrade system in order to increase the range as well as the

288
00:21:57,609 --> 00:22:03,078
再次造成伤害，这是一个非常简单的塔防AI迷你游戏，谢谢
damage so again here it is a very simple tower defense ai mini game thank you to

289
00:22:03,278 --> 00:22:06,588
添加混音，也感谢您为这些出色的支持者制作的视频
add mix and also thank you to these awesome supporters for making this video

290
00:22:06,788 --> 00:22:11,448
可能去patreon.com/scishow t猴子代码以获得一些津贴和帮助
possible go to patreon.com/scishow t code monkey to get some perks and help

291
00:22:11,648 --> 00:22:14,688
只要您可以下载项目，就可以让所有人免费观看视频
keep the videos free for everyone as long as you can download the project

292
00:22:14,888 --> 00:22:18,198
公用代码中的工具文件猴子comm，因此开始进入
files in utilities from unity code monkey comm so start to the channel for

293
00:22:18,398 --> 00:22:21,740
更多的统一教程发布任何人都有它的帐户，下一个见
more unity tutorials post any person have it accounts and I'll see you next

294
00:22:21,940 --> 00:22:28,569
时间 
time [Music]


