1
00:00:03,969 --> 00:00:09,730
With our enemy waves now working, it's time to start thinking of creative ways to kill our player,

2
00:00:09,730 --> 00:00:13,090
because at the moment the enemies can just pass right through them.

3
00:00:13,090 --> 00:00:17,680
And I think it would be nice before we start adding things like projectiles is to just have the player

4
00:00:17,680 --> 00:00:20,650
take damage if they collide with one of the enemies.

5
00:00:20,650 --> 00:00:22,840
So it almost becomes a game of dodge.

6
00:00:23,230 --> 00:00:26,110
To do this, we are going to need a couple of new scripts.

7
00:00:26,110 --> 00:00:31,360
But before we jump into the code side, let's set up our player and our enemy so that they can interact

8
00:00:31,360 --> 00:00:34,100
with each other over in our Prefabs folder.

9
00:00:34,120 --> 00:00:39,190
Let's click on both our enemy and our player because we're going to be setting up pretty much exactly

10
00:00:39,190 --> 00:00:39,880
the same.

11
00:00:39,880 --> 00:00:45,130
So doing it this way, we can add components to both objects rather than doing them one at a time.

12
00:00:45,520 --> 00:00:51,490
Over in our inspector, let's add some components and the first thing we need is a rigid body, 2D.

13
00:00:51,580 --> 00:00:55,540
And in here, the only thing we want to change for now is the body type.

14
00:00:55,540 --> 00:00:59,170
We want to change it from being dynamic to kinematic.

15
00:00:59,170 --> 00:01:04,450
And this is because we're moving all of our objects in code rather than relying on the physical system

16
00:01:04,599 --> 00:01:06,130
with our rigid body added.

17
00:01:06,130 --> 00:01:11,080
Let's go ahead and add another component and this time we're going to need a type of collider.

18
00:01:11,620 --> 00:01:14,980
Now there are a few different 2D colliders that we can choose from.

19
00:01:14,980 --> 00:01:19,930
So you may want to choose a box or a capsule, but I'm actually going to go with a circle.

20
00:01:20,110 --> 00:01:24,790
And the reason I've chosen a circle collider here is because I think it will nicely encapsulate our

21
00:01:24,790 --> 00:01:30,130
sprites, but depending on the sprites you're using, you may want to choose a different type of 2D

22
00:01:30,130 --> 00:01:35,950
collider with these components added to both our enemy and our player, let's just do some fine adjustments

23
00:01:35,950 --> 00:01:36,580
on each.

24
00:01:36,580 --> 00:01:42,760
So I'm going to start by going into my enemy prefab and just reducing the size of this circle.

25
00:01:42,760 --> 00:01:46,600
Collider A little bit, and I like to use the edit collider option.

26
00:01:46,600 --> 00:01:51,460
So we highlight this and then we can just drag in these nodes to change the size.

27
00:01:51,850 --> 00:01:57,100
Now this isn't an exact science and it doesn't have to be perfect, but I think somewhere around here

28
00:01:57,100 --> 00:01:58,210
is going to be okay.

29
00:01:58,210 --> 00:02:03,430
So if the player collides with the tips of these wings, the collision isn't going to be detected.

30
00:02:03,430 --> 00:02:09,009
But it can be nice just to give the player a little bit of leeway so that game doesn't feel too unfair.

31
00:02:09,280 --> 00:02:14,920
Then we're going to do the same thing on our player, so we'll open up our player prefab and again,

32
00:02:14,920 --> 00:02:17,860
just change the size of our circle collider.

33
00:02:18,930 --> 00:02:20,880
And again, I'm not being too precious about this.

34
00:02:20,880 --> 00:02:24,630
So if we happen to clip our wings, we're not going to take any damage.

35
00:02:24,630 --> 00:02:27,030
But something around here should be okay.

36
00:02:27,060 --> 00:02:29,820
After all, we can always come back and adjust these later on.

37
00:02:29,820 --> 00:02:33,690
We're not fixing any of this in stone just yet, and that will definitely be something we'd want to

38
00:02:33,690 --> 00:02:35,730
keep an eye on during our plate testing.

39
00:02:36,090 --> 00:02:39,740
So with our collider set up, there is one more thing that I've forgotten to do.

40
00:02:39,750 --> 00:02:42,990
So let's select both our enemy and our player once again.

41
00:02:43,170 --> 00:02:47,910
And if we go down into our circle Collider, we don't want these to be colliders.

42
00:02:47,910 --> 00:02:49,880
We want them to be triggers.

43
00:02:49,890 --> 00:02:54,540
And this will allow the objects to pass through one another rather than bouncing off each other.

44
00:02:54,930 --> 00:02:59,490
So with that all set up now let's think about what script we're going to need.

45
00:02:59,730 --> 00:03:05,550
Well, our player is going to need some health, so we could put that in our existing player script.

46
00:03:05,700 --> 00:03:11,640
But since our enemy is also going to need some health, I think it makes much more sense to have a dedicated

47
00:03:11,640 --> 00:03:14,790
script purely for looking after the health of an object.

48
00:03:15,030 --> 00:03:16,980
So let's create that new script.

49
00:03:16,980 --> 00:03:21,420
Lets right click Create C-sharp script and call this the Health script.

50
00:03:21,450 --> 00:03:25,690
And this is going to be attached to both our player and our enemy.

51
00:03:25,710 --> 00:03:30,150
So again, back over in our prefabs, let's select both of our prefabs.

52
00:03:31,040 --> 00:03:34,940
Scroll down and add the new health component that we've just created.

53
00:03:35,090 --> 00:03:40,590
And secondly, we're going to need a way of dealing damage to these objects that have health.

54
00:03:40,610 --> 00:03:44,600
So for now, we just want the enemy to be able to hurt the player.

55
00:03:44,750 --> 00:03:49,730
But later on, we're also going to want things like projectiles to be dealing damage as well.

56
00:03:49,730 --> 00:03:53,510
So again, it makes sense to have a dedicated script that can do this.

57
00:03:53,510 --> 00:03:58,610
And this kind of script reuse is really important in game development because it's going to save us

58
00:03:58,610 --> 00:04:02,840
a lot of time if we've got a lot of things in our game doing very similar things.

59
00:04:03,200 --> 00:04:05,840
So let's create another C-sharp script.

60
00:04:07,040 --> 00:04:09,710
And this time we're going to call it the damage dealer.

61
00:04:11,980 --> 00:04:17,410
Now to write both of these scripts, let's start off with our damage dealer, because this is going

62
00:04:17,410 --> 00:04:19,510
to be an incredibly simple script.

63
00:04:19,510 --> 00:04:21,630
It's really not going to do much at all.

64
00:04:21,640 --> 00:04:27,220
We're not going to need start and update, and all this is really going to do is hold the amount of

65
00:04:27,220 --> 00:04:30,520
damage the object that this is attached to is going to do.

66
00:04:30,550 --> 00:04:38,230
So let's create a serialized field of type int and call this the damage value or just damage in fact.

67
00:04:38,260 --> 00:04:41,470
And by default, let's give that a value of around ten.

68
00:04:41,770 --> 00:04:46,600
Next, we're going to need a getter so that other scripts can access this amount of damage that needs

69
00:04:46,600 --> 00:04:47,460
to be dealt.

70
00:04:48,190 --> 00:04:52,150
So we're just going to need a public int get damage.

71
00:04:54,660 --> 00:04:56,130
And return the damage.

72
00:04:57,360 --> 00:05:02,370
Now, we may come back and add things to this later on, but at its core, this is really all it needs

73
00:05:02,370 --> 00:05:02,970
to do.

74
00:05:03,180 --> 00:05:07,200
So let's save our script and now let's go into our health script.

75
00:05:07,730 --> 00:05:12,220
This one is going to be a little bit more complicated, but really not by much at the moment.

76
00:05:12,230 --> 00:05:15,140
So again, let's get rid of the start and update.

77
00:05:15,140 --> 00:05:18,710
And this script is going to hold the amount of health our object has.

78
00:05:18,710 --> 00:05:25,790
So let's have another serialized field of type int called health and let's maybe give that a default

79
00:05:25,790 --> 00:05:27,470
value of 50.

80
00:05:27,860 --> 00:05:33,470
Now the basic logic that we're going to be following is that our damage dealer is going to come in and

81
00:05:33,470 --> 00:05:38,060
hit the thing with health and then reduce that health by the damage amount.

82
00:05:38,450 --> 00:05:43,610
That's why we went through the trouble of adding those triggers on our objects earlier so that we can

83
00:05:43,610 --> 00:05:46,130
use the on trigger enter to DX method.

84
00:05:46,130 --> 00:05:49,490
So let's write our on trigger enter to DX method.

85
00:05:51,050 --> 00:05:57,110
This requires us to pass in a collider to DH and we'll call this the other collider.

86
00:05:57,980 --> 00:06:03,590
And in this method we want to check whether the thing we're colliding with is a damaged dealer and if

87
00:06:03,590 --> 00:06:05,720
it is, we want to take some damage.

88
00:06:05,750 --> 00:06:09,980
And there are a few different ways that we might check what the other collider is.

89
00:06:09,980 --> 00:06:13,880
We could use things like tags or layer masks and things like that.

90
00:06:13,880 --> 00:06:19,160
But what I'm going to do is I'm just going to query whether I can get the damaged dealer component off

91
00:06:19,160 --> 00:06:20,300
of that other object.

92
00:06:20,300 --> 00:06:25,790
So let's create a temporary variable or type damaged dealer and we'll call this the damage dealer.

93
00:06:26,030 --> 00:06:32,420
And this is going to be equal to the other dot get component of type damage dealer.

94
00:06:32,870 --> 00:06:38,910
So this will check the collider that we're passing in and see whether we can grab a component of type

95
00:06:38,930 --> 00:06:39,800
damage dealer.

96
00:06:40,040 --> 00:06:45,980
Now if this is successful, then our damaged deal dealer variable will hold a reference to that component.

97
00:06:45,980 --> 00:06:49,820
But if it fails, our damage dealer is going to be null.

98
00:06:49,940 --> 00:06:58,070
So now that we've tried to get our component, we can ask if our damage dealer does not equal null or

99
00:06:58,070 --> 00:07:04,850
in other words, if we do have something stored in that variable, then we want to take some damage.

100
00:07:05,450 --> 00:07:10,730
And we're also going to want to tell the damaged dealer that it actually hit something so that can destroy

101
00:07:10,730 --> 00:07:11,600
itself.

102
00:07:11,600 --> 00:07:17,420
So let's start with this second one and tell our damage dealer that it hit something and that it should

103
00:07:17,420 --> 00:07:18,530
destroy itself.

104
00:07:18,680 --> 00:07:22,550
So over in our damage dealer, let's add another public method.

105
00:07:23,030 --> 00:07:28,130
This can be void and we can just call this hit in here.

106
00:07:28,130 --> 00:07:33,770
We want to destroy our damage dealer so our enemy that we've hit or a projectile that's later going

107
00:07:33,770 --> 00:07:35,060
to be coming our way.

108
00:07:35,090 --> 00:07:38,720
And we can do that with destroy game object.

109
00:07:39,350 --> 00:07:46,280
So save our damage dealer, jump back into health and we can replace our comment here with damage dealer

110
00:07:46,280 --> 00:07:48,770
dot hit with that sorted.

111
00:07:48,770 --> 00:07:53,330
Now let's think about taking some damage and this is going to be your challenge.

112
00:07:53,750 --> 00:07:56,960
I want you to write the method for taking damage.

113
00:07:56,960 --> 00:08:02,810
This method that we're going to call take damage is going to pass in the amount of damage taken which

114
00:08:02,810 --> 00:08:08,360
we can get from our damage dealer, reduce our health by that damage amount.

115
00:08:08,360 --> 00:08:12,380
And if we run out of health, then we want to destroy the game object.

116
00:08:12,380 --> 00:08:15,170
So pause the video now and give that a go.

117
00:08:20,540 --> 00:08:20,960
Okay.

118
00:08:20,960 --> 00:08:21,720
Welcome back.

119
00:08:21,740 --> 00:08:24,200
So let's write our take damage method.

120
00:08:24,290 --> 00:08:28,600
We said this wanted to pass in the amount of damage from our damage dealer.

121
00:08:28,610 --> 00:08:31,610
So we can get that from our damage dealer.

122
00:08:31,820 --> 00:08:33,320
Don't get damage.

123
00:08:34,400 --> 00:08:37,700
So that's us calling the method, but we still need to write it.

124
00:08:37,700 --> 00:08:43,100
So underneath our on trigger enter method, let's have a void, take damage.

125
00:08:44,120 --> 00:08:49,700
And this is going to pass in an integer for the amount of damage in here.

126
00:08:49,700 --> 00:08:56,810
We want to reduce our health by the damage being taken so we can say health minus equals the damage.

127
00:08:57,470 --> 00:09:05,240
And then if our health runs out from taking this damage, we can say if our health is less than or equal

128
00:09:05,240 --> 00:09:09,530
to zero, then we want to destroy our game object.

129
00:09:09,890 --> 00:09:15,500
So we're taking a fairly simplistic approach for just destroying objects whenever they hit another object.

130
00:09:15,500 --> 00:09:20,840
But later on we will be expanding on that by adding things like a particle system and explosion sound

131
00:09:20,840 --> 00:09:22,550
and a whole bunch of other cool stuff.

132
00:09:22,580 --> 00:09:24,320
But for now, this is good enough.

133
00:09:24,320 --> 00:09:28,910
So let's save our script, head over into Unity and test this out.

134
00:09:29,150 --> 00:09:34,370
Now, before we go and hit play, we do need to add our damage dealer to our enemy.

135
00:09:34,370 --> 00:09:42,020
So in our prefabs folder let's go to our enemy prefab and add a component of type damage dealer and

136
00:09:42,020 --> 00:09:46,220
our player has been given a default health of 50, the same as our enemy.

137
00:09:46,220 --> 00:09:51,410
So let's give our damage stealer of four now a value of 25.

138
00:09:51,440 --> 00:09:54,680
This will mean we can hit two enemies before being destroyed.

139
00:09:54,710 --> 00:09:57,560
This is just for testing so we can see things in action.

140
00:09:57,560 --> 00:10:03,530
And later on I'd probably want this to be a lot higher to really incentivize the player not to touch

141
00:10:03,530 --> 00:10:04,400
the enemies.

142
00:10:04,730 --> 00:10:06,350
But for now, that should be fine.

143
00:10:06,350 --> 00:10:08,870
So let's go ahead and hit play and test this out.

144
00:10:10,860 --> 00:10:13,830
And now if we collide with an enemy, it disappears.

145
00:10:13,830 --> 00:10:18,380
And if we hit a second enemy, then both the player and the enemy get destroyed.

146
00:10:18,390 --> 00:10:25,020
So our health and our damaged dealing system is working nicely and with a few tweaks to our enemy wave

147
00:10:25,020 --> 00:10:25,410
system.

148
00:10:25,410 --> 00:10:31,170
To make it more frantic, we could have a very nice game of Dodge on our hands, but this is called

149
00:10:31,170 --> 00:10:32,130
Laser Defender.

150
00:10:32,130 --> 00:10:35,280
And what's a game of laser defender without any lasers?

151
00:10:35,280 --> 00:10:40,740
So in the next lecture, let's start adding some projectiles for the player to dodge as well and see

152
00:10:40,740 --> 00:10:44,580
how our damage dealer script can be reused for other objects in the game.

153
00:10:44,820 --> 00:10:46,230
So I'll see you there.

