Hello guys.
Welcome to the video.
In this one I want to show you how to set up difficulty for the game.
You're going to have a very simple setup, but it's pretty fun to make and pretty fun to play I believe.
So what we're going to do here, we're going to have easy, normal and hard mode.
Easy mode will be is the same as we have the game now.
On a normal mode, you're going to lose fruits every time you get in damage or every time you fall into
a dead zone.
And if you have zero fruits, we're going to restart the level.
And on a hard mode, no matter how you got the damage, you're going to restart the level from the beginning.
That's what I want to do.
So how are we going to pass info to the game?
So first let's set up the difficulty so we can set the difficulty and pass it to the main scene.
And I think we have two options here.
One of them is to create a difficulty manager.
Another option is to just rename this skin manager to Settings Manager and have everything in one script.
There is no right or wrong choice here, but I'm a bit limited.
I want this course to be as understandable as it can be, and I know that sometimes people go in the
end of the section and they download the project, and it may happen.
It will be difficult for them to find that skin manager was renamed to Setting Manager.
It can take some time to figure out.
So I want to make a new game object for this.
But for personal project most likely I will do everything like setting manager and I would save everything
there.
Maybe we can do that later on on a cleanup or on the Polish stage.
Now, uh, let me do this.
Let's go and make empty object difficulty manager.
Then we're going to need a script.
Difficulty manager.
I'm going to drag it here.
Let's go inside of the script.
And at the top we're going to declare the enum public enum.
Difficulty, uh, type.
It's going to be easy.
Normal.
Hard.
Then we're going to make it as public static difficulty manager instance, and we're going to need a
wake from a screen manager over here.
That's why I said we can do everything in one script.
Because they pretty much have same functionality.
Uh, yeah.
What is it?
Instance in sense?
Okay.
Now let's make um public.
Enum.
No public difficulty type.
Difficulty.
And we want to have a method that would help us to set up this difficulty.
So public void set difficulty.
We're going to do difficulty type difficulty equals to.
Let me do new difficulty.
You're going to be difficulty equals to new difficulty.
Perfect.
So now this will have information whenever we set parameter through the method through the button.
And we need to.
Make a script for buttons.
So I'm going to go to UI and over here I'm going to make UI difficulty.
Good.
Very good.
Let's go to this UI difficulty.
And over here we want to get reference to the difficulty manager.
So I'm going to the difficulty manager.
Difficulty manager.
In the start we're going to do difficulty manager equals to difficulty manager.
Dot instance.
Then we're going to do public void set.
Easy mode.
And it will do.
Difficulty manager set difficulty to easy.
Then we need public void set.
Normal mode.
Going to be difficult to manager said difficulty of normal.
And public void set.
Hard mode.
And it's going to do difficulty manager set difficulty of.
Hard mode.
Cool half job is done.
Let's go back to unity.
And we need to take main menu, we can just duplicate it and enable it.
Let's rename it to.
Difficulty UI.
Okay.
Now let's go delete the continue button.
We can take the new game button.
Move it above.
Duplicate it.
Move it below.
Duplicate it, move it below.
All right.
Looks good to me.
Now let's name this.
Too easy.
Mode button, and I'm going to copy the name because I feel lazy.
Paste it in here and do normal mode button and.
Hard mode button.
Okay, now we need to change the text.
This will be easy.
This will be normal and this will be hard.
Cool.
Love it.
Now we need to and drag the script over here.
UI difficulty.
And let's go select these three buttons.
Drag difficult to UI over here.
And also I want to do plus drag canvas over here and choose new game so you can easily check the functionality.
Then let's go here.
Choose Set Easy mode.
Then for the normal mode we're going to do UI difficulty set normal mode.
And for the hard one set hard mode.
Okay, cool.
Now, um, we know it's supposed to work, but let's just check it just in case.
I'm going to choose the hard mode and we're going to jump to a new game.
And we have difficulty manager over here with the hard mode being chosen.
Nice.
Now let's go to asset scene level one.
I'm going to save it.
And first of all, I want to make a gap so we can check the falling damage as well.
Uh, let me make a gap over here.
Think that, and I want to take one of the traps from a prefab folder traps and put a spike over here.
Okay.
Very nice.
Now what else?
And now we need to grab the difficulty.
From difficulty manager.
But we don't have it in this scene.
So for the test purpose, I'm going to make a difficulty manager.
Difficulty manager.
We don't have it because we run scene in a test mode in unity.
But your player will never have the situation, so you can easily just make some sort of null check
just to be sure we have it.
And if you don't have it, we're going to test levels in easy mode and only developer will be able to
do that.
Okay so we have difficulty manager.
Let's go to the player.
And I think we can update the difficulty every time we respawn the player.
There may be a better place for this, but we're going to have damage method on the player.
So I think it's okay to do it in the player as well.
Uh, let's go above over here.
And I'm going to do private difficulty type.
Game difficulty, and we probably going to need a reference to the game manager, because we're going
to use game manager here.
A couple of times now.
Gonna go to the start method over here.
Game manager equals to game manager dot instance and difficulty.
I mean game difficulty equals to difficulty manager dot instance dot difficulty.
And we just want to be sure we have uh difficulty manager in the scene.
So I'm going to do if difficulty manager.
That instance not equals to null.
Any other way to do that.
Let's do it this way.
Let's do it.
Difficulty manager.
Difficulty manager and then if difficulty manager.
Is not equal to null, then game difficulty equals to.
Difficulty.
Manager difficulty?
Yes.
And this all looks a bit confusing.
So we can take this into a method.
Extract method and do.
Uh, update game difficulty.
All right.
Now you can see me on the screen.
This all looks good to me.
And maybe we can just drag this over here to make it prettier.
Now we need to use difficulty and game manager as well.
So.
I think we can make some sort of damage method that will decide what is going to happen with the player
when he's getting damage.
Let me move this below update.
Okay.
Over here.
So I'm going to make a.
Private void damage.
Maybe we're going to need to make it public.
But let's see when we get in damage.
If game difficulty is equal to normal, we're going to do something.
Then we're going to return from a method.
For example, we're going to deduct fruit from a fruit collected on a game manager.
And I know that variable is public over here.
Fruits collected.
But I did it only so we can see it is working.
Overall it's better to keep it private since we still testing and developing the game.
I'm going to keep it public.
But to get the value or deduct the value, I'm going to make a method because that's a good practice.
To do so.
So over here I'm going to make public void remove route.
And this will do.
Fruits collected minus minus in game UI update fruit UI fruits collected total fruits and I want to
make a public int fruits collected and it's going to return me fruits collected.
Okay.
Very nice.
Now let's go back to player with this method.
Let's go back over here.
And when we get in damage and if game mode is normal we're going to do a game manager remove fruit.
And then we want to check if we have fruits at all.
Because if we don't have fruits we need to restart the game.
So I'm going to do if uh game manager.
Fruits collected is less or equals to zero, then we suppose to restart level.
All right.
And then after all of this, we're going to return.
And then over here we're going to make another if statement if game difficulty equals to.
Hard.
Then we can just.
Restart level.
Just like that.
No need to remove roots.
No need to do anything.
Because this is the hard mode.
You have to be careful.
You cannot get any damage.
Now, when can we call this damage method?
I think we need to call it when we get a knock back, and also when we fallen into a dead zone.
So.
Uh, for the knockback.
It's easy.
Knockback method is here.
So we can just call it here or we can go to damage trigger.
And just call it here.
Right, but we would need to make it public.
But we would know where do we call damage methods?
So I kind of like this idea.
Let's go here and make it public.
So now we have public void damage on a damage trigger over here we can not only call knockback.
And also we can do player damage and we need to go to the.
Uh.
That's all.
Over here.
And coal layer damage.
All righty.
Very nice.
Now we don't want to respawn player if game is hard, so maybe we can just do the check inside of a
game manager.
Yeah, now this is all good.
It's supposed to work, but I don't want to check it now.
I want to set up a level restart and check it all together.
So let's go to Game Manager.
Um.
Over here.
I'm going to make private void.
Load current scene and it will do.
Scene manager.
Load.
Scene of a level.
Last current level index.
And then I want to make a public method.
Public void restart level in which we're going to do UI in game dot instance fade effect.
What's up?
Great effect.
Fade effect.
I forgot, how does it work?
Screen fade, we're going to give target alpha of one duration 1.5 f.
And as a method we're going to pass load current scene.
Sorry it didn't work for some reason.
Load.
Current scene.
Just like that.
So now when we need to restart level, we can call this method.
Very good.
Uh, let's go to the respond method we have for the player.
Where is the respawn?
This game manager became too cluttered.
I want to clean this up.
Yes.
Over here.
Actually, over here I'm going to open curly brackets.
And I'm going to do if difficulty manager.
That the instant naught equals to null.
Then.
I wait.
How?
How do we make it pretty?
This.
How?
Equals to difficulty manager dot instance.
And then if difficulty manager not equals to null.
And difficulty manager.
Difficulty equals to hard.
Then we're gonna.
Return?
Yes.
This is how it's going to be.
Now, all of this supposed to work, we only need to call restart level on the player inside of a damage
method.
So let's go there.
I'm going to do game manager.
Restart level.
And over here.
Game manager.
Restart level.
All righty.
Going to save this.
And finally, this is ready.
Oh, by the way, how it is working right now, if we have one fruit and we get in damage, then we
will restart the level.
That's not what I want.
So let's.
Let's do it like so.
Let's do else.
Remove fruit.
So if you have.
Cero or less, we're going to restart.
Otherwise we're just going to remove fruit and nothing else.
It should be better.
Yeah.
Let's go to unity now.
I'm going to select my difficulty manager.
I'm going to choose normal.
I'm going to go to play mode.
So I have one fruit getting damage.
I lost it getting damage again.
And my character did not die.
But we restart the scene.
Okay, I'm going to see how we can fix it.
Let me try to jump into a bit.
Now there is no dead zone.
That's on.
I know I have to start over.
Jumping into a pit.
Restarting.
Okay, this is good.
The only fix is to kill the player when he's getting damage like that.
I like it now let's fix that problem with the knockback.
So when you get knocked without HP or fruits, you're going to die immediately.
Let's go over here.
I'm going to just call method die here like that and over here as well.
Die.
Yeah.
And over here we call damage before knockback on a damage trigger.
I guess this is important.
Don't forget to do this.
Now.
It should be good.
Let me try to start game in a hard mode.
Hard mode.
Now, even though I have a fruit, I'm going to get damage and died and have to start over.
Okay.
What if I do normal mode?
You've got the fruit.
Got the damage.
And get damage again.
And then you.
So this works very well I like it.
As I said, I'm going to delete difficulty manager from here because we're only going to need it when
we test in the difficulty or when we start the entire game to play.
But when we just test levels, I guess we can test them on the easy mode, right?
Lastly, I think the fade is too long when you die because this is the platformer game and I believe
player would want to start over faster.
So let's just go to game Manager over here.
And.
When we restart the scene.
Over here.
I'm going to do just a 0.75 F, so it's going to be faster.
Okay.
Hey guys, I just realized I forgot to show you something.
Something very nice.
Uh, let's go to the main menu.
And over here I want to display what the difficulty does, like sort of a text over here that would
say, hey, easy mode does this, normal mode does this.
So how is it going to work?
On the object.
You can add on pointer enter or on pointer exit functionality.
And we just need the script here.
Let me go to UI.
Script.
UI difficulty button.
Okay, we're going to open this.
And I'm going to do close all by this.
First we're going to need reference to the text.
So serialize field private text mesh Pro.
Um, difficulty info.
And we need, uh, serialized field, private stream description.
All right.
And we want to turn this string into some sort of text area.
And that's why we can type square brackets and do text area.
Because of that.
Now we'll have an area of attacks, not just one line.
Let's go back.
Let's drag this script on these three buttons.
If you go to button and you can see we have a description here.
Very nice.
Now we need another element here I'm going to make UI Text Mesh Pro.
Let me take this text.
Drag it.
Here, we're going to change it to Minecraft Asdf.
And I'm going to make it.
Look something like this.
I think it should be okay.
Let's align it in the center.
Now let's make font 42.
And then let's select all three buttons and drag this text over here.
I'm going to do description.
Difficulty.
Description.
Nice.
Now what we want to do next we want to go to the script of UI difficulty button.
And over here in this script we just need to type comma pointer enter.
And there is I pointer enter handler.
Let's choose it.
And let's add a pointer exit handler.
So these two methods will be called when pointer enters the area of the game object.
In our case pointer is a mouse and game object is a button.
So every time mouse will enter the area of a button, this method will be called.
And over here we can do difficulty info.
Dot text equals to description.
On pointer exit.
We're going to do difficulty info.
Dot text equals to just empty space like that.
Yeah.
Let's save this and let's go back to unity.
And as his own.
And here I'm going to help.
That's at your house.
The experience and knowledge.
And over here I'm going to type any damage will.
All right.
Now let's go to play mode and we can see if I'm entering a button with my mouse.
We have a description of the difficulty.
Any damage will kill you.
Yellows fruits on damage.
Unlimited life.
You don't lose fruits on damage.
Description could be a little bit better, but that's just an example.
If I come up with a better description, I'll let you know.
Okay, well now that's good.
Let's disable I mean, let's remove text here by default and let's disable it.
Let's enable main menu and we can go to the next video.