1
00:00:03,710 --> 00:00:08,029
Now that we've got our player moving around the screen, it's time to start thinking about adding some

2
00:00:08,029 --> 00:00:08,800
enemies.

3
00:00:08,810 --> 00:00:13,160
And the way we're going to do this is going to involve a few different scripts talking to each other.

4
00:00:13,160 --> 00:00:17,750
So before we just go ahead and start coding things up, let's actually talk about how our enemies are

5
00:00:17,750 --> 00:00:20,120
going to work and what script we're going to need.

6
00:00:20,240 --> 00:00:24,320
Now, our enemies themselves are going to have a few different requirements.

7
00:00:24,320 --> 00:00:29,030
They're going to need to be able to cause damage to the player, either through collisions or through

8
00:00:29,030 --> 00:00:30,020
shooting at them.

9
00:00:30,020 --> 00:00:34,580
And rather than just having a single type of enemy, we're going to have different enemies with different

10
00:00:34,580 --> 00:00:35,840
attack behaviors.

11
00:00:35,840 --> 00:00:40,400
So they may shoot faster or slower or they may even have different projectile types.

12
00:00:40,400 --> 00:00:45,080
The enemies are going to affect the player's score later on, so if they get destroyed, they're going

13
00:00:45,080 --> 00:00:46,670
to increase the player's score.

14
00:00:46,670 --> 00:00:52,070
And we may also add a mechanic in there to lower the player's score if they manage to escape unharmed

15
00:00:52,070 --> 00:00:55,310
and the enemies are going to spawn into our game in waves.

16
00:00:55,460 --> 00:00:59,870
Now, if you're not familiar with that term, let's have a look at what waves actually are.

17
00:00:59,900 --> 00:01:03,950
Well, you can kind of think of them as self-contained moments of gameplay.

18
00:01:03,950 --> 00:01:08,840
So we can see an example of a wave here where we've got two enemies following a set path.

19
00:01:08,840 --> 00:01:14,180
So for our game, a wave is going to spawn a certain number of enemies over a given amount of time,

20
00:01:14,180 --> 00:01:19,100
and all enemies in that wave are going to follow a set path once that wave has finished.

21
00:01:19,100 --> 00:01:22,940
So either all of the enemies have been destroyed or reached the end of the path.

22
00:01:22,940 --> 00:01:26,920
We can then go ahead and generate the next gameplay moment or the next wave.

23
00:01:26,930 --> 00:01:32,150
This could be the same as the first, or it could involve different enemies following a different path.

24
00:01:32,150 --> 00:01:35,150
So that's the basic mechanics of what we want to happen.

25
00:01:35,150 --> 00:01:39,410
But to get all this working, we're going to need a few different scripts working in unison.

26
00:01:39,410 --> 00:01:43,490
So next up, let's look at the scripts and their various responsibilities.

27
00:01:43,490 --> 00:01:48,890
First of all, we're going to need a wave config file and this is going to be a script object.

28
00:01:48,890 --> 00:01:53,450
This script will object is going to tell the wave which enemies need to be spawned.

29
00:01:53,480 --> 00:01:56,630
It's going to tell them the path that needs to be followed.

30
00:01:56,630 --> 00:02:01,580
And it's also going to control things like the time between enemies arriving within that wave.

31
00:02:01,580 --> 00:02:06,140
So they don't all just arrive at the same time and it's going to control the enemy movement.

32
00:02:06,140 --> 00:02:11,270
SPEED So we may have certain waves where the enemies are quicker or slower because all of this is contained

33
00:02:11,270 --> 00:02:12,590
within script or objects.

34
00:02:12,590 --> 00:02:17,660
We're going to need some scripts to actually use this data and the first is going to be the enemy spawn.

35
00:02:17,930 --> 00:02:23,120
This, unsurprisingly, is going to spawn the enemies into our game at the start of the path, but it's

36
00:02:23,120 --> 00:02:25,400
also going to dictate the order of the waves.

37
00:02:25,400 --> 00:02:30,200
So if we have multiple waves in our game, which we certainly will do, it's going to tell us which

38
00:02:30,200 --> 00:02:34,340
one to generate first and in which one to generate after that one has finished.

39
00:02:34,340 --> 00:02:38,180
And finally, it's going to be in control of the time between those waves.

40
00:02:38,420 --> 00:02:43,400
So that takes care of bringing the enemies into the world, but we also need to get them moving.

41
00:02:43,400 --> 00:02:44,990
This will be the job of an enemy.

42
00:02:44,990 --> 00:02:50,840
Pathfinding script and all this is going to do is tell the enemy to move to the next waypoint in a path.

43
00:02:51,170 --> 00:02:55,970
Now, we're obviously not going to get all of this done in one lecture, so let's just start by getting

44
00:02:55,970 --> 00:02:58,580
our enemy moving along a fixed path.

45
00:02:58,940 --> 00:03:04,550
And to create a path is actually incredibly simple because we're going to be using a waypoint system.

46
00:03:04,550 --> 00:03:10,670
So in our hierarchy, let's right click and create an empty game object and then reset the transform.

47
00:03:11,000 --> 00:03:17,750
And we'll call this object path zero because we very well may have more paths in the future.

48
00:03:17,810 --> 00:03:23,930
Then within this empty path object, let's right click again and create another empty object.

49
00:03:23,930 --> 00:03:26,300
And we're going to call this waypoint zero.

50
00:03:26,870 --> 00:03:33,050
And this time I've placed the zero in parentheses so that when we duplicate this object, it will automatically

51
00:03:33,050 --> 00:03:34,550
index these for us.

52
00:03:34,760 --> 00:03:39,560
Before we go ahead and do any duplication, though, the general idea is that we're going to have multiple

53
00:03:39,560 --> 00:03:45,080
waypoints in this path and then the enemy is going to move from one to the next.

54
00:03:45,320 --> 00:03:47,240
Because these are empty objects.

55
00:03:47,240 --> 00:03:51,470
We can't see them in our scene, which makes them very difficult to work with.

56
00:03:51,470 --> 00:03:57,500
So to fix this, rather than adding a sprite, which we would see in both our scene and our game view,

57
00:03:57,500 --> 00:04:02,630
what we're going to do is head up to the top of our inspector here where we've got this little cube

58
00:04:02,630 --> 00:04:03,290
icon.

59
00:04:03,290 --> 00:04:06,770
If we click on that, we then have some icons to choose from.

60
00:04:06,770 --> 00:04:12,680
I'm going to pick the yellow diamond so you can see what's going on here, and we can see that in our

61
00:04:12,680 --> 00:04:13,400
same view.

62
00:04:13,400 --> 00:04:15,440
We've now got this little yellow diamond.

63
00:04:15,440 --> 00:04:17,899
If we zoom in, we can see it a bit better.

64
00:04:17,899 --> 00:04:22,160
But if we look over on the game view, that little icon doesn't appear anywhere.

65
00:04:22,160 --> 00:04:26,600
So we don't like to have to worry about switching these things off in the game view.

66
00:04:26,960 --> 00:04:33,350
Now if we zoom back out so we can see our full game view again here in our scene, let's go ahead now

67
00:04:33,350 --> 00:04:35,510
and duplicate some of these waypoints.

68
00:04:35,510 --> 00:04:39,500
So just control the and I'm going to create five in total.

69
00:04:40,040 --> 00:04:43,340
All we have to do now is arrange them into a path that we want.

70
00:04:43,670 --> 00:04:48,860
I'm going to go with a similar path to what we saw in the slides, but feel free to create whatever

71
00:04:48,860 --> 00:04:50,360
kind of path you like.

72
00:04:53,620 --> 00:05:00,580
So with my path set up, I've got a nice sweeping curve here and notice that the start and end points

73
00:05:00,580 --> 00:05:02,490
are off of the screen.

74
00:05:02,500 --> 00:05:08,170
This is so the enemies can both appear and disappear off camera so that we don't get any abrupt changes

75
00:05:08,170 --> 00:05:10,780
where the enemy just pops in or out of existence.

76
00:05:11,080 --> 00:05:14,020
With our path set up, let's go ahead and prefab it.

77
00:05:14,020 --> 00:05:18,520
And rather than putting it into our prefabs folder because we're going to have a few of these, let's

78
00:05:18,520 --> 00:05:23,710
create a new folder specifically for our waves and our paths.

79
00:05:23,710 --> 00:05:28,510
So this will contain all of our paths and all of our wave config data later on.

80
00:05:28,660 --> 00:05:31,600
Let's go ahead and drag our path into that folder.

81
00:05:31,720 --> 00:05:34,270
And now let's look at how to get our enemy moving.

82
00:05:34,480 --> 00:05:37,600
We set the data for things like the path and the enemy movement.

83
00:05:37,600 --> 00:05:40,840
Speed would all be contained within a script object.

84
00:05:40,840 --> 00:05:46,930
So let's go ahead and go into our scripts folder and create this script object by right clicking, going

85
00:05:46,930 --> 00:05:51,700
to create and then C-sharp script and we're going to call this the wave config.

86
00:05:51,730 --> 00:05:57,340
So now we're not going to write this entire script or object in this lecture, but we are going to get

87
00:05:57,340 --> 00:06:00,280
enough information in there so that we can move our enemies.

88
00:06:00,580 --> 00:06:02,110
This is a script to objects.

89
00:06:02,110 --> 00:06:08,260
We don't need the start and update method, and we also need to change our mono behavior to be a script

90
00:06:08,470 --> 00:06:12,460
object so that we can create new wave configs in unity.

91
00:06:12,460 --> 00:06:16,480
Let's go ahead and add the Create Asset menu attribute to the top.

92
00:06:16,480 --> 00:06:25,690
I'm going to set the menu name to be wave config and I'm going to give it a default file name of new

93
00:06:25,690 --> 00:06:26,710
wave config.

94
00:06:28,950 --> 00:06:34,200
Now let's get our economy moving along the path this script will object is going to need two variables.

95
00:06:34,200 --> 00:06:40,350
These are all going to be serialized, filled, and the first is going to be of type transform and it's

96
00:06:40,350 --> 00:06:43,080
going to be the path prefab that we've just made.

97
00:06:43,080 --> 00:06:45,690
So this will contain all of the waypoints that we need.

98
00:06:45,870 --> 00:06:49,590
Next, we're going to have another serialized field of type float.

99
00:06:50,130 --> 00:06:53,280
And this is going to be the move speed for our enemy.

100
00:06:53,880 --> 00:06:57,720
And while we're here, let's give it a default value of around five.

101
00:06:58,360 --> 00:07:03,550
Now because these variables are private, being there in a script to object, we're also going to need

102
00:07:03,550 --> 00:07:08,830
some getters to access them from outside of this script and for the move speed, that's going to be

103
00:07:08,830 --> 00:07:09,910
fairly straightforward.

104
00:07:09,910 --> 00:07:19,180
We're just going to have a public float called Get Move Speed, and this is going to return the move

105
00:07:19,180 --> 00:07:19,720
speed.

106
00:07:20,260 --> 00:07:25,150
But for the waypoints in the path, things are going to get a little bit more complicated because we

107
00:07:25,150 --> 00:07:28,120
don't really need the container that those waypoints are in.

108
00:07:28,120 --> 00:07:30,190
We want the waypoints themselves.

109
00:07:30,220 --> 00:07:35,350
We're also going to want a quick way of grabbing the very first waypoint in the path as well.

110
00:07:35,350 --> 00:07:38,440
So let's start off with that one because it's a bit simpler.

111
00:07:38,440 --> 00:07:44,110
So our method for this is going to be public and is going to return a transform and we're going to call

112
00:07:44,110 --> 00:07:46,420
this get starting waypoint.

113
00:07:47,260 --> 00:07:53,710
And the starting waypoint in our path is going to be the very first child of this container so we can

114
00:07:53,710 --> 00:08:02,410
return the path prefab dot get child at index zero so that will return the very first child for us.

115
00:08:02,680 --> 00:08:06,820
Next, we want a way of returning a list of all of the waypoints.

116
00:08:06,820 --> 00:08:08,800
So let's create a public method.

117
00:08:08,800 --> 00:08:16,600
And this time we're going to return a list of transforms and we'll call this one, get waypoints for

118
00:08:16,600 --> 00:08:16,810
this.

119
00:08:16,810 --> 00:08:20,470
We're going to need to loop through all of the children within our path.

120
00:08:20,470 --> 00:08:22,330
Prefab parent object.

121
00:08:22,330 --> 00:08:27,760
And you may be thinking to yourself that a for loop is going to be ideal for this task and you're certainly

122
00:08:27,760 --> 00:08:28,570
not wrong.

123
00:08:28,570 --> 00:08:34,780
However, what we're going to do instead of using a for loop is we're going to use a for each loop instead,

124
00:08:34,780 --> 00:08:38,830
which works very similarly, but the syntax is slightly different.

125
00:08:38,830 --> 00:08:41,289
So how does a for each loop work?

126
00:08:41,289 --> 00:08:44,800
Well, first of all, let's remind ourselves of how a for loop works.

127
00:08:44,800 --> 00:08:51,280
And in the context of what we're doing, we may have a declaration, something like four int equals

128
00:08:51,280 --> 00:08:51,820
zero.

129
00:08:51,820 --> 00:08:55,990
So initializing our iterator at the first index of our path.

130
00:08:55,990 --> 00:09:02,410
And then while I is less than the child count of the parent prefab, we want to go ahead and grab the

131
00:09:02,410 --> 00:09:05,290
child and then increment our index by one.

132
00:09:05,470 --> 00:09:09,520
And as I said, there's absolutely nothing wrong with doing it this way.

133
00:09:09,550 --> 00:09:16,030
However, we could instead opt to use a for each loop which operates exactly the same but has a slightly

134
00:09:16,030 --> 00:09:17,230
cleaner syntax.

135
00:09:17,230 --> 00:09:24,190
So to do this exact same thing with a for each loop we could say for each child transform.

136
00:09:24,190 --> 00:09:30,340
So child is a variable in this case and it's of type transform in our parent.

137
00:09:30,340 --> 00:09:35,050
So in our case our path prefab, then we want to go ahead and do stuff.

138
00:09:35,380 --> 00:09:41,980
So let's head back over to our script and to return a list of all the waypoints in our path.

139
00:09:41,980 --> 00:09:48,460
Prefab First of all, we're going to have to create a new list of transforms and we'll call this waypoints.

140
00:09:49,700 --> 00:09:53,630
And then we'll initialize that to be a new list of transforms.

141
00:09:54,680 --> 00:09:59,320
Then we're going to go ahead and loop through all of the children of our path prefab.

142
00:09:59,330 --> 00:10:09,720
So using our for each syntax we're going to say for each transform child in path prefab.

143
00:10:09,740 --> 00:10:15,230
So we're going to take our path prefab and loop through all of the children and store them as transforms.

144
00:10:15,230 --> 00:10:21,890
And every time we do this, we're going to take our waypoints and add the child.

145
00:10:23,240 --> 00:10:26,030
Once we finished doing that, we've gone through all of the children.

146
00:10:26,030 --> 00:10:28,730
We can go ahead and return our list of waypoints.

147
00:10:28,970 --> 00:10:31,580
So let's save our script to object.

148
00:10:31,580 --> 00:10:34,010
And that should have everything we need for now.

149
00:10:34,190 --> 00:10:38,510
And to stop this lecture from getting too long, we're actually going to stop things there for now.

150
00:10:38,510 --> 00:10:41,650
And we're going to write the Pathfinding script in the next lecture.

151
00:10:41,660 --> 00:10:42,530
So well done.

152
00:10:42,530 --> 00:10:48,260
Forgetting our wave config script object started and I'll see you in the next lecture where we implement

153
00:10:48,260 --> 00:10:49,400
our enemy movement.

154
00:10:49,400 --> 00:10:50,660
So I'll see you there.

