Welcome back, my fellow game developers.
In this video, when you can see right here that we have
these green health pickups.
So when we get hit,
you notice that we are live as 500 over 600.
But once I pick up these health,
you'll notice that I increase my health and
now we're back in business and we can shoot everybody up.
So without further ado,
let's not waste any more time.
I do make a couple of mistakes in this video.
I hope you'll enjoy the lesson from
them and let's not waste any more time again.
And let's get started.
Oh, okay, so let's add the ability for
the player to pick up some health while he is damaged.
So the first thing I want to do is I
want to create the prefab out of the spike trap.
So prefab and what should be having here.
Let's right-click in here and
create a folder for the level objects.
So level objects.
And let's go ahead and add the spike trap and here,
and let's just keep it in our world.
Now, the next thing we want to do is to
go into the sprites.
Where are the sprites?
And let's say that in here we have
the health pickup or you know what,
let's just call them the PEC Ops. Hit Enter.
And in here I'm going to go into the resources and drag
the entire PNG or image or whatever you wanna call it.
And I wanted to do this outside
and do it before we start the video.
But I thought that maybe some of you might have been
angry that I created all the animations and stuff.
So I thought that let's create them together.
If you want, we can go ahead
and see who can do it faster.
So 64 by 64 or less slides that, okay,
obviously it should be
32 by 32 slice that applied changes. There we go.
Now I'm going to get one of those,
add them to the world in here.
It's extremely small.
Let's set it on the layer, layer.
It's still extremely small.
Let's make it 32 and the pixels per
unit apply the changes so it's a bit still a bit small.
Let's make it maybe 16.
Apply the changes. There we go.
I think this is good.
And by the way, this is one of the ugliest health pickups
that I've ever that I've ever seen.
So if you want now, no,
I didn't want to say that because
maybe the person that worked on
them maybe had a tough time
creating the shadows and so on.
So I'm sorry for saying that,
but honestly, they're not that good.
So I do recommend that you go find your own sprites.
All I want to do is make sure that we
have the game mechanics and place.
It doesn't have to be extremely pretty.
Anyways, we add a box collider obviously.
Let's see how big that is.
Yep, I think that is big enough.
We'll make sure it's a trigger will save that.
We'll go into the scripts in here
and let's right-click and create a C-sharp script
for the health or the health pickup. Hit enter.
And we'll create a folder which will be the pickups.
So any day now,
pickups and add the health pickup and do the pickups.
And then we'll add the health pickup
into the health pickup.
How cool is that?
So health pickup gets a new component,
which is the health pickup script.
There we go, open it up in
Visual Studio and let's get started.
So what do we need in here?
First of all, obviously we need to
know the amount that this will heal.
So serialized field, we'll make it an
integer and we'll call this the heel amount endless.
Just set it to be, I don't know, like maybe ten.
I think ten is a good enough number.
I'm going to remove the start and update for now.
And I'm going to go ahead and create an on
Trigger Enter to the
obviously what are we looking for? Compare tag.
I mean, sorry, we need
to first of all get the collision and
then we compare the tag and that tag as the player.
So you've already seen all of this,
I think about 1000 times, but anyways,
this is what we should be going through.
But before I do that,
let me just create the animation for the health pickup.
So Animation, create animation.
Let's create a folder in here which is going
to be the pickups.
Maybe we do add a bit of more pickups.
And in here we'll create
the health pickup just
to make sure that everything is really
clean and easy to go through.
So now we get the health up, down, up, down.
That go to the sprite switches on.
And where is it?
Backup, health pickup.
There we go.
Let's add all of these images
and we'll keep it as the default layer that's run.
Okay, that's an extremely fast, maybe like that.
Nope, that is boring.
Like that.
Okay. I think that is good enough maybe.
Yeah, sure. Why not? So let's just keep
it at that. I think.
Yeah, Sure.
As long as we can see what's happening
and it's just moving a little bit.
Maybe make it a bit slower.
Nope, that's 2 first.
I don't know.
This will really depend on you.
How do you want your pickup to look?
Obviously, some of you have battery sprites,
have better understanding of what
makes an animation pretty.
So I'll keep that to you as long as
we have the health pickup working properly.
Now let's go back in here.
And what do we want to do in here
after the player collides will
obviously we want to increase the health of the player,
but there is something
that is not quite right and that is,
we don't have any way of doing that.
So I'm going to issue you
a challenge to create a method that adds health.
So first of all,
and the player health handler create
a method that adds health to the player.
It should take a parameter which is the heel amount.
You'll need to make sure that it never goes
over the maximum of health that we have.
So our maximum is 100.
We don't want to have 120, for example.
And finally, and most importantly,
you need to make sure to also update the UI canvas.
So with that information,
pause the video right now and go do reach challenge.
Gay. Welcome back.
How did you get on that?
I hope it wasn't too hard
and I hope you gave it your all.
So in here where we damage the player right under,
we are going to create a public void.
And it's going to be add HB two player.
And it's going to take in an integer parameter,
which is the heel amount.
And what shall we be doing in here?
Well, we are going to be accessing the current health
and we are going to be increasing
it by the health amount.
Now for the tricky part,
if we have the current health
greater than the max health,
what should we be doing?
Well, we should be setting
the current health to be equal to the max health.
So this will ensure that the current health
never goes over the max health.
And finally, we need to
update player Health Plan, Update Player health.
Yep, Update Player health. You know what?
Double-click in here, Control RR,
Update Player health you are.
And the problem is
that there is something, one unresolved conflict.
What is the problem? Oh, okay.
So in here, it doesn't know what it should be using.
And there we go.
So now we update the player health you are.
And the last step is obviously to inhere,
we are going to go to the collision, the Get component,
because we already know this is the player,
so we won't face any errors that what was called at,
at HP to player,
at HP to player.
And we sent him the heel amount of this object.
And finally, and of course we need to
destroy the game object.
There we go. I believe this is all that we
need to do and this should be working.
So back in our game, Let's run this.
And let's see if that worked.
So we are going to stand on the spikes,
get hit once, Come on, Get him twice.
There we go.
Get hit a third time. Okay.
So now our health is 300.
So now when we pick up this,
we should have 310.
We have 320.
So why do you think that happened?
Let's see what the problem is here.
So the health pickup has done, it hasn't animation.
It has a box collider,
but the problem, Oh, welcome back.
So I believe it took me just a fraction of a
second to know what happened
because of the magic of editing.
But I went through a couple of things.
Just understand if my theory was correct and it was.
So what I was thinking is that
we have two colliders in here.
We have the capsule collider of the player,
and we have the Box Glider of the Player.
One of them is 2.
Make sure that we do not go beyond walls.
So we made one that is small so we can have
the illusion that this game is more than just a two.
These are the wall doesn't actually make
us unable to walk through it a little bit.
So if you know what I mean, I'm in here.
And then the second one is actually to be
able to get shot from the hat.
So the problem, What's happening?
I'm trying to show you,
I'm going to get hit by these spikes.
And now if I walk with these two colliders,
you'll notice that when I get to choose one of them,
you see that I get 20 because I'm
picking it up with both gliders.
If I just pick it up with the collider above,
look at the 520, you'll see that it's just 530.
If I pick it up with my head only, I get 540.
So 10, which is good.
And if I pick it up on from the side, there you go.
You can see that it added 20.
So this is the problem that we need to fix.
Now, I want you to take a moment and
think of a solution to this problem.
So I'm just going to give you,
this is not a challenge, not an efficient challenge.
I'm just going to give you a bit of time.
Okay, time's up. So let's
see what we are going to do in here.
So the whole thing that we need to make sure of is that
this health pickup has been picked up at least once.
So what we'll do is we'll create
a private bool and this is going to be called Pick it up.
So it has been picked up.
And in here we're going to say that if it's
the player and it has not been picked up,
so we add the exclamation mark,
then we are going to pick up,
but we'll also set this pickup to be true.
So once we pick it up, once,
That's it, We can no longer pick it up another time.
So let's save that.
Let's go back into our game and let's test this out.
So we run the game,
we get hit by the spikes.
And then when we walk on the sides,
there we go, we get 10.
So no matter how much we try right now,
we should never get this increasing by 20.
So now I walk through this that you go on the sides,
we always get 10 and never get an extra 20 or so.
I hope you enjoy it. I hope you'd like this.
And I really wasn't sure what the problem was solved.
Took me maybe about two to three minutes,
but I remember when I first
started this took me maybe back.
Today's to figure out what the hell was
wrong with this games or why wasn't big begin picked up?
This was a long time ago.
Now, because of my experience,
I understand things a bit
quicker and this is what I want you to know that
sometimes even though I know I have so much experience,
I'm teaching you the scores.
I also make a lot of mistakes and
things happened that I have no clue why,
even though this course actually I've
created this game beforehand and went through everything.
But I encountered this problem,
so I hope you enjoy it.
I hope you like my transparency that I
always make sure to show you the mistakes I made,
even though some people don't like it,
they say that, how can an unstructured make mistakes?
Oh no, everybody tries to be perfect.
I know nobody is.
Trust me.
Everybody uses Google to figure out what the problem was.
And this case I didn't because I've
already encountered it and fixed it beforehand,
so I hope you enjoy it.
Make sure to stage your files
and I'll see you in the next video.