Now this player GameObject needs to take damage from this fire trap saw and our spike head that we created
up over here.
Basically, we drag it here as you can notice, and if we select all of these traps, let's suppose
if I select my fire in the hierarchy and then press down F in order to focus on that and go over to
Inspector.
As you can notice, we assign it this fire as a trap tag.
And also if we select our spike head and then go over to Inspector, we can notice we also tag it as
a trap.
And also we have done the same thing with this jaw.
So as soon as this player GameObject just gonna trigger with a GameObject or just gonna collide with
the GameObject has a tag called trap means this player GameObject need to take damage and we already
created player damage and scripting if you haven't checked that out, uh, so you can check out the
previous lecture.
Maybe something like 3130 or 32, even 34, 33.
It can be so you can check that out.
Uh, that uh, scripting part.
Because in there we created the Takedamage function.
So we need to call the Takedamage function once we're gonna trigger with this, uh, trigger with this,
uh, fire trap or this game object.
If we collide with this game object, then we're going to call the function.
So let's open up our player script double by double clicking on it.
And let's go to the bottom of our class.
Uh, you can create a separate script if you do one for your player health, you can do that.
But I'm gonna do inside my player script.
So I'm gonna move over to town somewhere over here, as you can notice.
So we using this on collision enter 2D function.
And this function get called when we collide with something.
But when we gonna trigger with something this function not gonna call at all.
And for that we need to use another function.
And we do know that function is called on trigger.
So we need to include that which is on trigger.
Enter 2D function and this on.
Make sure you just type as I did over here.
Else it's not going to work.
So this on trigger 2D function get call when we trigger with something.
So we're gonna trigger with this uh this fire trap.
So this right over here, it's just going to call right away.
And we can extract information by going over to this collision parameter.
As you can see, we have an argument says Collider 2D and it says collision.
So we can extract information with what we trigger it with.
So what we're going to do over here.
So this function get called when we're going to trigger with uh that fire or any game object that we're
looking for.
So we just want to check the tag of the game object.
If the game object has a tag called trap, then we need to damage.
So before we, uh, damage we just need to check.
So we're going to use this if condition.
Let's make first bracket and some curly brackets and what we want to check over here.
We basically want to check the game object.
We trigger it with hashtag called a trap or not.
So we're going to use this collision.
Collision dot GameObject and collision dot GameObject basically means the GameObject our player trigger
it with.
So the player definitely gonna trigger with this fire as well as this, uh, ground, you can simply
say, so what we want to do now, we just want to access the tag of the game object.
So to access the tag.
So let's type dot tag and we will check if the tag is equals equals to trap.
If that is the case basically means uh, this player game object right over here, it just trigger with
this fire trap.
And now what we have to do, we need to destroy or we need to damage our player.
But before we damage our player, let's print out a message if it's perfectly working or not.
So I'm going to go over to script.
And over here we're going to type a message something like debug dot log.
And we're going to say something like, uh, take player takes damage.
So let's say player takes damage, player takes damage.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So you will see as soon as beginner trigger with this fire game object.
And we do know that this fire we just assign it with the tag called a trap.
So we just taking that tag.
And if the tag is equals equals to trap then what we simply doing, we just printing out the message
in the console.
As you can notice this player takes damage.
So this message is just going to pop up as soon as we're going to trigger with that fire game object.
So just wait a moment and you will see.
So just wait a moment.
And as soon as we're going to trigger with this fire game object, uh, that uh, that's just going
to pop up.
So let's go to Unity.
And let's click on this play button in the top in order to play our game.
And let's trigger with this fire a game object.
And we do know that we just enabling the fire game object a box Collider 2D once it's flaming up, basically
playing the flame animation clip.
So now if we click closer to fire and if I trigger now it's not calling.
But once the flaming animation is playing and if we move over to console tab, as you can see, the
message just getting pop up says player takes damage as soon as this fire.
Just playing the flaming animation clip animation clip.
Because in that moment we just enabling our box collider 2D of this fire fire game object.
So we're gonna get out of play mode.
So instead of printing out message, we just want to destroy our player.
And we also want to show up some explosion effect.
We're going to do it later.
For now, what we want.
As soon as it's gonna trigger with this trap, we just want to set the position of this player game
object one more time over here.
And also, we need to decrease our max health of this player game object.
So what we will do, we're gonna call we're gonna move over to our script.
So instead of printing out message what we're gonna do, we basically, uh, just want to call the Takedamage
function inside this our player scape.
So we're going to call the Takedamage.
So let's call take damage.
So take damage.
We made it up over here as you can notice.
So we just made it here.
So we need to call the function.
And this function just gonna do these things.
And if you just little bit confused then you can check out previous lecture.
And it may be something like 31.
So check that out.
Uh which I name it something like player health player health or damage scripting.
So you can check out the previous lecture for that.
So now we need to call this Takedamage function.
And this function just gonna do this thing as you can notice.
So let's call the function and this function as a argument.
It's going to take the parameter.
So we're going to pass one.
So every single time we're just gonna are we just gonna lose our health by one unit.
And we're going to press down Ctrl s to save our script.
And now let's move over to unity.
And also you, we need to make a point from where we want to spawn our player.
So just wait a moment and we're going to take a look on that later.
So for a moment let's take a look on this.
So if we just go ahead and then click on this play button in the top in order to play our game.
You will see as soon as we're going to trigger with this fire game object, once this Fire game object
is playing the fire animation clip, this player is just gonna lose all his health by one unit, her
health by one unit.
Sorry.
So if I get closer to.
As you can see, we're losing health.
Then it's became one and then it's became zero.
So we just loses all of our health.
So what we want to do once we lose our health by one unit.
I just want to spawn this player game object over here one more time.
So I'm going to get out of play mode.
And let's click on this play button in the top.
And let's check the position in the y axis of this player game object.
Since we're going to make a spawn point for this player in order to spawn this player game object itself.
So that is the reason.
So we're going to check the position.
So now if I select my player GameObject in the hierarchy, then move over to Inspector Inspector tab.
And as you can notice, the position in the X is 4.6.
You can simply say and in the Y we can see minus three point -1.3.
And in the x .65.
So what we can simply do, we can just right click on this, uh, transform.
And then we can go over to copy in the bottom.
And then we can just copy copy our position.
So let's click on it.
Now we just copy it and we're going to get out of play mode.
And we will create an empty parent empty game object.
And we're going to paste the value uh and we will call something layer something like player position
spawn.
So let's go to hierarchy.
Right click or hit this plus icon.
Let's create an empty game object.
And we're going to name this game object to something like player.
Let's call it player spawn position player spawn position and make sure you go to Inspector.
Right click and reset.
And we can just paste the value in the x.
We can set 4.65.
And in the Y we can set -1.3 since we already copied the position.
So we can simply go over to this transform right click.
Then we will go to paste.
In the bottom you can see.
And then we're going to paste the position.
And as soon as we paste we can notice in the x which is 4.65.
And in the y which is minus one .1.340.
So this seems pretty much cool.
And for this I'm going to choose an icon.
So let's select the player is player spawn position in the hierarchy.
Go over to Inspector and we're going to choose an icon for that something like this.
And this is fine.
And I'm gonna drag it in the top so I can see all the time.
So we also need a reference to our player spawn position.
Then we can spawn our player in this position.
So we will open up our player.
Skip one more time and let's go to take damage function where we're taking damage.
As you can see, we're taking damage right over here and we're shaking our camera.
So once we take damage, we want to set the position of our player to that position, which is player
spawn position.
So we need a reference to that position.
So for.
It what we will do, we will go to top of our class and we're going to make a serialized private.
Uh fill you can simply say so let's make it over here.
So let's make serialized field private.
And the component we're looking for is position means we basically looking for transform which is responsible
for position.
So we're going to pass our transform.
And we can call this something like player spawn position.
So let's call it player spawn position or a position.
Or you can call player spawn point as well.
And we're going to close that off with semicolon.
And now let's move over to that line of code where we're taking damage.
So we're taking damage over here as you can notice.
So we just want to set our player position to that position.
So what we're going to do after checking our camera we're going to set transform dot position and transform
dot position over here basically means this player current position.
And we're going to set the current position equal to our player spawn position spawn position dot position.
And let's close that up with semicolon.
So we're just setting equal to player spawn position dot position.
So we're setting the current position of our player.
When it takes damage we're setting to our player position player spawn position and we accessing the
position as you can see.
So we're just setting the position.
And now what we're going to do we're going to press down control S to save our script.
And now let's move over to unity.
So what we need we need a reference to that position.
So we created a variable underneath our health title as you've seen on top of our class.
So we need to drag our player's spawn position to that field.
So we're going to have the position.
And we can just spawn it.
As you can see, as soon as our player skip gets compiled, we can see player spawn position and it
says None Transform.
So we're going to take our player spawn position and drag it over to that field.
Now, as you can notice, we have the player spawn position and we are ready to spawn it.
And I'm going to take my player spawn position and let's drag it over to this other stuff GameObject
to just keep uh, I just want to keep my hierarchy clean, so that's why.
So make sure you just create an empty parent game object, and you can just drag all of the things there
in order to make your hierarchically.
So I can simply click over here in order to collapse, as you can notice.
And I'm going to take my fire and we're gonna take it to our traps game object.
But for now, let's leave that and let's go ahead and then click on this play button.
You will notice as soon as we're going to take damage, camera will check.
And our player position of this uh, of this just going to set back to our player position, a player
spawn position as soon as we're going to take damage.
So let's get closer to, uh, to this, uh, flame.
So now if we take damage, as you can see, as soon as we just taking damage, we just moving over to
player spawn position.
And if we select our player in the hierarchy, then go over to Inspector.
We can notice the player max cell is now one.
And if we just do for the last time, we can see now.
Now it's, uh died.
As you can see player died message just uh, pops up because once we taking once we losing all of our
health.
Then we just calling the die function and die function.
Just simply printing out this message saying player died.
So I'm going to get out of play mode, and we're gonna check with this.
Uh, stop by now, we're going to check with this.
So let's click on this play button and it's gonna do nothing since we didn't script it.
Our game object.
Uh, so once we're going to collide with this show, then what we want to do, we just want to do the
same thing as we have done with this fire.
So we're just going to take damage.
So let's wait a moment and we're going to surpass.
And if we collide with the shore, as you can see nothing's happening.
So we need to do something.
So what we're going to do we're going to get out of play mode.
And for that we're going to use on collision enter 2D function.
Why?
Because if I select my game object in the hierarchy then go over to Inspector, then Circle Collider
2D.
We didn't check the Is trigger, but if I select my fire then go over to Inspector, then box Collider
2D.
We just check the trigger.
So that's why we're not going to use trigger basically on trigger into 2D function for this.
So instead we're going to use on collision in on collision into 2D function because we added in set.
Sorry if I select my so then go over to Circle Collider 2D.
As you can notice we didn't check the trigger.
So that's why we're going to use the on collision enter 2D function instead of Ontriggerenter.
So we're going to open up our player, skip one more time and let's go to our skip.
And let's go to Oncollisionenter 2D function.
So as you can see I already have it inside my script.
If you haven't then make sure you just type as you.
As I did over here.
And you can just ignore these things.
Or you can just ignore this line of code.
And now what we're going to do.
So this function get called when we collide with something.
And we do know that if we, uh, we just can we can extract information by what we colliding with this
collision parameter.
So we can access access information by going over to this collision parameter.
So what we want to do, we basically want to check if our player GameObject collide with a GameObject.
And if that GameObject has a tag called trap.
So if I say select my shop and then spike head and go over to Inspector.
As you can notice, we just assigned this game object as a trap drop tag.
So we just want to check the check the tag.
If the, uh if the tag is trapped, then that means we need to take damage and we need to set the position
back to our player spawn position.
So we're going to do the same thing.
Basically, we need to call the Takedamage function and Takedamage function.
Just going to do the res thing.
So let's go to our script.
And over here we're going to check.
So let's use this if condition.
Let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check if collision dot GameObject dot tag and collision dot GameObject.
Uh collision dot GameObject over here basically means the GameObject we collided with.
So we definitely gonna collide with that.
So GameObject and the spike head.
So collision dot GameObject over here basically means that show or that spike head GameObject.
And then we accessing the tag.
As you can notice dot tag.
And we're going to check if the tag is equals equals to trap if that is the case.
So we firstly going over to that game object means our so our spike head, then we accessing the tag
and we checking if the tag is equals equals to trap.
Now what we have to do.
And now our player game object need to take damage.
So basically we need to call this Takedamage function.
And this take damage function.
Just going to do this thing as you can notice.
First it's gonna subtract our health amount and it's going to check our camera.
And then it's going to set our player position to player spawn position dot position as you can notice.
So when I call the function over here because we're going to collide with that trap game object.
So let's call the Takedamage function over here.
Let's make first bracket.
And let's close that up with semicolon.
And this take damage as a damage amount.
It's going to take an argument.
So if we just put five right away it's gonna lose all of his health.
Since I don't want to lose all of my health in one time.
So that's why as a take damage argument.
I'm just going to pass 1 or 2, something like that.
And now we're going to press down Ctrl S to save our skip.
And now let's move over to unity.
And you will see as soon as we collide with this saw game object or that spike had the game object,
we will lose one of our health, one health, and then the player position just going to set back to
our player spawn position or where we just want to spawn it.
Now our skip just gets compiled uh compiled.
So let's click on this play button in the top in in order to play our game.
So just wait a moment and once the compilation is done we can just go ahead and check that out.
So just wait a moment.
So I'm going to collide with this saw game object.
Just wait a moment.
And if we collide with this saw game object you will see that as you can see, as soon as we collide
it, we just spawn it over here from player's pawn position, and if I select my player GameObject in
the hierarchy, then go over to Inspector.
We can see our player health now is four Maxhealth it was five and we just loses one of one of one health
as soon as we just collided with this.
So our game object.
And now let's check with the spike one.
Uh, we're gonna check with this spike.
Or you can simply assume that even if we collide with collide with this spike GameObject.
Uh, basically just gonna it's just gonna, uh, do the same thing like that.
So now let's do that.
So we're going to check it.
So firstly I'm going to pass through it.
Let's press down space key.
Let's okay.
Sorry okay.
We're just taking damage from that enemy.
And this seems pretty much cool.
Basically it's gonna work fine and that's all.
So I'm gonna get out of play mode, so make sure.
And now we're just gonna make our, uh, hierarchy a little bit, uh, clean, you can say.
So let's create an empty parent object, and we're going to name that GameObject to something like drops.
And we're going to drag all of the tracks from our hierarchy into that GameObject in order to make our
hierarchy clean.
So let's do that.
So for it we're going to go to hierarchy.
Right click or hit this plus icon.
Then create an empty game object.
And we're going to call this GameObject to something like traps.
So let's call it traps.
And let's go over to Inspector Transform.
Let's right click on it and then reset.
And we can choose icon over here.
And now I'm going to select uh the traps which is fire and the shore.
And this spike head by holding down control.
And we're gonna drag it top of this traps GameObject in the hierarchy.
And we can just click over here to collapse.
As you can notice, it's now a lot more cool.
And we can press down control S in order to save our project.
So I hope you get the idea that how you can do this.