1
00:00:03,050 --> 00:00:07,790
In this lecture we're working with if statements and tags so that if we drive over something that is

2
00:00:07,790 --> 00:00:12,590
tagged as customer, we print to the console package picked up and if we drive over something tagged

3
00:00:12,590 --> 00:00:15,290
as customer, we say package delivered.

4
00:00:15,290 --> 00:00:16,730
So let's get started.

5
00:00:16,880 --> 00:00:18,680
Let's start by doing a little bit of repurposing.

6
00:00:18,680 --> 00:00:24,200
I'm going to click on my object here that was just called Square and I'm going to rename this to B package.

7
00:00:24,200 --> 00:00:27,590
So this is now the package we're picking up and just reshape it a little bit.

8
00:00:27,590 --> 00:00:28,520
So there we go.

9
00:00:28,520 --> 00:00:30,000
It's a bit of a square package.

10
00:00:30,020 --> 00:00:32,960
Doesn't quite look like the thing Amazon would deliver, but there we go.

11
00:00:32,960 --> 00:00:37,220
Well, pop it somewhere in our world so we can drive over it, put over near the player so it's nice

12
00:00:37,220 --> 00:00:38,300
and quick to find.

13
00:00:38,300 --> 00:00:38,600
Excellent.

14
00:00:38,600 --> 00:00:39,350
That's step one.

15
00:00:39,350 --> 00:00:44,540
Now, step two, I'm going to be using my collision script because that's what we've been dealing with

16
00:00:44,540 --> 00:00:46,640
on trigger enter and on collision enter.

17
00:00:46,640 --> 00:00:48,320
But collision doesn't make sense anymore.

18
00:00:48,320 --> 00:00:51,740
So I want to show you how we rename a script because it can be a little bit messy.

19
00:00:51,740 --> 00:00:55,670
First of all, I'm going to click on where it says Collision one more time or you can hit F2.

20
00:00:55,670 --> 00:00:58,040
Let me just check that works f to set a collision.

21
00:00:58,040 --> 00:01:00,440
I'm going to name this delivery because that makes more sense.

22
00:01:00,440 --> 00:01:02,480
We're dealing with the delivery system.

23
00:01:02,480 --> 00:01:03,860
So I'm going to hit enter now.

24
00:01:03,860 --> 00:01:05,510
I'm going to open up delivery.

25
00:01:05,510 --> 00:01:08,060
And you can see here, it's still public class collision.

26
00:01:08,060 --> 00:01:10,040
So it thinks I am a collision class.

27
00:01:10,070 --> 00:01:14,930
I'm going to just highlight the word collision and change that to the word delivery, making sure we

28
00:01:14,930 --> 00:01:20,480
spell it exactly the same as the name of our dot CSS file I'm going to save.

29
00:01:20,480 --> 00:01:22,160
Now back over into Unity.

30
00:01:22,160 --> 00:01:26,900
And the last step is to click on Cruise Image Drive and we can see here that the delivery script has

31
00:01:26,900 --> 00:01:29,690
automatically been updated from collision to delivery.

32
00:01:29,690 --> 00:01:29,950
Cool.

33
00:01:29,960 --> 00:01:32,450
So that should all work exactly the way we want to.

34
00:01:32,480 --> 00:01:34,850
Now, let me tell you a little bit about the logic of what we're going to do.

35
00:01:34,850 --> 00:01:41,030
I'm going to show you what happens when we drive over so when we trigger something to happen by driving

36
00:01:41,030 --> 00:01:43,640
over our package, I'm going to show you that.

37
00:01:43,640 --> 00:01:47,930
And I'm going to print out to the console a message that says something like, you picked up the package,

38
00:01:47,930 --> 00:01:54,080
but we only want to print that to the console if it's the package that we drive over.

39
00:01:54,080 --> 00:01:59,240
Because I'm going to give you a challenge later on in this lecture where I challenge you to make the

40
00:01:59,240 --> 00:01:59,900
customer.

41
00:01:59,900 --> 00:02:03,500
And if we drive over the customer, we want to print a different message.

42
00:02:03,500 --> 00:02:08,240
So we can't just say when you collide with anything, let's just have a look at our delivery script.

43
00:02:08,240 --> 00:02:12,710
It says, When you collide with anything or when you trigger anything, do a particular thing.

44
00:02:12,740 --> 00:02:16,790
But it's like, okay, well, when we when we have on trigger enter with one thing, we want to do one

45
00:02:16,790 --> 00:02:19,040
thing, but if it's on trigger, enter is something else.

46
00:02:19,040 --> 00:02:20,000
We want to do something else.

47
00:02:20,000 --> 00:02:20,870
How do we do that?

48
00:02:20,870 --> 00:02:24,050
The way we do that is with if statements.

49
00:02:24,050 --> 00:02:26,060
Let's talk about if statements just briefly here.

50
00:02:26,060 --> 00:02:31,880
So an if statement lets us check if something is true or not and then do something based upon the result.

51
00:02:31,880 --> 00:02:37,190
And then this is how the syntax looks in C-sharp we start with the if keyword and then in parentheses

52
00:02:37,190 --> 00:02:40,430
we have the thing that we want to know if it's true or not.

53
00:02:40,430 --> 00:02:44,120
So we need to write something in there saying, Is this particular thing true?

54
00:02:44,120 --> 00:02:49,340
And then in our code block we have what it is that we want to have happen if that thing actually is

55
00:02:49,340 --> 00:02:49,730
true.

56
00:02:49,730 --> 00:02:55,790
So for example, for me it might be if I'm hungry right now, then eat junk food while my wife isn't

57
00:02:55,790 --> 00:02:56,240
watching.

58
00:02:56,240 --> 00:02:58,550
That could be my if statement.

59
00:02:58,700 --> 00:03:02,750
You can tell what's on my mind at the moment and if I'm hungry turns out to be false.

60
00:03:02,750 --> 00:03:05,510
Then we don't even consider what's in the statement here.

61
00:03:05,510 --> 00:03:09,890
We don't even think about sneaking some tasty donuts while the wife isn't watching.

62
00:03:09,890 --> 00:03:12,170
It's a way of saying, if I'm hungry, do this.

63
00:03:12,170 --> 00:03:14,030
Otherwise do nothing.

64
00:03:14,030 --> 00:03:17,090
Okay, so let's start this process in here.

65
00:03:17,090 --> 00:03:18,680
Now I'll leave my on collision.

66
00:03:18,680 --> 00:03:22,730
Enter in there just in case we need it in the future, in case we bump into something, we might want

67
00:03:22,730 --> 00:03:23,660
to do something with that.

68
00:03:23,660 --> 00:03:26,690
But we're focusing the on trigger index triggers.

69
00:03:26,690 --> 00:03:30,140
We don't want to bounce into the package and the customer is going to drive over them.

70
00:03:30,140 --> 00:03:34,760
So in here we need to start out if statement and I'm just going to put a comment in here and write some

71
00:03:34,760 --> 00:03:39,680
pseudo code because we don't have everything we need yet to execute on this, but we'll write the general

72
00:03:39,680 --> 00:03:40,430
meaning of what we're doing.

73
00:03:40,430 --> 00:03:47,270
So if the thing we trigger is the package, then what do we want to do then?

74
00:03:47,270 --> 00:03:52,160
Print package picked up to the console.

75
00:03:52,160 --> 00:03:56,240
Now we can give this a little bit more structure by saying we'd have our curly braces and then we would

76
00:03:56,240 --> 00:04:01,310
have our print statement and then we would have our other curly braces.

77
00:04:01,340 --> 00:04:02,900
All of these are within the comments.

78
00:04:02,900 --> 00:04:03,950
You don't need to be typing this.

79
00:04:03,950 --> 00:04:06,170
I just want to type this so you can see exactly what we're doing.

80
00:04:06,170 --> 00:04:08,930
And the bit that we don't have is how do we know if it's a package or not?

81
00:04:08,930 --> 00:04:11,630
So let's have a chat about tags.

82
00:04:11,660 --> 00:04:17,660
Tags allow us to easily check in our code if something belongs to a particular category of thing and

83
00:04:17,660 --> 00:04:20,839
allows us to speed up that process of saying, Are you the package?

84
00:04:20,839 --> 00:04:21,500
Are you the package?

85
00:04:21,500 --> 00:04:22,250
Are you the package?

86
00:04:22,250 --> 00:04:24,380
So I'll show you how we go about doing that.

87
00:04:24,380 --> 00:04:25,460
We have our package.

88
00:04:25,460 --> 00:04:30,080
We're clicked on this game object and over in the top right of the inspector, you'll see Tag Untagged.

89
00:04:30,080 --> 00:04:31,430
At the moment it doesn't have a tag.

90
00:04:31,430 --> 00:04:33,770
It's like, I don't know what sort of tag you want me to be.

91
00:04:33,770 --> 00:04:34,340
No worries.

92
00:04:34,340 --> 00:04:39,890
When we click on the untagged area, we've got some default tags that already exist in Unity.

93
00:04:39,890 --> 00:04:44,450
Player is probably the main one that we would use in the future, but for now we're going to click on

94
00:04:44,450 --> 00:04:47,420
the ad tag and then you'll see Under Tags List is empty.

95
00:04:47,420 --> 00:04:49,100
Click the plus button in here.

96
00:04:49,100 --> 00:04:54,320
I'm going to type package as the tag uppercase P and then click on Save.

97
00:04:54,320 --> 00:04:57,650
Now we have package that hasn't assigned it yet.

98
00:04:57,650 --> 00:05:00,200
What you need to do is click on the game object again.

99
00:05:00,200 --> 00:05:01,280
You see it's untagged.

100
00:05:01,280 --> 00:05:02,150
Now I can go in.

101
00:05:02,250 --> 00:05:07,760
Select package, and we've tagged this game object as being of tag package.

102
00:05:07,770 --> 00:05:11,610
It's not a type package, it's just been tagged as package.

103
00:05:11,610 --> 00:05:15,930
Now we can jump back into our code and before I get rid of the comments, there's one other thing I

104
00:05:15,930 --> 00:05:16,980
want to show you in here.

105
00:05:16,980 --> 00:05:18,090
We talked about this one.

106
00:05:18,090 --> 00:05:23,220
We talked about the collision and the trigger that you can see where accessing or something's being

107
00:05:23,220 --> 00:05:24,480
passed into this.

108
00:05:24,480 --> 00:05:29,130
When it's cold, it's being passed in, it's giving us information about the other thing that we bumped

109
00:05:29,130 --> 00:05:29,670
into.

110
00:05:29,670 --> 00:05:31,080
So this is really cool.

111
00:05:31,080 --> 00:05:35,970
This delivery script is on our car, but when we trigger something or when we bump into something with

112
00:05:35,970 --> 00:05:41,280
collision, we know what it is that we bumped into so we can gather information from that.

113
00:05:41,280 --> 00:05:46,380
And in this case, we're going to say the other thing that we collided with, does that have a tag on

114
00:05:46,380 --> 00:05:47,550
it that says package?

115
00:05:47,550 --> 00:05:48,240
So that's what we'll do.

116
00:05:48,240 --> 00:05:49,350
We'll get rid of our comments here.

117
00:05:49,350 --> 00:05:50,310
We don't need those anymore.

118
00:05:50,310 --> 00:05:50,910
Thanks very much.

119
00:05:50,910 --> 00:05:51,570
Comments.

120
00:05:51,600 --> 00:05:52,770
Lots of squiggly lines.

121
00:05:52,770 --> 00:05:53,190
Cool.

122
00:05:53,190 --> 00:06:00,780
If we're going to access the other thing we bumped into by typing other dot and then it's as simple

123
00:06:00,780 --> 00:06:07,050
as saying tag, then we're going to use the double equals sign operator, which is a way of comparing

124
00:06:07,050 --> 00:06:10,110
the left and the right and saying, Are these two things the same?

125
00:06:10,110 --> 00:06:12,540
So we'd say equals equals.

126
00:06:12,630 --> 00:06:18,660
And then we're going to write in here quotation marks, capital P package, if I can spell it correctly.

127
00:06:18,660 --> 00:06:24,390
And this as soon as we use the equals equals operator, it's going to say, is it true or not?

128
00:06:24,390 --> 00:06:26,190
Does the left equal the right?

129
00:06:26,190 --> 00:06:29,010
We get a true or false value back from that.

130
00:06:29,010 --> 00:06:33,810
So this is going to say it's either true, yep, it's a package or the tag is not a package.

131
00:06:33,810 --> 00:06:35,280
And then in here, what do we want to do?

132
00:06:35,280 --> 00:06:45,210
Well, we will type in debug dot log package, picked up semicolon and save.

133
00:06:45,210 --> 00:06:47,160
Now let's jump back over into our game.

134
00:06:47,160 --> 00:06:50,850
Click on the console so that we can see if this is actually going to work or not.

135
00:06:50,850 --> 00:06:57,990
Click on play drive our car over to the package pip and it says Package picked up.

136
00:06:57,990 --> 00:06:58,350
Excellent.

137
00:06:58,350 --> 00:07:01,560
If you drive over it and over it, it's going to go up and up and up and up and up and up.

138
00:07:01,560 --> 00:07:05,550
So that's something we'll deal with in the future where we we get rid of the package after we've picked

139
00:07:05,550 --> 00:07:05,910
it up.

140
00:07:06,240 --> 00:07:08,790
For now, we can print out the message that we've picked up the package.

141
00:07:08,790 --> 00:07:10,920
So I've got a challenge for you.

142
00:07:10,920 --> 00:07:15,240
It's going to be a little bit more detailed, but I think you're ready for it is to set up our delivery

143
00:07:15,240 --> 00:07:16,320
location.

144
00:07:16,320 --> 00:07:17,910
So I'm going to call it the customer.

145
00:07:17,940 --> 00:07:20,190
It's probably just going to be a box or a circle or something.

146
00:07:20,190 --> 00:07:23,280
It doesn't actually need to look like the customer because we're going to drive over it.

147
00:07:23,280 --> 00:07:30,000
But I want you to create a customer that allows us to print delivered package to the console if we drive

148
00:07:30,000 --> 00:07:33,030
over it, similar to the way that we drove over our package.

149
00:07:33,030 --> 00:07:36,480
Now, if that's enough for you to go on, pause the video and jump in and take that on.

150
00:07:36,480 --> 00:07:38,580
Now, otherwise, I've got some hints for you.

151
00:07:38,580 --> 00:07:40,530
So if you don't need the hints, pause the video.

152
00:07:40,890 --> 00:07:42,930
But if you would like the hints, here they are.

153
00:07:42,930 --> 00:07:45,540
Create a game object that has a collider on it.

154
00:07:45,540 --> 00:07:51,090
If you can't see the object, check your sorting layer to make sure it's above the road set is triggered

155
00:07:51,090 --> 00:07:52,410
to true on the collider.

156
00:07:52,410 --> 00:07:57,810
Create a customer tag as we did with the package tag and assign it to the object and then print your

157
00:07:57,810 --> 00:08:00,000
message if we touch the customer object.

158
00:08:00,000 --> 00:08:00,840
So there you go.

159
00:08:00,840 --> 00:08:02,430
There's your challenge plus video.

160
00:08:02,430 --> 00:08:03,690
I'll see you back here when you're done.

161
00:08:06,860 --> 00:08:07,070
Okay.

162
00:08:07,390 --> 00:08:09,280
First step is get out of play mode.

163
00:08:09,280 --> 00:08:10,150
That's a good first step.

164
00:08:10,150 --> 00:08:14,410
And then right click, I'm going to create a 2D object will make a sprite.

165
00:08:14,410 --> 00:08:15,280
What should we make this?

166
00:08:15,550 --> 00:08:17,230
I'll make this a square as well.

167
00:08:17,230 --> 00:08:18,250
I'll just change its color.

168
00:08:18,250 --> 00:08:20,650
So we'll pop the customer over here.

169
00:08:20,650 --> 00:08:22,390
I'm going to increase the size of this a little bit.

170
00:08:22,390 --> 00:08:26,140
Just hit R and then grab the middle of it and scale it up for now.

171
00:08:26,140 --> 00:08:27,130
So it's nice and easy to see.

172
00:08:27,130 --> 00:08:29,560
We can change the graphics on this later on pretty easily.

173
00:08:29,560 --> 00:08:32,470
Change the color to maybe a blue.

174
00:08:32,470 --> 00:08:37,480
Now I'll rename this game object to be customer hit enter.

175
00:08:37,510 --> 00:08:44,320
I also need to create a tag so click on tag, add a new tag, click on plus and we will name this customer.

176
00:08:44,980 --> 00:08:46,180
Click on Save.

177
00:08:46,180 --> 00:08:51,430
Now back to clicking on customer the game object and tag this as customer the tag.

178
00:08:51,430 --> 00:08:52,120
Excellent.

179
00:08:52,120 --> 00:08:55,240
Now we need to go into our code.

180
00:08:55,240 --> 00:08:56,200
I think we've done all of this.

181
00:08:56,200 --> 00:08:58,030
We're going to create another if statement.

182
00:08:58,030 --> 00:09:06,250
So second if statement which will if other tag equals equals customer, then what do we want to do?

183
00:09:06,280 --> 00:09:15,160
Put in our curly braces, debug dot log package delivered semicolon and then save.

184
00:09:15,160 --> 00:09:20,740
Now I usually miss a step in these things, so let's see if I did or didn't miss a step.

185
00:09:20,740 --> 00:09:22,150
I'm going to click on play.

186
00:09:25,290 --> 00:09:25,440
Putt.

187
00:09:25,440 --> 00:09:25,800
Putt, putt.

188
00:09:25,800 --> 00:09:26,940
Drive over here.

189
00:09:26,940 --> 00:09:27,470
Yeah.

190
00:09:27,480 --> 00:09:28,380
Package picked up.

191
00:09:28,380 --> 00:09:29,400
Drive over here.

192
00:09:29,910 --> 00:09:30,510
Okay.

193
00:09:30,510 --> 00:09:34,770
So I have missed the step and I know what it is straight away because I've missed this step many times

194
00:09:34,770 --> 00:09:35,580
in my career.

195
00:09:35,580 --> 00:09:36,390
Do you know what it is?

196
00:09:36,390 --> 00:09:37,520
What has Rick done wrong?

197
00:09:37,530 --> 00:09:38,550
What mistake have I done?

198
00:09:38,550 --> 00:09:41,400
If I click on customer, I don't have a collision on here.

199
00:09:41,400 --> 00:09:42,600
How do I know I've bumped into it?

200
00:09:42,600 --> 00:09:45,360
So I'm going to add a box collider to DX.

201
00:09:45,360 --> 00:09:51,540
And then the very last step is to make sure that I click on is Trigger and then Save.

202
00:09:52,220 --> 00:09:53,990
Now click on play.

203
00:09:54,230 --> 00:09:56,150
That should have got it.

204
00:09:56,150 --> 00:09:58,190
So drive to drive over that one.

205
00:09:58,190 --> 00:09:58,430
Good.

206
00:09:58,430 --> 00:09:59,530
And then over this one.

207
00:09:59,540 --> 00:10:01,010
Excellent package delivered.

208
00:10:01,040 --> 00:10:01,250
Cool.

209
00:10:01,250 --> 00:10:04,130
So now we've got the fundamentals in our code of it knows.

210
00:10:04,130 --> 00:10:07,640
Oh, yeah, you went over a package and oh yeah, you delivered it to the customer, so.

211
00:10:07,640 --> 00:10:08,240
Excellent.

212
00:10:08,240 --> 00:10:09,410
So that's all for this lecture.

213
00:10:09,410 --> 00:10:10,970
I will see you in the next one.

