Hello guys.
Welcome to the video.
So in this one we're going to implement uh mobile controls for this game.
And for that I'm going to need a project from section 11.
That's what I'm going to get.
Because this new input system and things that we implemented in the co-op version of the game, they
are basically useless and they only make it difficult to implement touch controls.
So we don't need them.
Uh, Flynn.
Enemies.
This is an optional content.
If you want to have Flynn enemies, you can add them later on.
Or just maybe drag a prefab to a new project.
It's supposed to work.
Okay, so I'm going to open this project now that I had in the section 11.
And we're going to implement touch controls.
Okay.
I've got the project from the end of section 11.
This does not have any new input system.
It has the project as it was there.
And now we're going to implement touch controls.
Okay.
Uh, first of all, let me go to one of the scenes so I can see the game.
Um, level one is okay.
And now we need to get this asset from the asset store.
I'm going to show it to you here.
It is called joystick pack.
You can just search it, uh, by typing joystick.
And it should be the first one.
And I'm going to touch link to this video as well.
Okay.
So you get this asset pack I already have it in unity.
So I'm going to jump back.
I'm going to go to Windows Package Manager, my assets and I'm going to look for joystick.
And there it is.
Joystick.
So I'm going to just import it.
I'm going to import everything.
And now first let's make it work.
Then I'm going to talk about this joystick a little bit.
Or we need to go to asset joystick pack prefabs.
And over here I'm going to go to UI in game and drag this variable joystick over here.
And because of the way prefab is set, let's go to normal mode.
It's going to be attached to the bottom left corner, so it's fine.
Uh, we also need a jump button for the future.
So I'm going to make UI.
Button.
Let's focus on this button.
We're going to name it as um jump.
But on UI.
And I think I want to make it, uh, bigger.
Something like.
Something like this.
Uh, let's attach it to the right side.
Make it wider.
So it should take half of the screen.
Yeah, something like this will work.
And then let's reduce a little bit from the top.
Because on the top, we're gonna place a menu button with a UI.
Okay.
Nice.
Now let's go to the image and make it transparent so we cannot see this button.
Maybe for testing you want to make it visible, but after you're done, you're going to make it completely
invisible.
Okay.
And we want to remove text from here.
Now we need a component on this jump button.
So we can easily recognize this button.
I think we can go to scripts UI and we're going to make.
UI jump button.
Very nice.
Now let's put this on the game object.
And this will do the job for us later on.
Or maybe let's start with this one.
Why not?
Let's go to jump button over here.
First of all, we need a method.
So we can update players reference because we're going to call jump method from here.
Okay.
I'm going to do um.
Private player.
Player.
Then we need public void update players.
Ref.
And we're going to do.
Player.
New player.
And we're going to do player equals to new player.
We need this because every time player dies we would lose the reference.
And we can call this method every time we respawn the player to update the reference.
Now we need to get the interface I pointer.
Pointer down.
And we need to click Alt Enter.
Or you can just use your mouse.
Quick actions implement interface.
Now whatever we write inside of this method will be called every time pointer down on the jump button
we have.
In the pointer is usually mouse, but if you use your phone, then pointer becomes your finger and every
time you touch the button with your finger, it's going to call on pointer down method.
That is basically the perfect solution for us.
So now we just need to call a jump method from a player script.
Now let's go to player script.
And we already have.
This.
Wait a second.
Let's find it.
Yeah, yeah.
We have this jump button method.
How could we name it?
So it's not confusing for us.
Let's name it as we're going to rename this to jump.
Attempt.
So it's going to attempt the jump.
And then we're going to need a new method.
It's going to be public.
Void jump button.
And this jump button will do the same thing as these two lines of code.
So basically we can cut this out, paste it in here and then call jump button over here if we need to.
Very nice.
Yeah.
So we kind of took these two lines of code into a method, and we changed the name of the method over
here.
Now we can go back to UI jump button.
And over here we're going to do player jump button.
So every time we touch the jump button it's going to call jump button from a player and make it jump.
Simple as that.
Now we need to, uh, call this method every time player is respond.
I think we can do it in the wake of the player.
That would be pretty easy.
So we're going to go to the awake.
Yeah.
Why not here.
And we're going to do something like this.
Find first object by type of UI jump button.
And I'm assuming we're going to have only one jump button.
So this should be pretty much okay.
And we're going to call update layers reference.
And over here we need to pass the player.
And to do that we can easily type this.
Simple as that.
All right, so this will do the job.
Let's go and try this.
Yeah.
Let's go and try.
We're going to go to play mode.
And now when I'm pressing this button, character is jumping.
No delay.
Nothing.
Very good.
And it will work with your fingers as well when we make a mobile build.
Okay.
Very good.
Now we know it works.
We can just make this button transparent.
Let's do it like so.
And let's work on a joystick.
Same as a jump button over here.
This joystick has a script.
This one says Variable joystick and it inherits from a joystick script.
We can check that by going to edit script over here.
We can see it inherits from a joystick, and this joystick has values of horizontal and vertical input.
The code here might seem complicated.
You just ignore it.
We don't really need to know how it works.
It's just the same as we used input.
Get access row, but from a joystick.
Yeah.
Very good.
So we just need to get this variable and then we need to get these values from here.
This is how we can do it.
We can go to player and find, um.
This place over here.
And we can make private joystick.
Joystick variable.
Then in the awake we can do something like joystick equals to find first object by type.
And again I'm assuming we're going to have only one joystick on the screen.
So it's fine to do it this way.
Nice.
Now we have a reference to the joystick and we can go to the handle input.
Over here and replace these two with a with the input from a joystick.
Uh, let's do it.
Let's just comment out these two because we might need them later.
Uh, I'm going to do x input equals to joystick.
Uh, horizontal and y input.
Equals to joystick vertical.
And as you understand, if you want to test it on the computer, then you would have to uncomment these
two lines of code.
And I mean if you want to have control with the keyboard again, but we need this for mobile.
So we're going to keep these two.
Okay.
Uh let's go back to unity.
And now we're supposed to go to play mode, and character should be controlled with the joystick.
Yes.
Very nice.
Now, this is working very well and we only need to discuss the joystick itself.
This is the asset that was made by someone.
It's a shame.
I don't know who.
So let me check.
It was made by Phoenix Studio.
Thank you guys.
Yeah.
And so.
It's pretty simple.
Uh, it has joystick type.
And you probably remember we had different prefabs over here for the joystick.
So what you should know is just these three prefabs is a single joystick with a single type and variable
joystick is a joystick that contains all three of them together.
So if you want to have just one, you can drag one like that.
But there is no point because you can have variable joystick and switch between the one you want.
Okay, now let's go to play mode and see the difference between them.
Uh, fixed joystick is placed in the corner.
It's always there.
It does not disappear.
If you go to joystick type and choose floating, then your joystick is going to follow your finger.
Sorry I died.
Was not supposed to.
So as I was saying, the floating type will follow your finger a little bit so you can touch at any
place on the screen as long as it's within the joystick area over here on the top over here you can
see.
There's going to be like that.
I kind of like it.
I think it works very well.
So we can use this one.
And last option is dynamic.
This one will not only appear at the place where you touch screen with your finger, but also it's going
to follow your finger like that.
And if you like it, you can keep it.
Why not?
Seems pretty.
Yeah.
And also something else here.
You can see if I touch my joystick really slow, like by a little bit, my character barely moves.
This is how it works.
It's happening because we have a float value from a joystick, and if you want to have it like it was
before, we need to switch over here snap x and snap y.
And now it's going to be snappy and responsive just like it was before.
Okay, that's the idea.
So very good.
Let me go out of the play mode.
And I think I want to increase the joystick area to something like this.
And let's change it to floating snap snap.
And now we need to take UI in-game and apply overrides.
So these changes will be applied to all of the levels we have in the game.
Very good.
Now I can go to scene level two.
Let me save it.
And I have joystick here and I control and I can control character in this version as well.
Yeah.
Very good.
Now lastly, we need to add some sort of a pause button because we cannot press escape button in here.
Right.
Or do we not have escape?
Do we not have pause functionality at all?
So I'm sure I made it.
There is a post UI.
Let me check.
Oh, I'm using PKI.
Okay.
Let's go and make pose button.
I'm going to go to.
Wait a second.
We need to go here and we need to make.
UI button is going to be.
How's.
Buttons on the UI.
And we're going to touch it to the top right corner.
And maybe move it to the left and in the middle.
Wait a second.
I want it to be.
I know what we're going to do.
We're going to make it dark.
And a bit transparent like that.
You're going to change font on this.
And I'm just gonna.
I'm here.
How's.
And let's increase the size of a button itself.
Something like that.
I think it's better if it's white so we can see it.
And font size a bit bigger.
Yeah, it's kind of clunky, but I don't have any icon.
If you have an icon, you can replace it and make it pretty.
So far for me, I think I'm going to accept it.
Maybe I will take the timer.
Move it to the left.
Like that.
Or maybe in the middle.
Like that, I don't know.
Well, it's not as important right now, I guess, how we place the buttons.
Yeah, let's just make the functionality.
So pause button.
Should work just fine.
Like that font size 46.
I think I like it now.
You know what we need to do, right?
We need to, uh, add on, click over here, drag UI in game and find pose button method.
Very nice.
Now this one will work if you click it with the mouse or with your finger on your phone.
This one is not working.
What happened there?
I hate when this happens.
Um, let me see.
Mm.
I think the variable is covering these two buttons as well as a jump button.
So let's let's put pose UI over the variable joystick.
Let's see.
Okay.
Sorry.
We need to put this below John Button like that.
Oh, now it's fine.
Okay, let's fix it out of the play mode.
So we're going to take pose UI and drag it below jump button.
Okay, let's do it inside the prefab.
Um AWS UI.
No, wait.
I'm so confused.
Give me a second.
Um.
We need to make these changes in a smart way.
I'm going to apply overrides.
So now we have pause button UI in the prefab.
And then we're going to just drag it here.
It will show us menu that will allow us to open prefab.
Let's click it.
And then let's drag this UI below the jump button.
So it will not cover those buttons inside of the pause UI.
Now we can go back.
Save it.
We don't need to apply overrides.
It's going to be fine and we can go to play mode.
Click, pause, resume, pause, resume, jump, run around and all of it will work on the phone as
well.
And I'm going to show you that very soon.
Okay.
Uh, as for controls, this is pretty much everything we need to do was very simple and fast.
I hope you like it.
And let's go to the next video.