Hello, guys.
In this video, we're going to make our rain in this game.
And I want to make it not only raining, but I want to make a dynamic rain.
So sometimes it's raining, sometimes it's not.
But first we need to begin with making rain itself.
And it's going to be pretty easy because we're going to use acid store.
Let's go to window.
Acid store.
And in the search field.
We just need to type to the rain.
Good.
Now let's go and find the one we need.
This the one?
Exactly.
I'll try to leave the link in case you cannot find it.
Now let's click open in Unity.
And we're going to download this asset.
Once it's downloaded, we need to find it.
For some reason, I lost it.
Yeah.
Here.
And then click install.
I mean import.
We don't need them.
I seen we can uncheck this one.
We need a prefab and we need an editor, but we can uncheck redmi file.
Let's do import.
And if you remember in the lecture about particles, I told you we need to click reload.
So if you remember in the lecture about particles, I told you that it's easy to learn system but hard
to master.
And this is one of good examples what you can do with particles if you know it really well and if you
know how to code, you can make a rain like that.
Someone else already did it and he put it up for free use so we can use it.
Let's go to Rain maker Prefab and we need to find rain prefab to DH.
Let's drag it in the scene.
So this is the rain game object.
It's going to follow camera on its own and it's going to find camera on its own, so we don't need to
place it anywhere.
You can see it here and there.
The camera field is a main camera now and follow camera check.
So it's going to follow camera.
And because camera follows the player.
Rain going to follow the player as well.
Now, how does it work?
What does it do?
Very simple to understand.
Here we can see sounds for light, medium and heavy rain.
There is an audio mixer you can set up if you want, so you can control.
Oh, sorry.
Why that was so easy to miss.
Let me see.
Let's fix it.
Kind of list.
Main Menu tab to start.
Oh, it's our lane, the settings button.
Okay, let's move it below.
Somewhere here.
Now I want to make it smaller and.
Yeah.
So as I was saying.
You can take this rain.
Go to settings and create another mixer for volume of the rain or some buttons to switch it on or switch
it off after next video.
On our day night system, you're going to know how to do that for sure.
But in this video I want to focus on the rain itself and on setting up the random rain from time to
time.
So this is the sounds.
This is all the mixer.
This is the main setting.
We're going to use rain intensity.
If you increase it, it's going to rain more.
And as of now, you can see it's colliding with an object above entire scene we have.
So to fix that, we're going to do some simple steps.
We're going to go over here to Collision mask.
And as of now, we don't have many layers.
We have only ground layer.
Let's choose that one ground.
Now it's going to collide with the ground.
But we cannot see it.
We cannot see it because particles under this game object.
Have different certain layer.
Let's select three.
I mean all of them.
These three go all the way down to renderer and change certain layer to we can select player and we
can give it priority of number one, for example.
Now it is feasible and we're going to need to repeat all of the steps out of the play mode.
I'm just showing it in the play mode, so it's easier to explain.
So now rain intensity can be increased and we can see the rain.
And as you can see, it's colliding with the ground.
Some thing to notice is that particles of the rain going through the ground, sometimes it's happening
because of the settings over here.
Collision, lifetime rain over here.
If you wanted to go through the colliders, you can increase the value.
It's going to look like that.
But I think we should make it a zero so it bounds of the ground.
This is collision for mist.
And.
Wait a second.
Yeah, here is the wind settings, but they're not really important for us.
So the main point for us is the rain intensity.
And as you can see, high intensity with our mood, the game because it's kind of loud.
So if you put very high intensity, it's going to look like this and it's kind of too much, I guess,
for this game can play a lot.
So what we can do to fix it is to set minimum and maximum rain intensity.
I think maximum can be something like this even less.
They won without missed.
So 49.
This can be the maximum and the lowest can be point one.
Something about 1.5 is okay, what else we need to change here is the rain height.
And the rain with and with is most important.
So if you decrease the value, you can see the rain getting kind of slimmer.
We can make it wider.
And I think it should be somewhat about screen size.
Yeah, we can also increase height and it's going to go from a higher point.
You can see it in the thin window.
But we don't really need it.
So we can make it.
We can place it lower.
Just leave it how it was.
So this is the main settings and then we need to make a custom script to control intensity from time
to time.
Let's go and set it up over again.
First, we're going to select particles.
Find renderer change it to player with the order five.
Now we need to add layers.
Let's go to layers over here.
Let's go to layers and layer.
We're going to have one for player, one for traps and one for coins.
Or we could actually create one layer like interruptible.
But let's keep it like this.
And we have ground already.
So we're going to select player, change it to player.
We can do Yes.
Children as well.
Okay, that's good.
That's good enough for player.
Now let's find other objects in the prefab.
Coin can be changed to.
Coin traps can be changed to a trap.
This is the trap.
And the one inside.
Trump as well.
Okay.
And the platforms are already under the ground layer, I believe.
Otherwise we couldn't move on them.
Yeah.
Okay, let's see what else we want to set up.
Collision mask.
We need to set it up now from nothing.
We need to ground player drops, coins.
Cool enough.
Collision lifetime.
Let's make it zero.
What was the width?
I don't remember.
Honestly, let.
Let me double check.
Like 1.2.
Let's do 1.2.
Good.
We can leave it like that now.
And unless you want to set up mixer, you can do it.
Now I'm going to go to script folder and create a script.
C Sharp rain controller.
I'm going to unpack this prefab prefab, unpack, fold this one and drag my rain controller above.
Now I'm going to open this.
And if you take a look at this script, let me go inside of it.
It is inherited from his reign script.
And if you go to Baz Rain script, we can find here rain intensity, the one we used in the inspector,
and it uses range to make it look like that slider in the inspector.
So I'm going to copy this.
Go to my brain controller and paste it in.
You don't have to copy.
You can just type the same, basically.
But I want to change it from rain intensity to just intensity.
This will be the name.
Oh, and it can be private for this one.
Sterilized field.
By the way, if you don't type word private here, it's going to be private like that and just like
to type private in my courses.
So it's clear for students, probably, I should tell you that before.
Now we have intensity.
What else we need to know?
We need to know how fast we change in from, let's say, zero to some rain intensity because we cannot
do it like, you know, from zero to point four.
It's going to look real harsh and we want to make it and we want to make smooth transition.
So it's going to be serialized field private float change rate.
We also want to have minimum intensity and maximum intensity.
So private float mean let's do mean value and max value.
We need a cool down.
How often are we going to check?
But you know what?
Let's think about cool down later.
Let's focus on changing the intensity first.
As do private void.
Change intensity.
Inside.
We're going to check all Yeah, we need target intensity.
So we have intensity that we have at the moment and we have target intensity.
So we're going to do the serialize field, private float, target intensity.
In the change intensity, we're going to check if we are decreasing or increasing the value.
So for example, if we increase in the value, we're going to do this if intensity.
Is less than target intensity.
It means we increase in the value.
We want to bring intensity to the level of target intensity.
So in that case, we're going to do intensity.
Plus equals.
So we're going to increase it by change rate multiplied by time, dot, delta time.
Now we need to check if we reached that value that we want.
So if intensity is bigger or equals to target intensity, then we set intensity equals to target intensity
and we want to stop doing this changes.
So let's create a boolean.
Crime on the ball can change at intense intensity.
And we're going to set can change intensity to false.
Now we need to the same for decreasing the value.
So if intensity.
Is bigger than target intensity.
Then we're going to change it to the lower value.
So intensity minus equals change rate multiplied by time.
The delta time, if intensity is less or equals to target intensity, what was that target?
Target intensity, then intensity equals target.
Intensity can change.
Intensity equals false.
Good enough.
Now let's make an update that I deleted for no reason.
Update.
Date and I want to change intensity by pressing the button for now.
So I will do if input dot.
Get key down E code R then.
Can change.
Intensity equals true and lower over here we're going to do if.
Can change intensity, then change intensity.
Yeah, something like that.
And we're going to set target intensity with our hands, so it's fine.
The only thing I want to do is to set default value for change rate, and it's going to be 0.05 F.
We really need a small value and minimum value we define before.
So it's going to be minimum 1.2 F and a weight point to F and maximum will be.
.49 F, Let's go back.
So we have target intensity and let's go to play mode.
We can see intensity over here.
So let's say I'll set target intensity to point four and I'll click my R button, my input, boom,
it's increasing until it reaches 0.4.
And then stopped.
Very good.
I'm going to change it again to point one and press the button.
Are.
It's decreasing.
Very nice.
Now we need to connect these to rain intensities from the rain script to rain controller.
Luckily for us, the value in the script is public, if you remember.
Over here.
Let me go to base.
Rain intensity is public so we can get access to that.
We want to get access to the component of rain script on the same game object.
So I'm going to do private.
Rain script to RD rain controller.
And I want to show you another way of getting component how you could do it without start function and
without dragging objects in the inspector.
You can do return get component of rain script to D.
Yeah, this will work as well.
Now, in the update we're going to do rain controller dot rain intensity equals to intensity.
So we're going to assign an intensity of the rain script to DX to our intensity in this script.
Pretty simple, right?
I hope so.
And instead of pressing the button, I want to do it randomly from time to time.
So I want to make.
Serialize field private float.
Chance to do rain.
And default value will be 40.
Then we need cool down time.
So how often are we going to check if it's going to rain now or if it's going to stop?
Serialize field.
Pilot float.
Rain check.
Cool down.
And then we need a timer.
So, private float, rain check.
Timer.
Let's decrease the timer all the time.
Rain check.
Timer minus equals time, dot delta time.
And then we need a function.
I'm going to create a function here.
Private void.
Check or rain.
Inside.
We're going to say if.
What was that timer?
Hammer, Rain check.
Sorry.
Rain check timer is less than zero.
Then we're going to set rain check timer to cool down so it won't work for some time.
Cool down.
And then we're going to change the intensity, the target intensity.
We have target intensity equals to random range between mean value and max value.
Then we're going to say we can change intensity.
Now can change intensity equals to true.
And we're going to put it in the update.
Check for rain.
So now it will work and it will check the rain every now and then according to the cool down we have.
But there is a slight issue and it's always going to set value from minimum to maximum.
And in this way there will be always rain just with a different intensity.
So what I want to do here is to stop rain sometimes, like at all.
How are we going to do this?
We're going to use this chance to rain.
We're going to do if check.
Simple check if random range from 0 to 100 is bigger than chance to rain.
Then we're going to set target intensity to random between minimum and maximum.
And if it's not, then we're going to set target intensity to zero and it's going to decrease and it
won't do anything for next 40 seconds.
And then we can change intensity to true.
I'm going to place it here to avoid confusion.
And.
Yeah.
Good enough.
We done with the script.
How do you like it?
I like it.
I will set raincheck cooldown to 2 seconds and our chance to rain is zero for some reason.
I'll try.
I'll try 99.
Let's go to play mode and see how does it work for us.
So it's going to check every 2 seconds, which is pretty open so we can see changes if they work or
not.
What's that?
Did.
I think I put the wrong sign.
If I'll put one here.
Yeah.
I.
We need to fix.
Let's do it right now.
This one should be less than.
Yeah, right.
If random range.
If values less than chance to rain.
So now with a high chance, it's going to rain all the time and because of the high cooldown, we can
see how it's changing from there to there all the time.
What we can do next to fix it is to set code down to something bigger, like 20 seconds.
And chance the rain can be maybe 40 or something about that.
And now I'll try to run for some time and see how does it work for me.
This drops water and drops bouncing off the traps of the player.
Looks pretty nice.
Rain stopped.
Okay, let's say that chance is pretty low, so we need to we need to increase the chance.
So maybe a chance to rent can be 5050.
I don't know, guys.
This is all about balance.
How often you want it to rain.
But we did it.
Script is working and let's go into the next video.
Let's make beautiful skybox.