Welcome back my fellow game developers
to another very long video.
But in this one, it is worth it.
Like every single time we get hit, we break.
We can see that we have this item.
We don't pick it up immediately,
but after a certain time, not only that,
but now when we should the enemy and he dies,
at some point, he also drops and item.
And we obviously didn't fix
the way the layers interact with each other.
But I think because this video was so long,
we didn't have much time.
So without any further or do Let's
not waste any more time and let's get started.
Oh, case.
So now we have the health pickup working,
but not very fun when
you have all the health lying around you.
You could do that.
You could just set
a couple of health pickups in your world and
then pick them up once you
find certain rooms or whatever.
But I think it's much better when you can have some kind
of mechanic that drops
the items and allows you to pick them up.
So for example, if we have the breakable right here,
these urns, if we crush them or break them,
then we will have some kind of
health pickup or when we kill a certain enemy,
then we have also the health pickup get dropped.
So let's do that first things first,
I'm going to go into the Prefabs where
we have the where is it?
Where is it the weapons,
bullets, blood, bullets, enemy.
So apparently we don't have anything.
Oh, you know what?
Let's create a new folder for the pickups.
And then we are going to drop the health pickup in there.
Great. So now we have this.
The next thing we want to do is
we're going to go into the scripts.
We are going to find
the pickups and we are going to create
a new C-sharp script is going to be called item drop.
Now, why are we doing this?
First of all, I wanted to just close this for a second.
So what I wanted to do is I wanted to immediately add
this code right here into the health pickup or I mean,
sorry, into the breakable.
Where are they? Oh, we just closed it
so I wanted to immediately add it to the breakable.
So as soon as we break,
we get a certain item.
But then I thought,
what if I wanted some enemies drop items that
wouldn't be much efficient
because we want several things.
Maybe to drop items,
we want different earns,
maybe not just the breakable,
maybe I don't know anything that drops items.
I thought it might be better to just create
a script which is the item dropper and add it
to whatever object we want to drop an item
once he dies or kills or whatever.
Okay, so let's go ahead and do that.
First thing.
First we are going to go ahead
and create a serialized field,
which is going to make sure that we have the option to
check whether he drops items,
whether he or she or whatever drops items.
The second variable I want to
create is a serialized field,
which is of type game object.
And we're going to make it an array and it's going to be
the items too drop.
Now you might be wondering, we only have
one item to drop, which is the health.
But later on we'll create a coin
and maybe even you might want to create
some kind of leveling up systems or you drop
something that travels up just like we have in Hades.
So items to drop,
we have it as an array.
Next thing we'll have as a serialized field,
there's going to be of type float
and it's going to be called
the items drop or item drop.
Chance.
And we're going to set it at 0.5.
So it's a 5050 chance.
And why do we want to have this?
Well, we don't want to have all
of our breakable or all of
our enemies dropping this the items.
So we want to have a 5050 chance of getting an item.
Great.
So now I'm going to go
ahead and remove the start and update.
We will create them if we do need them.
And I'm going to create a public void drop item.
And this will simply go ahead and
create a random integer.
And first of all, we're going to
check for a couple of conditions.
So the first one will be random.
Dot value is less than the items chance to drop.
And what is random dot value that's hover over it and
see returns a random float between 0, one.
So if we get, for example,
zero-point for it's less than 0.5, we want instantiate.
This is a great way to just
avoid the whole is less than one,
more than 0.5 or wherever this
just gives us a random value between 01.
If it's bigger, we instantiate.
If it's not, then we want.
So then we are going to go
ahead and create a random integer.
So random item number and it's going to be equal to
the random dot range and range.
And in here we are going to have a range
between 0 and the items to drop.
Dot length. So pretty simple what we're doing in here.
We already know all of this
and then we are going to go ahead and
instantiate the items to drop.
And we are going to have it as the random item number.
We're going to set it at the transform dot position
and the transform rotation.
There we go.
So I'm going to save this.
I'm going to go,
oh, and before we do that,
I'm going to also create a condition in here
that I completely forgot for some reason.
I'm going to make sure that if drops items,
then we are going to go ahead and do all of this.
And let's just add a couple of
curly brackets around it so it's better that way.
I don't like having multiple lines under
a single drops items.
Okay, great. So with that,
Don't save that back in our game.
And let's go into the breakable pot.
And that's get these a bit closed.
And then we are going to add a item dropper in here.
And as you can see,
there is a problem.
You can see that we don't have
the deserialized fields and
you can see there is something expected in here,
which is we didn't close off one of the,
okay, so there we go.
Now we save that.
Now we should see in here the great.
So I drops items,
items to droplets, just give it the health pickup.
Let's close this down or lockdown
actually go into the Prefabs.
Where is it, Where is it pickups and the health pickup.
So now we only have the health pickup.
And just for testing purposes,
I'm going to set the items to drop chance to 1.
So we always drop an item, save that.
And now the last thing that we
want to make sure of is that
in where should it be in the break bubbles?
If we go, Where is it?
Where is it burry?
2ls breakable.
And in here we are going to
check after we make sure that we,
Where is it we instantiate the object.
We also want to go ahead and check if the
Get component and item dropper.
And we are going to check and hope we
have mass little thing in here that
will do because it's a serialized field,
Let's just create a returns.
So public bool is item,
item dropper.
And we're going to just return here,
True or whatever it is, the drops item.
So this will return true if it does drop an item so we
can go back and the word was it,
okay, The breakable dot is item dropper.
For this item dropper,
once the player dashes into it,
we trigger the break,
we instantiate the parts and
we check if he is an item dropper,
then we are going to again access
the Get Component Item dropper, drop item.
There we go, save that.
And you know what, I was thinking that maybe
this if statement in here isn't that necessary.
So what let's just remove it.
And now I remember why I forgot it,
because it wasn't necessary there.
So back in here,
save that everything should be working fine.
The only thing that's still bothers me is
that if we tried to get the component,
but it's not there,
so we didn't add it to some kind of breakable pot.
Maybe that will cause a problem if it does,
the best way to fix this is to
create an extra if condition,
an extra layer in here of protection
if the Get Component Item dropper,
and it's different than null,
that means that we do have this item right here.
Then we can go ahead and check for the item dropper.
So this will give us an extra layer of
protection if we don't have the component item dropper,
if it's equal to null,
then we are going to check if we can't drop items,
and then we are going to actually drop an item.
So save that back now again, let's see.
Now this is like, Honestly,
I don't like this a lot because we
should be knowing how we are creating this game,
Bu, but this is a worst-case scenario.
So run the game.
And now when we try to break a part,
let me just get a destroyed.
And then when we try to break
the part that you see, there we go.
We destroyed it.
And you can see that our health just
move into from 500 to 510.
But there is a problem.
Did you see it?
Did you see the item get dropped?
Nope, you didn't, but the health increased.
So what is the problem?
Well, the problem here is that it's extremely
fast and we don't want this to happen instantaneously.
We actually want to see the item that we've
dropped before we immediately pick it up.
So I'm going to issue it to you as a challenge.
And your challenge is to delay the time
before we can pick up an item.
So first of all, create a new script.
Pick up the layer again.
Why is this? This is
an amazing video for how you structure your game.
Because pick up the layer,
this will enable us to
delay the time before we pick up an item.
And we don't want to add this delaying just for
the health script and then have to copy it
every single time and to all of the coins,
the power-ups and et cetera.
So what we'll do is just create a new script of
pickup the layer and added to
every single item that we want to pick up,
which is much easier and healthier when it comes to
game development and game design structure and so on.
That's why we create a new script.
Then you'll create a float
for the time before we can pick up,
you need to create a Boolean variable that checks
if we can actually pick up the item or not.
Create a core team that after time,
after a certain amount of time, which is the,
obviously the float variable
recreated in here allows us to pick up the item.
And finally, where should we were?
Should we can call this coating?
Where should we be calling the school routine?
So obviously,
good thing I'm not good at coding because I'm
horrible at grammar and everything else anyways,
you know, what's the point of the last sentence?
Pause the video right now and go to the challenge.
Okay, welcome back.
So one thing that I wanted to do is
I want to go into the health pickup.
Nope, not the health pickup, the breakable part.
And I'm going to apply
the changes to all of the other parts,
but make sure that whenever you do add a pot,
that you make sure to
check whether it drops an item or not
and you have to change the items drop chance obviously.
And electron, of course we will be changing that.
So always keep that in mind when adding new bots.
And then we need to go into the,
where is it the room, the enemies.
We have a couple of enemies.
Let's just check this one and add to him item dropper.
There we go.
So he drops an item.
What is the item that he drops?
It will be That's why we always lock it down.
Then we add the item that's set it as 11.
Should we be dropping an item?
Let's go into the NMI controller in here.
Where does he die?
Damage enemy if health is less than 0,
instantiate the straw game objects.
So the Deaf splatter.
We will also check in here if the component, now what?
Whereas the breakable, we copy this one.
And then we just pasted in here.
So this is redundant code.
But I'm going to let it slide because of all of
the other efficient things that we've created.
So we check if we do have an item dropper,
then we check if he is a dropper,
and then we go ahead and drop the item.
So let's try to test this
out on the skeleton enemy will apply the changes.
So now all the enemies should have
the script to run the game.
And I know this was not at all part of the challenge,
but I just want to test it out so we should the bullets
finally he dies and we can pick
up an item that see if it works.
I get it.
I pick up the item. Excellent.
Now the next thing is to delay,
which is the actual challenge.
So let's go into the,
I know this was a side note,
but maybe you might have not
exactly knew what to do in
this case. No worries about that.
Let's go into the scripts.
The pickups, and right-click in here,
create a new C Sharp script,
which is going to be the PEC.
Pick up, the big,
oops, Pick up the layer, hit Enter.
Wait for this to compile and double-click in here.
Double-click in here.
And let's see what we are going to do.
First of all, we're going to create
the serialized field for the float,
which is the time before pick up.
And let's just set it at around 0.5 seconds.
Then we are going to create a private bool
can be picked up.
Save that now and start.
We are going to make sure that can be
picked up as equal to false.
And then we are going to create the Coubertin And here,
which is going to be the eye enumerator.
And it's going to be called the pick-up, cool down.
Yeah, sure, pick up, cool down.
And then in here we are going to yield, return, new.
Wait four seconds.
And I'm pretty sure by this point, you know,
co routines so well that you are bored of
me every time saying yield return you for weight seconds.
You're probably saying Michael,
we already know how to do this.
Please stop.
Nope.
We're going to learn.
He has Gantt be picked up is equal back to true.
Save that.
And now we are going to go into the item dropper.
And I'm sorry, I mean, the health pickup.
And in here we're going
to check if it has been picked up.
And we're also going to check.
And if the Get component,
the picked up, the layer,
that what should it be called?
Where is it?
Can be picked up? Let's go ahead and return this.
So public boolean, it can be picked up.
And we're just going to return.
Can be picked up variable.
Okay, not very creative name for this one, but it works.
And here is some kind of problem that
type vector definition can be picked up.
Okay?
So can, can be picked up.
Sure, let's say has called a method.
So I believe it's just because of the name.
So you can't name the variable just as you
name the method that you want.
So we'll just call it method
because we have the imagination of
a small fruit fly and then can be picked up method.
And this should do the trick.
Now the last thing and the last part of
this challenge was where do we call the Colton?
I hope you thought about that and I hope to figure that
out that it should be called immediately and start.
So the start, we start the co-routine.
That is can be, Nope, I mean,
pick up, cool down,
pick up, cool down. There we go.
So why and start?
Because we are instantiating the item.
So as soon as we instantiate the item,
it has the pickup D layer,
we immediately start the Colton.
So back in our game.
Let's see in here, the layer.
Pick up the layer.
How many, how long does it wait?
And I'm going to apply the changes to everyone.
So now when we run the game,
we should see that as soon as we
hit the break bubbles, there we go.
We hit the brake bolts.
We don't pick up immediately the item.
We see it getting dropped.
There we go.
Yep. You can see right now that it's still there.
We haven't picked it up, but now we can,
let's try something different.
Let's add the delaying to
around three seconds and see if it works.
In real life.
We're going to get hit.
We are going to destroy this and try to pick it up.
Okay, so that worked pretty
fast and I'm wondering what the problem is here.
Oh, okay, welcome back.
So the biggest mistake that has ever been made,
I changed this health pickup to 10,
but the actual prefab hasn't changed.
So let's apply the changes.
I was wondering everything is working smoothly,
but for some reason this wasn't working.
So there we go with the steroid and now we have 10,
10 seconds before we can actually pick it up,
but we are going to set it to about 0.5. Save that.
I hope you enjoy it.
I hope you liked
all the different mechanics
and small tweak things and the
adding and removing and making sure
that we can apply different things, different items.
So I know this was a long video.
Thank you for your attention.
Thank you for staging all of your files every
single time and making
all of the progress that you've made.
I also thank you for the refute,
amazing amazing review that you've left
me and I'll see you in the next video.