With our enemy waves now working, it's time to start thinking of creative ways to kill our player,
because at the moment the enemies can just pass right through them.
And I think it would be nice before we start adding things like projectiles is to just have the player
take damage if they collide with one of the enemies.
So it almost becomes a game of dodge.
To do this, we are going to need a couple of new scripts.
But before we jump into the code side, let's set up our player and our enemy so that they can interact
with each other over in our Prefabs folder.
Let's click on both our enemy and our player because we're going to be setting up pretty much exactly
the same.
So doing it this way, we can add components to both objects rather than doing them one at a time.
Over in our inspector, let's add some components and the first thing we need is a rigid body, 2D.
And in here, the only thing we want to change for now is the body type.
We want to change it from being dynamic to kinematic.
And this is because we're moving all of our objects in code rather than relying on the physical system
with our rigid body added.
Let's go ahead and add another component and this time we're going to need a type of collider.
Now there are a few different 2D colliders that we can choose from.
So you may want to choose a box or a capsule, but I'm actually going to go with a circle.
And the reason I've chosen a circle collider here is because I think it will nicely encapsulate our
sprites, but depending on the sprites you're using, you may want to choose a different type of 2D
collider with these components added to both our enemy and our player, let's just do some fine adjustments
on each.
So I'm going to start by going into my enemy prefab and just reducing the size of this circle.
Collider A little bit, and I like to use the edit collider option.
So we highlight this and then we can just drag in these nodes to change the size.
Now this isn't an exact science and it doesn't have to be perfect, but I think somewhere around here
is going to be okay.
So if the player collides with the tips of these wings, the collision isn't going to be detected.
But it can be nice just to give the player a little bit of leeway so that game doesn't feel too unfair.
Then we're going to do the same thing on our player, so we'll open up our player prefab and again,
just change the size of our circle collider.
And again, I'm not being too precious about this.
So if we happen to clip our wings, we're not going to take any damage.
But something around here should be okay.
After all, we can always come back and adjust these later on.
We're not fixing any of this in stone just yet, and that will definitely be something we'd want to
keep an eye on during our plate testing.
So with our collider set up, there is one more thing that I've forgotten to do.
So let's select both our enemy and our player once again.
And if we go down into our circle Collider, we don't want these to be colliders.
We want them to be triggers.
And this will allow the objects to pass through one another rather than bouncing off each other.
So with that all set up now let's think about what script we're going to need.
Well, our player is going to need some health, so we could put that in our existing player script.
But since our enemy is also going to need some health, I think it makes much more sense to have a dedicated
script purely for looking after the health of an object.
So let's create that new script.
Lets right click Create C-sharp script and call this the Health script.
And this is going to be attached to both our player and our enemy.
So again, back over in our prefabs, let's select both of our prefabs.
Scroll down and add the new health component that we've just created.
And secondly, we're going to need a way of dealing damage to these objects that have health.
So for now, we just want the enemy to be able to hurt the player.
But later on, we're also going to want things like projectiles to be dealing damage as well.
So again, it makes sense to have a dedicated script that can do this.
And this kind of script reuse is really important in game development because it's going to save us
a lot of time if we've got a lot of things in our game doing very similar things.
So let's create another C-sharp script.
And this time we're going to call it the damage dealer.
Now to write both of these scripts, let's start off with our damage dealer, because this is going
to be an incredibly simple script.
It's really not going to do much at all.
We're not going to need start and update, and all this is really going to do is hold the amount of
damage the object that this is attached to is going to do.
So let's create a serialized field of type int and call this the damage value or just damage in fact.
And by default, let's give that a value of around ten.
Next, we're going to need a getter so that other scripts can access this amount of damage that needs
to be dealt.
So we're just going to need a public int get damage.
And return the damage.
Now, we may come back and add things to this later on, but at its core, this is really all it needs
to do.
So let's save our script and now let's go into our health script.
This one is going to be a little bit more complicated, but really not by much at the moment.
So again, let's get rid of the start and update.
And this script is going to hold the amount of health our object has.
So let's have another serialized field of type int called health and let's maybe give that a default
value of 50.
Now the basic logic that we're going to be following is that our damage dealer is going to come in and
hit the thing with health and then reduce that health by the damage amount.
That's why we went through the trouble of adding those triggers on our objects earlier so that we can
use the on trigger enter to DX method.
So let's write our on trigger enter to DX method.
This requires us to pass in a collider to DH and we'll call this the other collider.
And in this method we want to check whether the thing we're colliding with is a damaged dealer and if
it is, we want to take some damage.
And there are a few different ways that we might check what the other collider is.
We could use things like tags or layer masks and things like that.
But what I'm going to do is I'm just going to query whether I can get the damaged dealer component off
of that other object.
So let's create a temporary variable or type damaged dealer and we'll call this the damage dealer.
And this is going to be equal to the other dot get component of type damage dealer.
So this will check the collider that we're passing in and see whether we can grab a component of type
damage dealer.
Now if this is successful, then our damaged deal dealer variable will hold a reference to that component.
But if it fails, our damage dealer is going to be null.
So now that we've tried to get our component, we can ask if our damage dealer does not equal null or
in other words, if we do have something stored in that variable, then we want to take some damage.
And we're also going to want to tell the damaged dealer that it actually hit something so that can destroy
itself.
So let's start with this second one and tell our damage dealer that it hit something and that it should
destroy itself.
So over in our damage dealer, let's add another public method.
This can be void and we can just call this hit in here.
We want to destroy our damage dealer so our enemy that we've hit or a projectile that's later going
to be coming our way.
And we can do that with destroy game object.
So save our damage dealer, jump back into health and we can replace our comment here with damage dealer
dot hit with that sorted.
Now let's think about taking some damage and this is going to be your challenge.
I want you to write the method for taking damage.
This method that we're going to call take damage is going to pass in the amount of damage taken which
we can get from our damage dealer, reduce our health by that damage amount.
And if we run out of health, then we want to destroy the game object.
So pause the video now and give that a go.
Okay.
Welcome back.
So let's write our take damage method.
We said this wanted to pass in the amount of damage from our damage dealer.
So we can get that from our damage dealer.
Don't get damage.
So that's us calling the method, but we still need to write it.
So underneath our on trigger enter method, let's have a void, take damage.
And this is going to pass in an integer for the amount of damage in here.
We want to reduce our health by the damage being taken so we can say health minus equals the damage.
And then if our health runs out from taking this damage, we can say if our health is less than or equal
to zero, then we want to destroy our game object.
So we're taking a fairly simplistic approach for just destroying objects whenever they hit another object.
But later on we will be expanding on that by adding things like a particle system and explosion sound
and a whole bunch of other cool stuff.
But for now, this is good enough.
So let's save our script, head over into Unity and test this out.
Now, before we go and hit play, we do need to add our damage dealer to our enemy.
So in our prefabs folder let's go to our enemy prefab and add a component of type damage dealer and
our player has been given a default health of 50, the same as our enemy.
So let's give our damage stealer of four now a value of 25.
This will mean we can hit two enemies before being destroyed.
This is just for testing so we can see things in action.
And later on I'd probably want this to be a lot higher to really incentivize the player not to touch
the enemies.
But for now, that should be fine.
So let's go ahead and hit play and test this out.
And now if we collide with an enemy, it disappears.
And if we hit a second enemy, then both the player and the enemy get destroyed.
So our health and our damaged dealing system is working nicely and with a few tweaks to our enemy wave
system.
To make it more frantic, we could have a very nice game of Dodge on our hands, but this is called
Laser Defender.
And what's a game of laser defender without any lasers?
So in the next lecture, let's start adding some projectiles for the player to dodge as well and see
how our damage dealer script can be reused for other objects in the game.
So I'll see you there.