1
00:00:03,100 --> 00:00:07,120
In this lecture, we're taking on a meatier challenge where we're going to have particles that play

2
00:00:07,120 --> 00:00:12,010
when the character is touching the ground and turn off when the player is in the air.

3
00:00:12,040 --> 00:00:13,480
So let's get started.

4
00:00:13,510 --> 00:00:17,650
I'll start by showing you a new concept, and then I'm going to give you a nice, big, meaty challenge

5
00:00:17,650 --> 00:00:21,010
that brings together a lot of the stuff we've been talking about in this section.

6
00:00:21,010 --> 00:00:23,440
So you don't need to follow along with adding these things.

7
00:00:23,440 --> 00:00:27,400
I'm just showing it to illustrate, we've got two sprites, one on the left, one on the right, and

8
00:00:27,400 --> 00:00:29,890
each of them has a box collider on them.

9
00:00:29,890 --> 00:00:31,360
So if I turn off that sprite renderer.

10
00:00:31,360 --> 00:00:34,930
Yeah, you can see there's the box collider on the right one and the left one has the same.

11
00:00:34,930 --> 00:00:40,900
Now, what we know already is that when this one or when that one moves and goes bump and bumps into

12
00:00:40,900 --> 00:00:42,820
that, you can see the green line on here.

13
00:00:42,820 --> 00:00:43,630
I've left it like that.

14
00:00:43,630 --> 00:00:45,000
So you can see the green line.

15
00:00:45,010 --> 00:00:48,130
This one also has its green line, which is the collider and it goes bump.

16
00:00:48,130 --> 00:00:54,340
At that very moment we get an on collision enter event that happens that unity knows about and then

17
00:00:54,340 --> 00:00:56,140
we can use that moment to do stuff.

18
00:00:56,140 --> 00:01:01,540
We can say play a particle effect or end the level or do whatever or crash the player.

19
00:01:01,540 --> 00:01:03,640
So there's a moment bump that's on collision.

20
00:01:03,640 --> 00:01:05,430
Enter 2D we've been working through.

21
00:01:05,440 --> 00:01:11,380
There's also another thing we know is that when they are no longer touching, there's another event

22
00:01:11,380 --> 00:01:13,480
which is on collision exit.

23
00:01:13,480 --> 00:01:15,190
So it works exactly the same way.

24
00:01:15,190 --> 00:01:17,920
But one is when they touch and the other is when they stop touching.

25
00:01:17,920 --> 00:01:19,150
It's the same with trigger volumes.

26
00:01:19,150 --> 00:01:24,550
If the one that I'm wiggling here is a trigger as opposed to a collider, then as you pass through it

27
00:01:24,550 --> 00:01:26,770
you'll say that's an on trigger enter.

28
00:01:26,770 --> 00:01:32,800
And as you finally leave it just there, you'll get an on trigger exit event.

29
00:01:32,800 --> 00:01:36,760
And we can use that to say, okay, when is Barre the snowboarder?

30
00:01:36,760 --> 00:01:38,710
I'm just going to delete these guys don't need them anymore.

31
00:01:38,710 --> 00:01:39,430
Thanks very much.

32
00:01:39,430 --> 00:01:42,820
When is Barre, the snowboarder, touching the ground?

33
00:01:42,820 --> 00:01:45,370
That's when his collider is touching with the ground.

34
00:01:45,370 --> 00:01:51,670
Collider We know at the very moment that happens on trigger enter and then if Barre gets some sweet

35
00:01:51,670 --> 00:01:56,080
air off one of the jumps, then we get an on collision exit moment.

36
00:01:56,080 --> 00:01:59,440
So there's a little bit of theory here is the meaty challenge, I promise.

37
00:01:59,440 --> 00:02:02,530
First of all, I'd like you to create a new script called Dust Trail.

38
00:02:02,740 --> 00:02:02,940
Okay?

39
00:02:03,070 --> 00:02:07,450
So we're going to put all of our logic in this new script, and then I want you to make particles that

40
00:02:07,450 --> 00:02:09,580
can be scattered along behind the player.

41
00:02:09,580 --> 00:02:12,880
So just a little simple particle system doesn't need to be super fancy.

42
00:02:12,880 --> 00:02:17,620
And then here's the cool thing only play the particles when the player is touching the ground.

43
00:02:17,620 --> 00:02:19,810
And this is the main meat of the challenge.

44
00:02:19,810 --> 00:02:23,890
And if that's enough for you, if you're like, Yeah, I got this, Rick, no worries, then pause the

45
00:02:23,890 --> 00:02:24,280
video.

46
00:02:24,280 --> 00:02:26,350
Take that on if you'd like a couple of hints.

47
00:02:26,350 --> 00:02:33,970
Here are some hints for you consider using on collision into 2D and on Collision Exit 2D.

48
00:02:33,970 --> 00:02:37,300
The theory that I was just telling you about, I was telling it to you for a reason.

49
00:02:37,300 --> 00:02:42,250
It wasn't just random pondering from an old man for collision events, for entering and exiting.

50
00:02:42,250 --> 00:02:49,210
We can use other game object tag to find out what it is that we're colliding with now.

51
00:02:49,210 --> 00:02:53,830
I think in the past when we were bumping into triggers, we only used other tag.

52
00:02:53,830 --> 00:02:58,360
But if it's a collision event because we're getting information about the game object, that's what

53
00:02:58,360 --> 00:02:59,350
other is all about.

54
00:02:59,350 --> 00:03:05,940
Other game objects tag might be what you need to use and then consider using particle system stock.

55
00:03:05,950 --> 00:03:11,560
We've used particle system play, so I'm pretty sure you can figure out what particle system stop is

56
00:03:11,560 --> 00:03:13,360
because I know you're a clever cookie.

57
00:03:13,360 --> 00:03:15,040
So there's our challenge.

58
00:03:15,040 --> 00:03:16,210
It's a nice, meaty one.

59
00:03:16,210 --> 00:03:17,050
Give it your best shot.

60
00:03:17,050 --> 00:03:18,400
See you back here when you're done.

61
00:03:20,500 --> 00:03:20,860
Okay.

62
00:03:20,860 --> 00:03:23,550
So I'm going to make a particle system really quickly.

63
00:03:23,560 --> 00:03:28,270
Might look a bit ugly, but click on Barry Right Click Effects Particle System.

64
00:03:28,270 --> 00:03:31,510
I rename the particle system to be, say, dust particles.

65
00:03:31,510 --> 00:03:37,000
I know they're not technically dust, but kind of the thing we're doing is pushing dust behind us.

66
00:03:37,090 --> 00:03:38,200
Kind of snow, I guess.

67
00:03:38,200 --> 00:03:41,500
But we'll call it dust, pop it in the back of where we want to come out.

68
00:03:41,500 --> 00:03:45,130
And I'm just going to change the duration down to something quite small.

69
00:03:45,130 --> 00:03:48,640
So point one, leave on looping.

70
00:03:48,640 --> 00:03:52,840
That's fine because we want it to play, play, play, play until we tell it to stop, start lifetime.

71
00:03:52,840 --> 00:03:54,250
They're going a little bit too far at the moment.

72
00:03:54,250 --> 00:03:56,140
We'll put the Lifetime down to 0.5.

73
00:03:56,140 --> 00:04:00,700
I'll do some variation later on, but for now we're just getting the macro amount start speed.

74
00:04:00,700 --> 00:04:03,340
I think that's fine as well because I'll be moving away.

75
00:04:03,370 --> 00:04:06,370
We want them to be kind of sticking around in the world emission.

76
00:04:06,370 --> 00:04:09,550
I'm going to increase this to say 50.

77
00:04:09,910 --> 00:04:15,130
I'm going to go find my renderer down the bottom, change the material, click on the little selector

78
00:04:15,130 --> 00:04:16,420
to the square thingy.

79
00:04:16,420 --> 00:04:16,930
That's good.

80
00:04:16,930 --> 00:04:17,860
They're now too big.

81
00:04:17,860 --> 00:04:22,150
So I'll reduce the size of this from one down to, say, 0.2.

82
00:04:22,450 --> 00:04:24,070
Okay, so far, so good.

83
00:04:24,070 --> 00:04:27,730
Now I'm going to change the simulation space from local to world.

84
00:04:27,730 --> 00:04:32,020
So that means as we're cruising along, let me see if I can just simulate this for you here.

85
00:04:33,140 --> 00:04:38,090
As we're cruising along, we want them to stay where we leave them in the world.

86
00:04:38,090 --> 00:04:40,280
We don't want them to follow along with Barry.

87
00:04:40,290 --> 00:04:44,990
If I was to have them on local as I move this around, you can see the entire particle system moves

88
00:04:44,990 --> 00:04:45,830
around with me.

89
00:04:45,830 --> 00:04:48,020
So I want to leave that in world space.

90
00:04:48,020 --> 00:04:48,860
Let's click on play.

91
00:04:48,860 --> 00:04:50,540
Just see if it's generally in the ballpark.

92
00:04:50,540 --> 00:04:54,620
I don't want to finesse this too much at the moment because I want to get into the code, but let's

93
00:04:54,620 --> 00:04:56,180
see so you can actually see it.

94
00:04:56,180 --> 00:04:56,460
Okay.

95
00:04:56,480 --> 00:04:56,660
Yeah.

96
00:04:56,660 --> 00:05:02,240
So I mean, there's little particles flying along behind as we cruise along looks okay.

97
00:05:02,240 --> 00:05:03,110
I'm going to change it.

98
00:05:03,110 --> 00:05:08,360
So instead of it being in a emitted in a circle like this, let's change it the shape.

99
00:05:08,360 --> 00:05:11,600
So change the shape to be a cone, I think.

100
00:05:11,600 --> 00:05:13,910
And you can see it doesn't quite look like a cone.

101
00:05:13,910 --> 00:05:14,690
Click on to DH.

102
00:05:14,690 --> 00:05:18,920
You can see the cone is pointing into the screen into the Z or Z axis.

103
00:05:18,920 --> 00:05:21,860
So if we change the the Y, yep.

104
00:05:21,860 --> 00:05:22,220
There we go.

105
00:05:22,230 --> 00:05:23,660
Now we can have it pointing backwards.

106
00:05:23,660 --> 00:05:29,360
So I'm just going to do this at -90, you know, decrease the initial start point of it.

107
00:05:29,360 --> 00:05:30,440
That would be the radius.

108
00:05:30,440 --> 00:05:34,460
Drop that down a little bit, just kick it up a bit and we'll see how this is feeling.

109
00:05:34,460 --> 00:05:34,940
Okay.

110
00:05:34,940 --> 00:05:36,110
Not too bad.

111
00:05:36,110 --> 00:05:37,940
I'm just going to change one last thing and then move on.

112
00:05:37,940 --> 00:05:38,810
I'm going to change the start.

113
00:05:38,810 --> 00:05:40,310
Speed down to say two.

114
00:05:40,340 --> 00:05:40,850
There we go.

115
00:05:40,850 --> 00:05:41,900
Some little dust particles.

116
00:05:41,900 --> 00:05:42,530
Good enough for now.

117
00:05:42,530 --> 00:05:44,000
We can finesse that a bit more later on.

118
00:05:44,000 --> 00:05:47,810
But we've got something and it's called dust particles and it's sitting on Barry.

119
00:05:47,840 --> 00:05:49,880
Next thing I want to do is to create my script.

120
00:05:49,880 --> 00:05:55,520
So create C sharp and we'll call this dust trail.

121
00:05:55,520 --> 00:05:59,390
You know, first of all, what I'll do is add dust trail where I want it to be added.

122
00:05:59,390 --> 00:06:03,290
So I'm going to add dust trail as a script on Barry.

123
00:06:03,290 --> 00:06:07,580
So I will scroll down and just drag dust trail onto Barry.

124
00:06:07,580 --> 00:06:10,880
You might be saying, why not just put all of these things in the player controller?

125
00:06:10,880 --> 00:06:12,890
Rick We could just shove all of our code in there.

126
00:06:12,890 --> 00:06:13,880
You could do that.

127
00:06:13,880 --> 00:06:17,690
But then it becomes a big old mess every time you go into the player controller script.

128
00:06:17,690 --> 00:06:21,950
If you've got four or five or six or seven different things that it's doing, you're like, Where do

129
00:06:21,950 --> 00:06:22,460
I find it?

130
00:06:22,490 --> 00:06:23,780
What's what's doing what?

131
00:06:23,780 --> 00:06:24,410
How does it work?

132
00:06:24,410 --> 00:06:28,580
So if you've got these things as separate scripts, once you've set up the dust trail, it doesn't need

133
00:06:28,580 --> 00:06:32,420
to get in the way anymore if you're doing work in the player, controller and so on.

134
00:06:32,420 --> 00:06:37,490
So this is part of good coding practice is for each class or each script.

135
00:06:37,490 --> 00:06:40,550
Try to have it, do just one thing, have one purpose to it.

136
00:06:40,670 --> 00:06:42,290
Back to dust trail.

137
00:06:42,290 --> 00:06:46,520
Now first thing I want to do is point to or get a reference for our particles.

138
00:06:46,520 --> 00:06:52,790
So I'm going to serialize field and this will be particle system and the particle system I'm going to

139
00:06:52,790 --> 00:06:55,040
call dust particles.

140
00:06:55,040 --> 00:07:00,020
Now, pretty sure I'm not going to use start or update, so we'll delete those and I'm going to create

141
00:07:00,020 --> 00:07:07,250
myself an on collision enter to DH just remove the private tidy up the formatting a little bit.

142
00:07:07,250 --> 00:07:12,560
And you know, while I'm here, I'll also add my on collision exit to DX.

143
00:07:12,800 --> 00:07:16,040
So we've got our shape all ready and raring.

144
00:07:16,040 --> 00:07:23,060
I'm also going to jump over into my world, click on my level, sprite shape and tag that as ground.

145
00:07:23,060 --> 00:07:24,140
It's already tagged as ground.

146
00:07:24,140 --> 00:07:24,680
Oh, look at that.

147
00:07:24,680 --> 00:07:27,650
We didn't need to have that as a clue that was already done.

148
00:07:27,650 --> 00:07:32,120
So yeah, for us, I like looking at the visuals when I'm trying to figure out what the code is going

149
00:07:32,120 --> 00:07:38,210
to do if the player is touching the ground play particles, if the player is not touching the ground,

150
00:07:38,210 --> 00:07:39,440
don't play particles.

151
00:07:39,440 --> 00:07:41,930
So we want to make sure, first of all, that we're touching the ground.

152
00:07:41,930 --> 00:07:48,530
So if other remember we get the information about the other thing that we're touching are the dot game

153
00:07:48,530 --> 00:07:53,720
object tag equals equals and then we want to have ground.

154
00:07:53,720 --> 00:07:55,400
Make sure we spell that correctly.

155
00:07:55,400 --> 00:07:57,380
Then what do we want to do?

156
00:07:57,410 --> 00:07:59,540
We want to find our dust particles.

157
00:07:59,540 --> 00:08:01,970
So dust particles and we want to play them.

158
00:08:01,970 --> 00:08:03,530
Man There we go.

159
00:08:03,530 --> 00:08:05,150
Parentheses, semicolon.

160
00:08:05,150 --> 00:08:10,130
If we're touching the ground play and then if we exit, if we are not touching the ground.

161
00:08:10,130 --> 00:08:15,500
So if other game object tag equals equals ground.

162
00:08:15,500 --> 00:08:20,090
So in other words, if we've just gone and exited the ground, then what do we want to do?

163
00:08:20,120 --> 00:08:27,950
Dust particles, dot stop and let's save that up and see if by some miracle I haven't forgotten a step.

164
00:08:27,950 --> 00:08:30,080
What we do need to do is find Barry.

165
00:08:30,080 --> 00:08:33,770
We've got our dust script, we've got our particle system.

166
00:08:33,770 --> 00:08:36,710
We want to grab our dust particles, drag that over there.

167
00:08:36,710 --> 00:08:39,710
So it's very clear this is what we want to be popping out behind us.

168
00:08:39,710 --> 00:08:42,230
Make my screen a little bit bigger so we see what's going on here.

169
00:08:42,230 --> 00:08:44,960
If we're touching the ground, do we have particles?

170
00:08:45,080 --> 00:08:47,030
Okay, the particles started before we landed.

171
00:08:47,030 --> 00:08:47,930
We might want to look at that.

172
00:08:47,930 --> 00:08:52,010
And then we get some sweet air, but no particles whatsoever.

173
00:08:52,040 --> 00:08:53,240
Get some speed on that.

174
00:08:53,240 --> 00:08:53,780
Awesome.

175
00:08:53,780 --> 00:08:57,800
So they're turning on and off based upon whether we're touching the ground just with a little bit of

176
00:08:57,800 --> 00:08:58,820
simple logic there.

177
00:08:58,820 --> 00:09:01,250
So hopefully you went okay with that challenge.

178
00:09:01,340 --> 00:09:06,860
Let's do one last thing here where I'm going to click on the dust particles and we just need to turn

179
00:09:06,860 --> 00:09:08,960
off the plate on a week.

180
00:09:08,960 --> 00:09:12,770
We only want to have them play when we start touching the ground.

181
00:09:12,770 --> 00:09:13,750
Let's have a look.

182
00:09:14,600 --> 00:09:15,230
Touching the ground.

183
00:09:15,230 --> 00:09:15,800
Touching the ground.

184
00:09:15,800 --> 00:09:16,160
Okay.

185
00:09:16,160 --> 00:09:19,130
So if you wanted to, you could go and finesse your particles a bit more.

186
00:09:19,130 --> 00:09:23,870
I might do a little bit of that off camera, but not enough that you need to worry about just with that

187
00:09:23,870 --> 00:09:24,590
finessing of it.

188
00:09:24,590 --> 00:09:25,610
So good stuff.

189
00:09:25,610 --> 00:09:27,980
I look forward to seeing you in the next lecture.

