Hello guys.
Welcome to the video.
So in this one I want to show you another example of Polish.
I just want to improve this game a little bit more.
And I want to make player drop the fruits when he gets damaged.
Not on the counter only, but also there are going to be a fruit created in the game and it's going
to blink and fly away.
And once it stopped blinking, it means player can pick up the fruit again.
So if he is fast enough, he can catch up the fruits that he lost because of some damage.
That's what we're going to do to make it easier to test.
I'm going to maybe change the scene.
Let me see.
What can we use here?
Well, this scene should work.
I only want to make it maybe a bit prettier by placing something over here.
Just, you know.
Some things.
Uh, some things like that.
Okay, now let me find the fruit.
I want to take this fruit and this is going to be our test subject.
Okay.
So for this fruit we're going to need a boolean that will say can you pick up fruit or not.
Because if we create fruit at the position of a player, he will enter the trigger immediately and he'll
pick up the very same fruit.
So first let's make a prefab and let's make another script that will inherit from the fruit.
I'm going to go to scripts.
I'm going to make script fruit and the name will be a little bit ugly, but I think we can accept it
because it's going to have lots of meaning in it.
Fruit dropped by player.
Okay, let's open the script.
It is over here.
And before we go any further, let's do close all but this and let's go and find fruit script.
And over here I want to make animator available for child script.
So I'm going to do protected.
And also we're going to need reference to sprite renderer SR.
Because if we need to make object transparent, we just need to change alpha of the color which is on
sprite renderer.
Let's go to start now awake and over here I'm going to do.
SR getcomponent in children.
Nice.
We're going to need to override start because we want to start a coroutine later on in the start.
So I'm going to do protected virtual.
Virtual void.
And we're going to need to override on trigger enter.
So I'm going to do protected virtual void here.
Very nice.
Now let's save this.
Let's go back and we're going to inherit from the fruit.
And we're going to need a boolean private bool can pick up.
And then we're going to override on trigger enter.
And if can pick up is false then we're going to return.
So this fruit is not picked uppable until you change the boolean.
Nice.
Very nice.
Now, let's not do anything else for now.
Let's go ahead and make player drop these fruits on damage.
And for that I want to use Object Creator.
Since we have the functionality here, I don't see the point of repeating the code in the player as
well.
The only thing I want to do is to add optional, um, destroy part of the object.
So we're going to pass a boolean here.
Bool should be destroyed.
Then over here we're going to do check if should be destroyed then destroy new object after let's say
15 seconds.
It's pretty enough time.
And if fruit will fly off the level it will be destroyed after some time and it will not clutter up
the game.
Although I don't think it's important for this type of game because levels are very short.
And even if you create 1000 of fruits during your gameplay, it will not overload your system.
But it's a good practice in general to keep an eye on this.
Now let's parse this boolean over here.
Bull should be destroyed and I'm going to make it false by default.
So if you want to destroy the object, you need to pass a true over here.
It should be destroyed.
Nice.
Now let's go to player itself.
Somewhere above, I'll just make a reference to the game object.
Uh.
Sterilized field.
Private game object.
Um.
Player.
Fruit.
Or fruit drop.
Yeah.
Fruit drop.
Nice.
And if you want to make this functionality for enemies, you can do that in a very simple way.
You just do it as I'm doing it with the player, but call it in a different place.
For example, here we're going to make a fruit when player getting damage, but for the enemies, you
can drop a fruit when you like when enemies die.
So it's not that difficult to do.
I guess you can manage.
So over here, when we're getting damage and if we have fruits, we're going to do object creator dot
instance.
Read object over root drop.
A transform of the player.
We don't need any delay, but we want it to be destroyed.
So I'm going to pass true over here.
Okay.
If you don't want to pass zero over here, you can go back to, uh, Object Creator and you can place
this boolean before the.
Delay like that.
You don't need to change anything else unless you want it to be pretty, and I want it to be pretty,
so I'm going to do that.
I'll just do it like that.
Okay?
Now we can go back to player and only pass true, because that will be zero by default.
But if you want to have delay, just type it in here.
Although that may create an error in other place.
Are we going to see it.
Anyway, this now supposed to create a fruit when we get in damage?
Let's go back to the game.
Okay.
There is an error.
Let me just.
Uh, pass.
Pulse and cool down.
All right.
This is how it's going to be.
Uh, let's go back.
Let's rename this to fruit.
I'm going to do prefab unpack.
Completely, then route dropped by player.
Then we're going to remove this component over here.
And we're going to add fruit drop by player.
Okay.
Now let's assign pick up a fix over here prefab.
And we fix.
Pick up a fix.
Okay.
Now we can go to prefabs.
Let's make it as a prefab.
Let's find player and give it to this field over here.
Now I'm going to apply overrides and I want to go to managers find difficulty manager and make difficulty
normal by default.
Because, uh, game manager will create difficulty manager for me.
And I need normal difficulty to be there by default.
Okay, so difficulty is normal.
I'm supposed to get damage and I'm supposed to drop a fruit.
Let's try.
Oh, I had zero.
Okay, let me pick up some.
I have five.
And.
I don't see an object created.
Why not?
Player has it here.
This is the one.
Why do we not create an object?
Okay, maybe there is a different difficulty on.
No, wait, we deducted the value, right?
Huh?
Hello.
Oh, okay.
No reason to be confused.
Is just me being dumb.
Let's go to player and we remove fruit over here.
So we supposed to create an object over here?
Okay, let's save this and let's go now.
Gonna pick up some.
And then you and we're creating a fruit when we get in damage.
Perfect.
Now our goal is to make it pick uppable.
And also our goal is to make it blink.
There's going to be pretty easy to do on the fruit over here.
We have this color and we can change transparency with a delay like that.
And it's going to blink.
Now let's go to the script and I'm going to explain what we're going to do here.
Uh, we want to have a bunch of values for the delay, because I want blinking to be fast.
Fast and slower.
Slower and slow.
Slow.
That's what I want to do.
If that's the case, then we need to have a set of different values.
But I'm going to do serialize field.
Private float array.
Wait.
Uh, time.
Okay.
And this array will have different values.
Then we're going to make a coroutine.
Private enumerator.
A numerator.
Um, blink routine.
And in here, we just need to make something very simple.
We need to stop the animator because we don't want to play animation of the fruit, and we don't want
to disable the animator or change the animation.
We can just do anim speed equals to zero and this will pause the animation.
Once we start the coroutine.
I mean it will not pause it, it will play it, but it will have speed over zero.
And technically it will not be paused.
But still, because of the zero speed, it will not be played.
I hope you're getting me here.
Yeah.
Now let's go and make for each loop.
And we're going to have.
For each float seconds in weight time.
And here we're going to need a method.
Private void toggle color.
And we're going to pass color over here color color.
And then SR dot color equals to color.
Okay, now how it's going to work.
We're going to use for each loop, for each value in the array of floats.
And over here we're going to do toggle color.
We're going to make it transparent.
And we don't have value for transparent color.
Let's make it serialize field.
Private color.
Transparent color.
So we pass in a transparent color.
Then we ask it to wait for seconds that we currently using in the for each cycle.
So it's going to be yield return new wait for seconds of seconds.
Then we're going to toggle color to color dot white.
And this will basically make it visible and not transparent.
Then we're going to wait again for a seconds and then once for each loop is over, we're going to do
anim dot speed equals to one and can pick up equals to true.
Now what it does how it works.
Let me explain.
We have for each loop that will do action for each value that we have in the array.
And let's say first value is 0.1 second.
We start for each loop.
And this value will be passed here and here.
And we do toggle color.
Then you know that coroutine exposes the functionality.
So we will post functionality of this coroutine as well as a for each loop we'll wait for 0.1.
Then we make it white.
Then we make it 0.1 and then it will enter.
Uh, like the second circle of the circle, it will take second value in the array.
Do the same for another delay.
And because we're using coroutine again, we can pause the, um.
Method, and that will make it work like a blinking effect with a delay, because every time we enter
the cycle, we use a new value from the array.
And that's it.
Basically.
Yeah.
So it's like simple for each.
But because we're using yield return new, wait for seconds, it's going to wait before the next step.
And thanks to that it's going to look like we doing something with a delay or with a cooldown.
All right.
Let's go to start method.
Actually we need to override start.
Override start.
Oh, I'm so sorry.
Override start and then start.
Coroutine of a blink.
Coroutine.
Nice.
Now let's go ahead.
Sorry, I'm a bit worried.
This can be confusing, so I'll explain once again.
This will work for each element and it will take them one by one.
So, uh.
Uh, let's go here and let's.
Make a couple of values here.
So wait time.
Let's say it's 0.1. 15.2.
And let's go back and.
And this for each loop will do everything for each element in this array, and it will take the first
element point one.
It will be applied here until the functionality is done.
It will not enter the second loop for second element, and functionality will not be done until we wait
for seconds, because this is a coroutine and we can pause it once, it will do two steps with a delay.
It will go and enter cycle for the second element, and again the second element will be applied here
with the delay.
Then it will apply delay again.
Then it will go for the third cycle.
And until we have values in this array.
So that's how for each loop works in a coroutine.
And now let's go here we need to decide what's the transparency.
Color will be here.
It should be white so we can see it.
But the transparency we need to decide.
Now let's go to animator open color and see the transparency we want to have.
Probably something like 75 will do.
Yeah, so we can make it fully visible.
Then let's go to fruit Drop by player and let's make transparency I mean alpha 75 here like that.
So this will be used for the transparent color in a for loop I'm going to add a couple of more values
maybe 25 and point three in total.
It should be something like one second.
Let me check.
Point, 3.5. 6.85 and then one.
So in total it is one second.
Okay, should be enough.
And let's go.
Now let's pick up some fruits and then let's get damage.
Okay.
It is picked up immediately for some reason.
So let me check the script.
This one happened here.
We have a prefab that we drop on the player, and this prefab has wait time zero in the array.
Because of that, this coroutine does not wait for anything.
It just makes can pick up true immediately.
Because float time is empty and.
And we do not wait.
So that's the problem.
Let's go here and let's apply overrides.
Now it's going to be on the prefab as well.
And it's going to work.
And we do need to make this visible anymore.
Okay.
And I don't need this debug.
Wow, this was interesting.
All right, let's go to play mode.
They are the fruits and there is a fruit blinking, and then it is ready to be picked up.
Okay, I like it.
Now let's make it move up while it is blinking.
This is what we're going to do.
We're going to make.
A surplus field.
Private vector two velocity.
And I know it's not going to be big.
It's going to be just one and one maybe.
But I want to have a variable because maybe you want to adjust the speed.
Then I'm going to make update in which we're going to do.
And transform.
Position plus equals new vector three.
And over here we're going to give it velocity dot x.
And velocity dot y like that.
We don't need to pass the Z because it's going to be zero by default.
And let's just multiply it by time dot delta time.
So it is frame rate independent.
And we want to not only send it up, but also we want to toggle speed over here.
So I'm going to do toggle speed.
And color.
And inside of this method we're going to do velocity dot x equals to velocity dot x multiplied by minus
one.
Because of that, it's going to move left and right while it's flying away.
And it's going to look really nice telling you.
And also once we're done with the coroutine I want to make velocity dot x equals to zero.
So it will not move left and right anymore.
Yes.
Okay.
Let's go.
Let's go now.
Uh, let's take it.
Let's make one and one.
Don't set value too big on the way because it will fly very fast.
Maybe make it slower a bit.
But before that, let's see how it works.
Let's apply overrides and let's go to game mode now.
People are playing nicely, so I've got the damage.
There is my fruit and I need to catch it before it flies away.
Yes.
Isn't it amazing?
I think it's super amazing, I love it.
Uh, no.
Wait to do.
Got it.
So that's the idea.
I like how it works.
Now let me go back.
Let me delete this from the scene.
And let me try this on different level.
Supposed to work.
We only want to take player and override or we don't need to.
Okay, let's go to scene.
Uh, level three.
There is no fruits on it.
Uh, level four.
Yeah.
There are fruits.
So let's say.
Going to take some fruits.
And if I drop there, there is my fruit and I have a chance of catching it.
I just need to wait.
Cool.
I love the idea.
Well, that's it for this video.
This was great.
Thank you so much for paying attention and I'll see you in the next one.