1
00:00:03,160 --> 00:00:07,120
In this lecture, we're making our enemy move and we're making our enemy flip direction.

2
00:00:07,120 --> 00:00:10,300
When they fly and bump into the wall, they'll head the other direction.

3
00:00:10,300 --> 00:00:11,370
So let's get started.

4
00:00:11,380 --> 00:00:14,530
Let's kick off by creating a new script in our scripts folder.

5
00:00:14,530 --> 00:00:20,290
Right click, create a C-sharp script and we'll call this C sharp script.

6
00:00:20,290 --> 00:00:23,440
Enemy movement, open up enemy movement.

7
00:00:23,500 --> 00:00:27,520
Just get rid of the stuff we don't need the couple of comments that we're absolute legends by now,

8
00:00:27,520 --> 00:00:30,760
so we don't need to know those comments, just wasted space.

9
00:00:30,760 --> 00:00:36,520
What we do need to do is to access the rigid body, because a rigid body is how we move things around

10
00:00:36,520 --> 00:00:37,120
in the world.

11
00:00:37,120 --> 00:00:43,090
So as we usually do, we're going to create a reference for rigid body 2D, which will be type and we'll

12
00:00:43,090 --> 00:00:46,480
call it my rigid body semicolon.

13
00:00:46,480 --> 00:00:47,530
While we're here.

14
00:00:47,530 --> 00:00:55,330
I'd also like some way of us being able to have a variable to serialize the speed of the enemy.

15
00:00:55,330 --> 00:00:59,650
So I'm going to create a new float and I'll do a serialized field before that.

16
00:00:59,650 --> 00:01:02,320
Actually, that'd be wise, serialized, filled float.

17
00:01:02,350 --> 00:01:05,050
We'll call this move speed and I'll initialize that.

18
00:01:05,050 --> 00:01:10,030
And just straight up one F as a starting point, let's just get the enemy moving across the screen,

19
00:01:10,030 --> 00:01:12,640
just just going from left to right, doing a bit of a move.

20
00:01:12,640 --> 00:01:17,440
So an update we will access my rigid body dot velocity.

21
00:01:17,440 --> 00:01:22,300
We've done this before with the player and that's going to equal what a little micro challenge for you

22
00:01:22,300 --> 00:01:22,720
here.

23
00:01:22,900 --> 00:01:30,070
It's going to equal new vector two and in parentheses we want move speed because this is on the x y

24
00:01:30,070 --> 00:01:35,560
axis not move input, move speed x y excess comma zero because we don't want it to be doing any ups

25
00:01:35,560 --> 00:01:36,430
and downs.

26
00:01:36,820 --> 00:01:39,100
So we'll save that back over into unity.

27
00:01:39,100 --> 00:01:42,160
We better add that script to the enemy so it knows what's going on.

28
00:01:42,160 --> 00:01:46,960
Enemy and then just drag the enemy movement script over onto the enemy.

29
00:01:46,960 --> 00:01:51,010
There we go, click on play actually to make this a little bit more hilarious because I think I know

30
00:01:51,010 --> 00:01:51,550
what's going to happen.

31
00:01:51,550 --> 00:01:55,900
We'll put the enemy over on the left, but the player on the right, the enemy should be moving from

32
00:01:55,900 --> 00:01:57,220
left to right of screen.

33
00:01:57,220 --> 00:01:58,000
It's not.

34
00:01:58,000 --> 00:01:59,170
We've done something wrong.

35
00:01:59,170 --> 00:02:00,160
Oh, goodness me.

36
00:02:00,370 --> 00:02:01,180
My apologies.

37
00:02:01,180 --> 00:02:05,650
If you knew this and you were yelling at me, yelling at the screen, I could edit out my mistake,

38
00:02:05,650 --> 00:02:05,830
but.

39
00:02:05,830 --> 00:02:06,370
Oh, why not?

40
00:02:06,370 --> 00:02:08,380
We'll leave Rick as being a bit of a numpty.

41
00:02:08,380 --> 00:02:09,880
So what have we missed in here?

42
00:02:09,880 --> 00:02:11,770
We've missed getting the component.

43
00:02:11,770 --> 00:02:15,100
My rigid body equals get component.

44
00:02:15,100 --> 00:02:17,770
Rigid body to dx parentheses semicolon.

45
00:02:17,770 --> 00:02:18,250
There we go.

46
00:02:18,250 --> 00:02:19,540
That's a step that we missed.

47
00:02:19,540 --> 00:02:20,170
Thank you.

48
00:02:20,170 --> 00:02:22,510
And apologies if you knew that already.

49
00:02:22,510 --> 00:02:23,440
If you didn't spot it.

50
00:02:23,440 --> 00:02:25,000
Not to worry, I didn't spot it.

51
00:02:25,000 --> 00:02:27,130
Moving too quick, moving with too much velocity.

52
00:02:27,130 --> 00:02:30,040
So click on play now.

53
00:02:30,040 --> 00:02:33,190
We'll see if our hilarious thing happens with the enemies bouncing across the screen.

54
00:02:33,190 --> 00:02:36,070
Gets the player, hey player, let's go for a little bit of a ride.

55
00:02:36,070 --> 00:02:40,300
Our center machine camera's moving along with it and he's going to say, Let's just drop you off the

56
00:02:40,300 --> 00:02:40,660
cliff.

57
00:02:40,660 --> 00:02:41,290
Not a problem.

58
00:02:41,290 --> 00:02:44,590
I thought it was funny, but in hindsight it actually wasn't overly hilarious.

59
00:02:44,590 --> 00:02:49,600
But let's move to the next stage, which is to get our enemy running down there, hitting the wall and

60
00:02:49,600 --> 00:02:50,620
then turning around.

61
00:02:50,620 --> 00:02:51,700
That was a pretty slow journey.

62
00:02:51,700 --> 00:02:55,990
So we'll put the enemy really close for this and we want to make sure the enemy is touching right on

63
00:02:55,990 --> 00:02:56,710
top of the ground.

64
00:02:56,710 --> 00:02:58,120
Now, there's a little technique.

65
00:02:58,120 --> 00:03:01,600
I was going to give this to you as a challenge, but it's a bit sneaky because it's a tough one to figure

66
00:03:01,600 --> 00:03:01,840
out.

67
00:03:01,840 --> 00:03:03,460
Or at least it took me a while to figure out.

68
00:03:03,460 --> 00:03:05,710
I'm going to show you how we'll make the enemy turn.

69
00:03:05,710 --> 00:03:08,140
And then if you're ready to add the code yourself, you can.

70
00:03:08,140 --> 00:03:13,210
What we need to do on the enemy is add another component, yet another component which will be a box

71
00:03:13,210 --> 00:03:19,000
collider 2d a little bit the way the player had feet in addition to this, this capsule that's going

72
00:03:19,000 --> 00:03:22,750
to be the capsule is going to be for when it's player to enemy collision.

73
00:03:22,750 --> 00:03:28,270
But this box collider that we're about to add is going to be for should I turn around yet so box collider

74
00:03:28,270 --> 00:03:33,520
two DD We'll get in there and edit that a little bit using the edit collider What I want to do is make

75
00:03:33,520 --> 00:03:35,080
a reverse periscope.

76
00:03:35,080 --> 00:03:35,980
What does that even mean?

77
00:03:35,980 --> 00:03:39,520
Well, you know, in submarines, how they have a periscope that sticks up out of the ground and you

78
00:03:39,520 --> 00:03:41,200
can see something above the water.

79
00:03:41,200 --> 00:03:45,910
We're doing it the other way around, which is this is going to be underground underneath here.

80
00:03:46,030 --> 00:03:46,930
Put it back a little bit.

81
00:03:46,960 --> 00:03:50,410
We want to have the we want to have it fairly narrow.

82
00:03:50,440 --> 00:03:51,940
So what's going to happen here?

83
00:03:51,940 --> 00:03:52,720
Let me explain.

84
00:03:52,720 --> 00:03:54,970
Is that goober just w to movie?

85
00:03:54,970 --> 00:03:55,900
It's going to bounce along.

86
00:03:55,900 --> 00:03:56,980
Bounce, bounce, bounce, bounce.

87
00:03:56,980 --> 00:04:03,430
We're going to use on Collision Exit, which means currently there's collision on the ground here.

88
00:04:03,430 --> 00:04:05,080
There's collision on the box.

89
00:04:05,080 --> 00:04:07,420
There's going to be collision on the wall as well.

90
00:04:07,420 --> 00:04:12,160
And when we get there, don't, don't, don't at this point right here, he's exited all the collision.

91
00:04:12,340 --> 00:04:17,649
This collision box is no longer touching our collider that is related to the ground.

92
00:04:17,649 --> 00:04:21,250
At that point, we're going to say, hey, flip around and then he'll flip and go back the other way.

93
00:04:21,250 --> 00:04:24,370
And we know how we flip is on our X scale.

94
00:04:24,370 --> 00:04:25,480
We just hit negative.

95
00:04:25,480 --> 00:04:29,590
And then when this is no longer colliding, we'll flip again.

96
00:04:29,590 --> 00:04:30,880
So it's a little bit sneaky.

97
00:04:30,880 --> 00:04:32,470
We're using on trigger exit.

98
00:04:32,470 --> 00:04:37,210
So if that's enough for you to go on, if you want to jump in and work on the code for having it flip,

99
00:04:37,210 --> 00:04:38,440
then pause the video and do so.

100
00:04:38,440 --> 00:04:41,920
There'll be another challenge, a bit of a shorter challenge when I'm halfway through the code, but

101
00:04:41,920 --> 00:04:43,780
if you're a bit more of a pro, you're ready for that.

102
00:04:43,780 --> 00:04:45,010
Then go ahead and do that.

103
00:04:45,010 --> 00:04:45,340
Hopefully.

104
00:04:45,340 --> 00:04:48,490
My telescope, my periscope, my reverse periscope.

105
00:04:48,490 --> 00:04:50,160
Did I call it a telescope before?

106
00:04:50,200 --> 00:04:51,040
Probably did.

107
00:04:51,040 --> 00:04:53,140
It's one of those lectures where I'm getting a few things wrong.

108
00:04:53,140 --> 00:04:55,060
It's a reverse periscope, Rick.

109
00:04:55,060 --> 00:04:56,740
Now we'll jump back over into our code.

110
00:04:56,740 --> 00:04:58,030
Stop talking to ourselves.

111
00:04:58,030 --> 00:05:02,620
We need to say what's going to happen when we do our on trigger trigger exit.

112
00:05:02,730 --> 00:05:08,190
So similar to on trigger enter but it's void on trigger.

113
00:05:08,640 --> 00:05:10,710
Exit to DD if we've got one of those.

114
00:05:10,710 --> 00:05:11,460
There we go.

115
00:05:11,790 --> 00:05:14,310
It's going to add in a whole bunch of extra stuff I didn't ask for.

116
00:05:14,730 --> 00:05:16,140
Void on trigger exit to DD.

117
00:05:16,170 --> 00:05:16,690
Great.

118
00:05:16,710 --> 00:05:18,990
Now, the first thing I'll do in here is say move.

119
00:05:18,990 --> 00:05:21,810
Speed equals what?

120
00:05:21,810 --> 00:05:25,170
Move speed equals minus move speed.

121
00:05:25,170 --> 00:05:31,260
Which is another way of saying whenever you exit your your reverse periscope exits the thing instead

122
00:05:31,260 --> 00:05:33,990
of heading in a positive direction, head in a negative direction.

123
00:05:33,990 --> 00:05:38,760
If you're currently heading in a negative direction, i.e. your x value is is negative, meaning you're

124
00:05:38,760 --> 00:05:41,910
heading from right to left, then switch it by adding a minus.

125
00:05:41,910 --> 00:05:48,420
And just a reminder on that, if you have a plus and you combine that with a minus, you get what?

126
00:05:48,450 --> 00:05:49,370
You get a minus.

127
00:05:49,380 --> 00:05:53,040
So in other words, adding a minus will actually be subtracting.

128
00:05:53,040 --> 00:05:56,990
If you subtract a minus, that's the same as adding.

129
00:05:57,000 --> 00:06:01,500
So that's just a little maths reminder there jump into our maths for games course if you want a reminder

130
00:06:01,500 --> 00:06:03,690
on all these maths fundamentals as well.

131
00:06:03,690 --> 00:06:06,210
It's a great cause for a lot of fundamental concepts in there.

132
00:06:06,210 --> 00:06:08,160
So move speed equals minus, move speed.

133
00:06:08,160 --> 00:06:15,630
And then we're going to do one more thing in here, which is to flip enemy facing parentheses semicolon.

134
00:06:15,630 --> 00:06:20,010
This is a method we haven't created just yet, but I'm going to give that to you as a challenge to see

135
00:06:20,010 --> 00:06:24,660
if you can figure out how to flip the facing direction of the enemy, which is important to make sure

136
00:06:24,660 --> 00:06:28,320
our reverse periscope is the thing at the front that bumps into the wall.

137
00:06:28,320 --> 00:06:31,260
So finish the flip and code haha there's a joke for you.

138
00:06:31,260 --> 00:06:33,960
Finish our code to make the enemy flip directions.

139
00:06:33,960 --> 00:06:34,860
There's a hint.

140
00:06:34,860 --> 00:06:38,130
Look at the player movement script for some clues on how to do that.

141
00:06:38,130 --> 00:06:40,050
So that's all I'll give you for this pause video.

142
00:06:40,050 --> 00:06:40,860
Take on that challenge.

143
00:06:40,860 --> 00:06:41,970
So you back in when you done.

144
00:06:45,710 --> 00:06:46,040
Now.

145
00:06:46,040 --> 00:06:46,870
What was I talking about?

146
00:06:46,880 --> 00:06:48,170
Now play a movement code.

147
00:06:48,170 --> 00:06:53,300
Well, if we scroll around and find where we're flipping the player, it's even called Flip Sprite in

148
00:06:53,300 --> 00:06:53,420
here.

149
00:06:53,420 --> 00:06:58,100
You can see we've got this transformed local scale equals new vector to blah blah, blah and we're using

150
00:06:58,100 --> 00:06:59,690
math sign.

151
00:06:59,690 --> 00:07:00,710
That's one of our clues.

152
00:07:00,710 --> 00:07:02,540
I'm just going to copy this line in here.

153
00:07:02,570 --> 00:07:03,620
This is a good practice.

154
00:07:03,620 --> 00:07:07,430
I think in coding you want to have a look at what you've coded before or figured out before.

155
00:07:07,430 --> 00:07:13,970
So I will create our flip enemy facing method, put in our curly braces, and within here I'll paste

156
00:07:13,970 --> 00:07:15,080
the line that we had before.

157
00:07:15,080 --> 00:07:15,380
Okay.

158
00:07:15,380 --> 00:07:19,310
So we do want to transform the local scale and it does need to be a new vector.

159
00:07:19,310 --> 00:07:22,940
Two We've got our math f sign my rigid body.

160
00:07:22,940 --> 00:07:28,130
So this is going to say right here, it will say, what direction are you facing?

161
00:07:28,130 --> 00:07:31,880
Our code is a little bit different to the player code because the player code is reading inputs.

162
00:07:31,880 --> 00:07:35,180
So we know whether you're pushing a direction on the controller or on the keyboard.

163
00:07:35,180 --> 00:07:37,220
So we're using that to read what's going on.

164
00:07:37,220 --> 00:07:39,290
That's why that code is a little bit different here.

165
00:07:39,290 --> 00:07:44,450
It's simply a case of saying whatever you're doing at the moment, just flip it the other way around

166
00:07:44,450 --> 00:07:46,250
when this method gets called.

167
00:07:46,250 --> 00:07:51,500
So in other words, when you use the on trigger exit to DX, so what we're going to do in here is put

168
00:07:51,500 --> 00:07:53,990
a negative in front of a math sign.

169
00:07:53,990 --> 00:07:57,500
I need another set of parentheses to wrap around that.

170
00:07:57,500 --> 00:08:02,300
So basically we're saying whatever your current sign is, in other words, whatever your current scale

171
00:08:02,300 --> 00:08:07,520
is, just flip that when we tell you to do flipping and that's on the x axis, we don't need to do anything

172
00:08:07,520 --> 00:08:08,300
on the y axis.

173
00:08:08,300 --> 00:08:10,640
That's why y axis is one f.

174
00:08:10,670 --> 00:08:15,110
And just before I save and head back into unity, what have I missed in my method here?

175
00:08:15,770 --> 00:08:17,480
I'm going to pretend I did this deliberately.

176
00:08:17,480 --> 00:08:19,310
As a little bit of a micro challenge for you.

177
00:08:19,310 --> 00:08:22,040
There's something I've missed in here, which means it won't work at all.

178
00:08:22,130 --> 00:08:26,060
What I've missed is a return type, so avoid flip facing enemies.

179
00:08:26,060 --> 00:08:28,160
What we need in there and back over into unity.

180
00:08:28,160 --> 00:08:30,680
There's one last main thing we need to do in here.

181
00:08:30,680 --> 00:08:31,790
Hopefully you figure this out.

182
00:08:31,790 --> 00:08:36,200
When you were adding a code and actually getting your character to flip, we need to play around with

183
00:08:36,200 --> 00:08:37,789
our collision matrix.

184
00:08:37,789 --> 00:08:40,309
So currently the enemy is on the default layer.

185
00:08:40,309 --> 00:08:44,000
I'm going to add a new layer which we will call enemies.

186
00:08:44,330 --> 00:08:50,270
Enemies I think is a good layer, and then click back on Cuba and assign that layer to Cuba and then

187
00:08:50,270 --> 00:08:54,410
over into edit project settings, physics 2D.

188
00:08:54,530 --> 00:08:58,760
Hopefully you're starting to get the knack of this now with the layers and with the matrix, the enemies.

189
00:08:58,760 --> 00:08:59,930
What do we want them to collide with?

190
00:08:59,930 --> 00:09:01,550
Pretty much just the player, isn't it?

191
00:09:01,550 --> 00:09:02,710
Not really anything else.

192
00:09:02,710 --> 00:09:05,150
So I'm going to click all the other things.

193
00:09:05,150 --> 00:09:06,260
Don't, don't, don't, don't, don't.

194
00:09:06,290 --> 00:09:07,010
Just to be sure.

195
00:09:07,010 --> 00:09:07,490
Background.

196
00:09:07,490 --> 00:09:08,360
No bouncing.

197
00:09:08,360 --> 00:09:08,900
No.

198
00:09:08,930 --> 00:09:10,970
Do we want the enemies to bump into each other?

199
00:09:10,970 --> 00:09:14,810
You may do if you've got a couple of enemies on a thing or you want them to pass through each other.

200
00:09:14,810 --> 00:09:17,000
So passing through we would turn that off.

201
00:09:17,000 --> 00:09:19,190
If you want them to bump, then you would turn that on.

202
00:09:19,190 --> 00:09:20,510
So we'll turn that off for now.

203
00:09:20,510 --> 00:09:25,220
Oh, and of course, we also need the enemy to collide with the ground or else we don't know when our

204
00:09:25,220 --> 00:09:26,510
on trigger exit is happening.

205
00:09:26,510 --> 00:09:28,880
So ground and player, probably not the climbing.

206
00:09:28,880 --> 00:09:32,690
We don't want it to bump into ladders and we'll see if we've got some success here.

207
00:09:32,690 --> 00:09:37,550
Just move you a little bit away from the wall so we can get a bit of a run up at it and bounce off it.

208
00:09:37,670 --> 00:09:38,570
So here we go.

209
00:09:38,570 --> 00:09:40,340
Bounce, bounce, bounce hits the wall.

210
00:09:40,340 --> 00:09:41,330
Nothing happens.

211
00:09:41,330 --> 00:09:46,910
And that's probably, I think because we need this box collider to DH to be a trigger because we were

212
00:09:46,910 --> 00:09:49,250
using what on trigger exit.

213
00:09:49,250 --> 00:09:54,650
Now when we click on play hopefully we've got some success here should run up to the wall, bounce into

214
00:09:54,650 --> 00:09:56,450
the wall and head over in the other direction.

215
00:09:56,450 --> 00:09:57,080
Excellent.

216
00:09:57,080 --> 00:09:59,720
And so the play is probably going to mess up the enemy.

217
00:09:59,720 --> 00:10:02,180
If we run into the enemy, bounce into it.

218
00:10:02,180 --> 00:10:04,730
We'll set up some functionality in the future where the enemy hurts.

219
00:10:04,730 --> 00:10:06,800
The player probably causes the player to die.

220
00:10:06,830 --> 00:10:09,290
But for now, this is the functionality exactly that we want.

221
00:10:09,290 --> 00:10:10,160
So well done.

222
00:10:10,160 --> 00:10:13,220
Hopefully you did well in this lecture and I'll see you in an upcoming lecture.

