Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:04,290 --> 00:00:07,340
In this video, we're implementing
our damage systems so that blank,
2
2
00:00:07,340 --> 00:00:11,020
there we go, we can shoot projectiles
that kill enemies that have health.
3
3
00:00:11,020 --> 00:00:12,560
And when they get to zero,
they're destroyed.
4
4
00:00:12,560 --> 00:00:14,820
So, let's jump in and get started.
5
5
00:00:14,820 --> 00:00:17,272
Okay, let's start off by
implementing some colliders.
6
6
00:00:17,272 --> 00:00:20,910
I'm gonna find my Zucchini and
add a component,
7
7
00:00:20,910 --> 00:00:24,220
which will be a collider, box collider 2D.
8
8
00:00:24,220 --> 00:00:25,010
I think that's pretty good.
9
9
00:00:25,010 --> 00:00:26,590
It doesn't need to be
perfect with the Zucchini.
10
10
00:00:26,590 --> 00:00:29,020
I'll edit just a little bit, bring it in.
11
11
00:00:29,020 --> 00:00:32,270
Bring it down, bring it in just so
it's reasonably tight.
12
12
00:00:33,670 --> 00:00:34,920
Click on the Edit Collider.
13
13
00:00:36,220 --> 00:00:36,850
Okay, there we go.
14
14
00:00:36,850 --> 00:00:38,882
Pretty good.
It just kinda has to strike the center of
15
15
00:00:38,882 --> 00:00:39,530
the Zucchini.
16
16
00:00:39,530 --> 00:00:42,840
We're in lanes, so
we don't need super precise collision.
17
17
00:00:42,840 --> 00:00:45,210
That's great,
apply that back to the prefab, excellent.
18
18
00:00:45,210 --> 00:00:47,480
I think I can remove my
Zucchini from the scene now.
19
19
00:00:47,480 --> 00:00:50,220
We'll just be using prefabs
when we instantiate them.
20
20
00:00:50,220 --> 00:00:51,800
Now, let's find the lizard.
21
21
00:00:51,800 --> 00:00:54,130
The lizard, I do need to drag
a lizard into the scene, so
22
22
00:00:54,130 --> 00:00:55,800
I can see what's going on.
23
23
00:00:55,800 --> 00:00:58,640
Click on ADD COMPONENT, Box Collider 2D.
24
24
00:00:58,640 --> 00:01:01,240
That's around the entire outside,
which is a bit big.
25
25
00:01:01,240 --> 00:01:03,620
I'm gonna switch onto Lizard Walk.
26
26
00:01:03,620 --> 00:01:06,907
I think that's where I wanna have my
collider around wherever he walks, because
27
27
00:01:06,907 --> 00:01:09,965
during the jump, I think the wizard
should be little and even vulnerable.
28
28
00:01:09,965 --> 00:01:11,306
Jump, jump, jump, jump.
29
29
00:01:11,306 --> 00:01:16,520
Okay, during that stage, I don't really
want my cactus to be hitting it.
30
30
00:01:16,520 --> 00:01:21,460
So I will just wrap my collider around,
what it looks like when he's walking.
31
31
00:01:21,460 --> 00:01:24,603
So edit collider on the box,
collider move this in.
32
32
00:01:24,603 --> 00:01:27,900
Move that in as well, move that down.
33
33
00:01:27,900 --> 00:01:30,250
Now move that whoops.
34
34
00:01:30,250 --> 00:01:32,480
That's bit of a pain when that happens.
35
35
00:01:32,480 --> 00:01:33,720
Back on Edit Collider.
36
36
00:01:34,860 --> 00:01:36,830
And there we go, excellent.
37
37
00:01:36,830 --> 00:01:39,350
Apply that back to the prefab.
38
38
00:01:39,350 --> 00:01:43,060
Very good, and once again, I don't
think I need to my lizard any more.
39
39
00:01:43,060 --> 00:01:47,390
What I will do is find a Zucchini and
find where we,
40
40
00:01:47,390 --> 00:01:49,370
is Trigger is on the Box Collider 2D.
41
41
00:01:49,370 --> 00:01:51,460
Is Trigger, tick that on.
42
42
00:01:51,460 --> 00:01:55,690
Now, one thing we need to review
any time we're doing collisions,
43
43
00:01:55,690 --> 00:01:56,880
is our collision matrix.
44
44
00:01:56,880 --> 00:01:58,060
So let's have a quick look at that.
45
45
00:01:58,060 --> 00:02:00,080
We can see all of these grids in here.
46
46
00:02:00,080 --> 00:02:01,700
We've looked at these before.
47
47
00:02:01,700 --> 00:02:05,350
We're using
a Trigger Static Trigger Collidor,
48
48
00:02:05,350 --> 00:02:09,460
Dynamic Rigidbody Trigger Collidor,
Kinematic Rigidbody Trigger Collidor.
49
49
00:02:09,460 --> 00:02:12,420
Well it's not static and it's not dynamic,
50
50
00:02:12,420 --> 00:02:17,430
so it's gonna be a Kinematic Rigidbody
Trigger Collidor which reminds me.
51
51
00:02:17,430 --> 00:02:19,410
We don't have a rigidbody anywhere.
52
52
00:02:19,410 --> 00:02:22,030
So I'll find my lizard.
53
53
00:02:23,100 --> 00:02:27,060
And add a rigidbody.
54
54
00:02:27,060 --> 00:02:28,580
I didn't list that out in my requirements.
55
55
00:02:28,580 --> 00:02:30,380
This is one of those things
when you do the requirements,
56
56
00:02:30,380 --> 00:02:31,570
you think you're being pretty thorough.
57
57
00:02:31,570 --> 00:02:33,620
But we missed, that we need a rigidbody.
58
58
00:02:33,620 --> 00:02:37,250
I'll switch this to kinematic, cuz we're
saying walk across the screen this way.
59
59
00:02:37,250 --> 00:02:40,390
I'm putting a rigidbody onto the lizard.
60
60
00:02:40,390 --> 00:02:43,840
And we could put a rigidbody
on the projectile as well.
61
61
00:02:43,840 --> 00:02:47,184
Actually, let's do that just so
we are being thorough on the Zucchini.
62
62
00:02:47,184 --> 00:02:49,836
Add Component, RigidBody 2D.
63
63
00:02:49,836 --> 00:02:53,350
And it's gonna work without this,
I believe, but we'll say it's Kinematic.
64
64
00:02:53,350 --> 00:02:56,650
Now, temporary over into your scripts.
65
65
00:02:56,650 --> 00:02:57,810
What script are we looking for
66
66
00:02:57,810 --> 00:02:59,670
here as I move my microphone
out of the way a little bit?
67
67
00:02:59,670 --> 00:03:01,660
I'm looking for the Projectile script.
68
68
00:03:01,660 --> 00:03:03,120
Open that up in Visual Studio.
69
69
00:03:04,620 --> 00:03:09,070
Let's start a new line, where we're
going to check for the trigger.
70
70
00:03:09,070 --> 00:03:11,610
So we'll say void OnTriggerEnter2D,
71
71
00:03:11,610 --> 00:03:15,720
and the auto completes
a bunch of stuff for us.
72
72
00:03:15,720 --> 00:03:17,960
I'm gonna rename from collision here,
73
73
00:03:17,960 --> 00:03:23,450
this is what's getting passed in,
rename to this to otherCollider.
74
74
00:03:23,450 --> 00:03:25,150
otherCollision, we'll
cal it otherCollider.
75
75
00:03:25,150 --> 00:03:26,610
You can call it whatever you want.
76
76
00:03:26,610 --> 00:03:28,470
You could call it bob if you wanted to,
but
77
77
00:03:28,470 --> 00:03:33,450
I wanted to just be very clear that we're
talking about the other things collider.
78
78
00:03:33,450 --> 00:03:38,060
So this is the projectile, and
when we bump into anything else,
79
79
00:03:38,060 --> 00:03:41,330
we wanna know details about the anything
else, so that's the otherCollider.
80
80
00:03:42,470 --> 00:03:45,756
No we haven't even done this for
81
81
00:03:45,756 --> 00:03:51,111
a while still be printing, so Debug .Log,
82
82
00:03:51,111 --> 00:03:57,830
and then ("I hit: " what do
we hit + otherCollider-name).
83
83
00:03:57,830 --> 00:04:00,260
So we'll just make sure that
our collision is working.
84
84
00:04:00,260 --> 00:04:03,763
Before we go anywhere else on collision,
you always wanna make sure [LAUGH] it's
85
85
00:04:03,763 --> 00:04:06,320
working, or else you scratch your
head wondering what's going on.
86
86
00:04:06,320 --> 00:04:08,340
So back over into here, click on Play.
87
87
00:04:10,150 --> 00:04:13,873
Once it's finished compiling and thinking,
click on Play, open up the Console.
88
88
00:04:16,325 --> 00:04:19,599
A couple of warnings,
we're not using that, that's fine.
89
89
00:04:19,599 --> 00:04:23,960
And then when this hits here, boing,
we should get, I hit a lizard, excellent.
90
90
00:04:23,960 --> 00:04:26,470
Okay, so
collision is successfully working.
91
91
00:04:26,470 --> 00:04:27,440
That's really important for
92
92
00:04:27,440 --> 00:04:30,450
us to be confident hat we're
on top of that, good stuff.
93
93
00:04:31,570 --> 00:04:34,600
Okay, so when we have that collision
moment, basically in here,
94
94
00:04:34,600 --> 00:04:37,910
what we wanna be doing is reduce health.
95
95
00:04:37,910 --> 00:04:39,220
That's what we wanna be doing in here.
96
96
00:04:39,220 --> 00:04:40,696
So how do we go about doing that?
97
97
00:04:40,696 --> 00:04:48,146
Well, we would call some sort of say,
DealDamage method.
98
98
00:04:48,146 --> 00:04:49,631
And DealDamage(),
we haven't created that yet,
99
99
00:04:49,631 --> 00:04:50,820
it doesn't know where it's coming from.
100
100
00:04:50,820 --> 00:04:53,200
So let's go and
create all of that mechanism.
101
101
00:04:53,200 --> 00:04:56,660
So jump back over into Unity,
we'll need to make ourselves a new script,
102
102
00:04:56,660 --> 00:04:58,180
which will be called Health.
103
103
00:04:58,180 --> 00:05:00,591
Create C# script, Health.
104
104
00:05:00,591 --> 00:05:03,380
I'll just double click on Health.
105
105
00:05:03,380 --> 00:05:06,110
Okay, so let's start off by giving
ourselves a notion of how much health
106
106
00:05:06,110 --> 00:05:08,010
the particular thing will have.
107
107
00:05:08,010 --> 00:05:11,930
So SerializeField, and
then this will be a float,
108
108
00:05:11,930 --> 00:05:16,700
we'll call this health,
an issue like that 100f seems pretty good.
109
109
00:05:16,700 --> 00:05:18,480
Let's have a quick look at our checklist,
110
110
00:05:18,480 --> 00:05:20,580
we're going through the mechanism
now the Health script.
111
111
00:05:20,580 --> 00:05:23,710
We've created Health script,
serialize health, yes, create DealDamage,
112
112
00:05:23,710 --> 00:05:24,480
let's do that.
113
113
00:05:24,480 --> 00:05:28,920
So for now, I'm going to just
get rid of start and update,
114
114
00:05:28,920 --> 00:05:32,790
we put them back in if we need them,
create a public method,
115
115
00:05:32,790 --> 00:05:36,000
cuz we need to be accessing
this from the projectile.
116
116
00:05:36,000 --> 00:05:39,890
The projectile's gonna say, I hit
something, and we call the Deal Damage, so
117
117
00:05:39,890 --> 00:05:41,330
we call DealDamage.
118
118
00:05:42,630 --> 00:05:43,770
And what do we need at this point?
119
119
00:05:43,770 --> 00:05:47,020
Well we need to know how
much damage to deal,
120
120
00:05:47,020 --> 00:05:50,740
and we want that to be dependent upon
the particular projectile that's hit you.
121
121
00:05:50,740 --> 00:05:54,296
So we'll say, when DealDamage is called,
122
122
00:05:54,296 --> 00:05:59,387
we require you to let us know of
type float how much damage it is,
123
123
00:05:59,387 --> 00:06:03,440
that you need to be passing
back in to DealDamage.
124
124
00:06:03,440 --> 00:06:05,147
Okay, open our curly braces.
125
125
00:06:05,147 --> 00:06:09,310
And then within here we'll have
126
126
00:06:09,310 --> 00:06:14,110
the standard health -= damage;.
127
127
00:06:14,110 --> 00:06:16,310
So we're saying, whatever health you
have at the moment starting up for
128
128
00:06:16,310 --> 00:06:21,540
the hundred is -= damage that's just being
passed through when we call DealDamage.
129
129
00:06:21,540 --> 00:06:24,230
And then we do our standard if did.
130
130
00:06:24,230 --> 00:06:26,590
So if you think you know how to do this,
131
131
00:06:26,590 --> 00:06:32,080
then just if the health gets below zero,
destroy the game object.
132
132
00:06:32,080 --> 00:06:32,772
Just complete that.
133
133
00:06:35,102 --> 00:06:38,611
Okay, if health is <0,
134
134
00:06:38,611 --> 00:06:44,360
then We will Destroy(gameObject).
135
135
00:06:44,360 --> 00:06:46,860
So basically destroying
me is what that means.
136
136
00:06:46,860 --> 00:06:50,260
Whatever me is attached to,
whatever this health has been placed onto.
137
137
00:06:50,260 --> 00:06:56,000
So it's been reasonably good, save that
up, jump back over into projectile.
138
138
00:06:56,000 --> 00:06:59,880
Now, DealDamage,
we have a mechanism for calling that.
139
139
00:06:59,880 --> 00:07:01,191
How are we gonna do that,
140
140
00:07:01,191 --> 00:07:04,381
well we know that when we collide
with the particular thing.
141
141
00:07:04,381 --> 00:07:09,021
The otherCollider, then at that instance
we know who the otherCollider belongs to.
142
142
00:07:09,021 --> 00:07:12,480
So we don't need to go searching
through our entire project and say,
143
143
00:07:12,480 --> 00:07:16,054
hang on a minute who did I just collide
with I just collided with the thing
144
144
00:07:16,054 --> 00:07:18,670
that owns the collider,
so the otherCollider.
145
145
00:07:18,670 --> 00:07:21,990
This could be called otherObject that
currently possesses a collider, but
146
146
00:07:21,990 --> 00:07:22,880
that's a bit too long.
147
147
00:07:22,880 --> 00:07:27,400
So, let's start of by creating a variable,
which we'll call var,
148
148
00:07:27,400 --> 00:07:28,810
just a general variable.
149
149
00:07:28,810 --> 00:07:37,506
var health = otherColider.GetComponent.
150
150
00:07:37,506 --> 00:07:40,679
And component of type Health.
151
151
00:07:40,679 --> 00:07:43,867
Now we have a reference to the health
component or the health script, so
152
152
00:07:43,867 --> 00:07:45,370
just remove my comments in here.
153
153
00:07:46,490 --> 00:07:49,842
Then DealDamage, instead of just
DealDamage, we're going to say
154
154
00:07:49,842 --> 00:07:54,680
health.DealDamage, cuz we're referring
to health which is our health component.
155
155
00:07:54,680 --> 00:07:57,315
And DealDamage,
I spelled it incorrectly there.
156
156
00:07:57,315 --> 00:08:01,089
DealDamage, let me go back over
into health and see what I code it.
157
157
00:08:01,089 --> 00:08:04,910
DealDamage, and the problem is
that I'm not passing anything in.
158
158
00:08:04,910 --> 00:08:08,610
So it's saying we mass over it,
it's saying there's no argument.
159
159
00:08:08,610 --> 00:08:12,079
And that was because we said we
need to know how much damage, so
160
160
00:08:12,079 --> 00:08:14,900
thank you Visual Studio for
reminding me of that.
161
161
00:08:14,900 --> 00:08:20,777
How much damage, well, let's say we
need something a serialized damage.
162
162
00:08:20,777 --> 00:08:24,941
So we'll say damage in here,
just kind of damage amount.
163
163
00:08:24,941 --> 00:08:30,675
Up at the time
[SerializeField] float damage.
164
164
00:08:30,675 --> 00:08:36,268
Initialize that at, say, 50;.
165
165
00:08:36,268 --> 00:08:40,709
Okay, now it's happy, it's passing, it's
calling, hey, DealDamage, I want you to
166
166
00:08:40,709 --> 00:08:45,160
give 50 points of damage, or whatever
it's being changed to in the inspector.
167
167
00:08:45,160 --> 00:08:45,980
Now, let's jump over and
168
168
00:08:45,980 --> 00:08:50,142
see if we can get any mechanisms
to see what's going on here.
169
169
00:08:50,142 --> 00:08:55,320
Go to Console, clear our issues,
click on Play.
170
170
00:08:55,320 --> 00:08:57,260
Now, do we have a way to
visualize what's going on?
171
171
00:08:57,260 --> 00:09:02,400
Let's find whichever the first
lizard is that comes out.
172
172
00:09:02,400 --> 00:09:05,270
Let's grab them in our lane that is.
173
173
00:09:05,270 --> 00:09:08,580
So this guy just here
if I can click on New,
174
174
00:09:08,580 --> 00:09:12,870
excellent, has health somewhere,
we haven't added the health script.
175
175
00:09:14,110 --> 00:09:15,500
What are we crazy?
176
176
00:09:15,500 --> 00:09:17,780
That's what we are,
we need to add the health script so
177
177
00:09:17,780 --> 00:09:20,770
find your prefab of the lizard.
178
178
00:09:20,770 --> 00:09:22,900
Click on Add Component, type in health.
179
179
00:09:24,410 --> 00:09:26,090
Double click on that,
I'll single click on that.
180
180
00:09:26,090 --> 00:09:28,260
That is health, 100, yep that's fine.
181
181
00:09:28,260 --> 00:09:34,112
Now let we click on Play, I'm gonna
select one of the attackers that spawns.
182
182
00:09:34,112 --> 00:09:37,300
This first, it was probably that guy,
183
183
00:09:37,300 --> 00:09:41,500
they have already hit him that one,
yep, there we go.
184
184
00:09:41,500 --> 00:09:42,210
Already has 50 health.
185
185
00:09:42,210 --> 00:09:44,140
That first Zucchini smacked into it and
186
186
00:09:44,140 --> 00:09:46,780
then second time around,
boom, health goes to 0.
187
187
00:09:46,780 --> 00:09:53,350
Excellent, so we have cactus shooting
projectiles that's damaging the lizard.
188
188
00:09:53,350 --> 00:09:55,356
The lizard health is going down.
189
189
00:09:55,356 --> 00:09:58,281
We've done that by
creating our own trigger,
190
190
00:09:58,281 --> 00:10:03,456
where we are calling the DealDamage and
by having a mechanism that says as health,
191
191
00:10:03,456 --> 00:10:07,433
as you're hit, then decrease
your health until you get to 0.
192
192
00:10:07,433 --> 00:10:12,580
And then I noticed one little bug which
is the fact that we hit 0, so we twice
193
193
00:10:12,580 --> 00:10:18,577
on 0 health but the attacker wasn't
destroyed, and that's cuz we're saying <0.
194
194
00:10:18,577 --> 00:10:21,955
So we need to say <=0.
195
195
00:10:21,955 --> 00:10:25,595
We'll try that one more time,
click on Play,
196
196
00:10:25,595 --> 00:10:29,760
hopefully we hit this lizard,
197
197
00:10:29,760 --> 00:10:34,318
it will be once, and then he did
a second time, and it should disappear.
198
198
00:10:34,318 --> 00:10:36,318
There we go, fantastic.
199
199
00:10:36,318 --> 00:10:39,438
A few little bits and pieces we can
tidy up but that is good for this video.
200
200
00:10:39,438 --> 00:10:41,298
I think we got our core functionality in.
201
201
00:10:41,298 --> 00:10:43,978
Let's put in a little bit more of
that finesse in the next video.
202
202
00:10:43,978 --> 00:10:45,128
So see you again shortly.
19659
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.