WEBVTT

00:00.000 --> 00:00.450
Hello guys.

00:00.450 --> 00:01.410
Welcome to the video.

00:01.410 --> 00:04.770
In this one we have to do a couple of more changes to the player manager.

00:04.770 --> 00:08.730
And I want to make on player join functionality.

00:08.730 --> 00:14.370
As of now when we join the player he just chooses the position and somehow it is placed at the starting

00:14.370 --> 00:15.720
point where it's supposed to be.

00:15.720 --> 00:21.150
But we may move the start point, or we may update the checkpoint position and then we need to be sure

00:21.150 --> 00:24.240
player is created at the position we want it to be.

00:24.840 --> 00:29.940
Also, we need to have skins for each player and also which is most important.

00:29.940 --> 00:34.290
We need to remember which player uses which device, right?

00:35.560 --> 00:40.780
And before we do all of that, I want to be able to kill the player just to check some functionality.

00:40.810 --> 00:45.730
So we need to do a bit of a change that we're going to upgrade later on.

00:46.060 --> 00:48.820
As of now, we need to go to the player script.

00:49.960 --> 00:54.340
And over here we have this functionality where we restart the game.

00:54.340 --> 00:55.570
That's not what I want to do.

00:55.570 --> 01:00.790
I want to comment this out and I want to comment this out here as well.

01:00.880 --> 01:03.550
I think we are starting the game from somewhere else.

01:03.550 --> 01:06.820
So I want to double check that by going to Game Manager.

01:09.400 --> 01:12.400
Let me find this restart game method.

01:15.560 --> 01:16.190
Where is it?

01:16.190 --> 01:17.300
Restart level.

01:17.300 --> 01:19.100
It has zero references.

01:19.100 --> 01:19.490
Okay.

01:19.490 --> 01:21.140
It is not used anywhere for now.

01:21.140 --> 01:21.830
Very good.

01:21.830 --> 01:23.060
So now the player dies.

01:23.060 --> 01:24.530
We don't need to restart the game.

01:24.530 --> 01:28.550
Also, I want player to die no matter how many fruits he has.

01:28.550 --> 01:33.110
So I'm going to go to prefab of a player and I'm going to choose difficulty hard.

01:33.110 --> 01:39.200
And on the managers over here on difficulty manager, I'm going to choose hard difficulty as well just

01:39.200 --> 01:39.950
in case.

01:40.280 --> 01:41.300
Very nice.

01:41.300 --> 01:44.450
Now let's go to Player Manager script.

01:45.290 --> 01:47.210
We need to do a couple of changes here.

01:47.900 --> 01:53.300
Uh, first of all, we want to know when player joined the game so we can give it some default variables,

01:53.300 --> 01:55.610
change some maybe position or anything else.

01:55.610 --> 01:57.320
Let me do close all by this.

01:57.440 --> 02:02.210
We can achieve that by, uh, getting component of a player input manager.

02:02.210 --> 02:07.430
And then there is a possibility to subscribe to the event of player joining the game.

02:07.460 --> 02:10.670
Uh, let's go here and let's maybe let's do it above.

02:11.540 --> 02:18.110
I'm going to do public, uh, player input manager, player input manager.

02:18.110 --> 02:23.000
And I'm going to do get private set because we're going to need it in the future from another script.

02:23.210 --> 02:29.450
Now on their wake, let's do player input manager equals to get component of player input manager.

02:29.780 --> 02:31.010
Very nice.

02:31.040 --> 02:33.410
Now we need on enable method.

02:34.430 --> 02:40.820
I'm going to do on enable, and I'm going to do on disable and inside of on enable method we can do

02:40.820 --> 02:47.360
this player input manager on player joined plus equals.

02:47.360 --> 02:50.210
And now we can type name of a method that we want to have.

02:50.240 --> 02:52.820
For example add player.

02:52.820 --> 02:55.040
And then we can uh sorry like that.

02:55.040 --> 02:58.430
And then we can press alt enter or maybe just mouse button.

02:58.430 --> 03:03.050
Quick actions and refactoring generate method add player.

03:03.050 --> 03:06.530
So now this method will be called every time player joins the game.

03:06.530 --> 03:11.210
And also it will have information about the input that were used to join the game.

03:11.210 --> 03:16.160
We can get access to the uh input component to the player component.

03:16.160 --> 03:18.980
Everything can be done through this variable over here.

03:18.980 --> 03:20.870
Let's rename it to player.

03:21.170 --> 03:21.980
Sorry.

03:24.040 --> 03:25.990
And let's move this method below.

03:25.990 --> 03:26.890
Over here.

03:27.490 --> 03:28.840
So it's pretty.

03:28.840 --> 03:30.910
Maybe even here like that.

03:30.910 --> 03:31.570
Okay.

03:32.320 --> 03:33.700
Then on disable.

03:33.700 --> 03:35.470
We want to unsubscribe from this event.

03:35.470 --> 03:37.090
So I'm going to duplicate this.

03:37.090 --> 03:39.850
Cut it out paste it in here and do minus.

03:40.670 --> 03:41.570
Like that.

03:41.780 --> 03:42.800
Very good.

03:42.830 --> 03:43.850
I like it.

03:43.880 --> 03:49.340
So now we have method that is called on player joined which basically is a respawn method.

03:49.340 --> 03:55.010
Now that we have it let's take this on player respawn and walk and place it inside of this method.

03:55.010 --> 03:57.830
And this will update the reference to the player.

03:57.830 --> 04:02.270
However, it is not working perfectly because now we're going to have a bunch of players, not just

04:02.270 --> 04:02.690
one.

04:02.690 --> 04:04.670
And if you remember enemy.

04:06.730 --> 04:07.600
There is the enemy.

04:07.600 --> 04:09.190
Enemy over here.

04:10.790 --> 04:16.100
Just updates transform of a first player in the player manager.

04:16.100 --> 04:17.540
Let's not touch it for now.

04:17.540 --> 04:19.040
Let it just do its job.

04:19.040 --> 04:19.730
It's fine.

04:19.730 --> 04:22.940
Let's just go ahead here and maybe you know what?

04:22.940 --> 04:25.040
Maybe, just in case, I'll do this.

04:25.040 --> 04:30.080
If player is equals to null, then player.

04:31.720 --> 04:31.900
Right.

04:31.900 --> 04:37.660
We need to check this this player because we have player variable over here.

04:38.890 --> 04:45.940
So if this player is equals to null, then this player equals to player getcomponent of a player.

04:46.150 --> 04:47.380
This is a placeholder.

04:47.380 --> 04:51.310
I'm going to remove it later on, but for now we just need it so we don't see any errors.

04:51.310 --> 04:51.940
Okay.

04:52.360 --> 04:53.080
Uh very nice.

04:53.080 --> 04:54.430
Let me place it like that.

04:55.300 --> 04:57.970
And we can forget about this now what we want to have.

04:57.970 --> 05:04.090
We want to update the respawn position when player is added, and then we want to place him at respawn

05:04.090 --> 05:04.810
position.

05:06.960 --> 05:10.020
So first of all, I want to take this.

05:10.020 --> 05:11.610
I want to extract method.

05:13.790 --> 05:15.860
Place player.

05:17.010 --> 05:23.370
Place new player at respawn point and this one will require a transform.

05:23.370 --> 05:26.370
So I'm going to pass transform new player.

05:27.240 --> 05:28.950
Then we can remove it from the start.

05:28.950 --> 05:31.530
We're not going to need it in the start.

05:32.810 --> 05:34.160
Then over here.

05:34.160 --> 05:38.120
You remember we just double checking if we have respawn points so we can keep it.

05:38.120 --> 05:45.020
And after that we're going to do new player dot position equals to respawn point dot position just like

05:45.020 --> 05:45.620
that.

05:45.620 --> 05:53.870
And then we can just go ahead over here and do place new player at respawn point.

05:53.870 --> 05:55.910
And we just need to pass it a transform.

05:55.910 --> 05:59.240
And I'm going to pass player transform like that.

06:00.540 --> 06:06.840
Okay, just to clarify things, when we created a player, uh, on player joined, this method will

06:06.840 --> 06:07.350
be called.

06:07.350 --> 06:11.610
And it will have information about player input component on a player game object.

06:13.000 --> 06:16.390
Which is, uh, right here.

06:17.410 --> 06:22.660
And through this component we can get access to any other component on the player game object.

06:22.660 --> 06:24.580
That's why we're doing it this way.

06:25.060 --> 06:27.550
We're not going to change this method in the nearest future.

06:27.550 --> 06:32.260
So I'm going to cut this out from here and place it somewhere below because we can forget about it for

06:32.260 --> 06:32.680
now.

06:33.590 --> 06:37.460
Okay, let's go ahead and just make sure it's all working.

06:37.490 --> 06:39.140
So I'm going to place it here.

06:39.140 --> 06:42.350
And also by the way we don't need this start method anymore.

06:42.350 --> 06:45.470
So I can just clean this like that.

06:48.340 --> 06:48.940
Okay.

06:48.940 --> 06:49.660
Okay, okay.

06:49.660 --> 06:50.650
Very good.

06:52.690 --> 06:55.420
I'm going to show you a wing, just in case if you want to see this.

06:55.570 --> 06:57.250
This is how it looks right now.

06:57.760 --> 07:00.040
And let's go back to unity.

07:00.850 --> 07:05.050
I'm going to keep first player in the game for now, but later on we're going to just join both of the

07:05.050 --> 07:05.800
players.

07:07.820 --> 07:11.240
Okay, so I'm going to use my gamepad to join the game and I'm there.

07:11.240 --> 07:13.040
I don't see any errors.

07:13.040 --> 07:16.130
Let me heal the player from a gamepad.

07:16.730 --> 07:17.150
Done.

07:17.150 --> 07:20.360
And let me change respawn point.

07:21.400 --> 07:23.950
Let's say we have a checkpoint in the game.

07:24.100 --> 07:24.880
Prefab.

07:25.030 --> 07:26.890
Uh, where is it?

07:28.190 --> 07:29.090
Checkpoint.

07:29.390 --> 07:30.650
I don't see anything.

07:33.040 --> 07:35.110
Let's say checkpoint is, um.

07:36.540 --> 07:37.560
It was updated.

07:37.560 --> 07:38.100
Oh, my.

07:38.460 --> 07:39.150
Wait a second.

07:39.150 --> 07:45.060
Let's say checkpoint is over here, and first player from a keyboard jumped to that point to activate

07:45.060 --> 07:46.200
the checkpoint.

07:46.230 --> 07:49.500
Now it's supposed to be updated on the player manager.

07:49.500 --> 07:50.430
Over here it is.

07:50.430 --> 07:51.120
Checkpoint.

07:51.120 --> 07:53.760
And I'm going to use GamePad to create a new player.

07:53.760 --> 07:57.720
And I'm at checkpoint so it works okay.

07:58.140 --> 07:59.070
Very nice.

07:59.070 --> 07:59.790
Love it.

08:00.660 --> 08:03.930
What other things we want to do on player join?

08:03.960 --> 08:10.650
Well, we want to keep track of players that are in the game, so probably it is a good idea to add

08:10.650 --> 08:12.030
a list of players.

08:13.310 --> 08:14.510
Uh, let's make it this way.

08:14.510 --> 08:15.950
Let's do serialize field.

08:15.980 --> 08:17.450
Try what player?

08:17.450 --> 08:20.780
Actually, we're going to do list of player.

08:22.070 --> 08:23.990
It should be as serialized field.

08:25.380 --> 08:27.540
And I'm going to name it as Player list.

08:27.930 --> 08:31.140
And let's make it equals to new list of players by default.

08:31.140 --> 08:32.040
All right.

08:32.520 --> 08:32.940
Um.

08:33.830 --> 08:38.570
I don't remember if we used the list before in this course, but it's pretty simple to understand.

08:38.570 --> 08:43.040
It's just like an array, but the difference is that you can change list dynamically.

08:43.040 --> 08:48.770
You can change content of the list in the script while you playing the game at runtime, and you cannot

08:48.770 --> 08:51.440
do that with an array, but with a list it is possible.

08:51.440 --> 08:52.520
So now.

08:53.380 --> 08:56.020
We can go to add Player method.

08:56.020 --> 08:59.170
And over here we can get component of a player script.

08:59.170 --> 09:00.430
I'm going to do player.

09:02.840 --> 09:03.260
Um.

09:03.260 --> 09:04.190
Player.

09:04.670 --> 09:05.360
Let's do.

09:05.360 --> 09:06.200
Player.

09:06.990 --> 09:08.310
Player script.

09:08.340 --> 09:09.690
Equals to player.

09:09.720 --> 09:11.760
Get component of a player.

09:12.410 --> 09:13.700
And just like that.

09:15.290 --> 09:18.110
And then we're going to do player list.

09:19.010 --> 09:19.820
And.

09:20.470 --> 09:21.790
Player script.

09:22.150 --> 09:22.630
All right.

09:22.630 --> 09:26.050
So now we're going to have reference to the player script in the player list.

09:26.050 --> 09:31.570
And later on if we need to get one of the players from player manager from enemy or any other place,

09:31.570 --> 09:35.230
maybe camera that can be done through this variable.

09:35.230 --> 09:43.150
So I think since we do need this way, it would be a good idea to go here and make it as a get private

09:43.150 --> 09:43.810
set.

09:44.200 --> 09:51.310
So I'm going to do over here, get private set like that might look a bit confusing, but it's very

09:51.310 --> 09:51.580
simple.

09:51.580 --> 09:56.740
We just combined everything that we did before we made it public with a public getter, private setter.

09:56.740 --> 09:59.440
So it can be changed only inside the player manager.

09:59.440 --> 10:04.540
Then we make it a serialized field so it is visible in the inspector, and also we made it equals to

10:04.540 --> 10:05.080
the new list.

10:05.080 --> 10:08.260
So it has a list every time we start the game.

10:09.230 --> 10:11.090
Because we need to initialize the list.

10:11.510 --> 10:12.770
Uh, that's what we need to do.

10:12.800 --> 10:14.180
Okay, okay.

10:15.410 --> 10:21.110
Now let's go and make on player disconnected or on player removed method.

10:21.110 --> 10:24.770
Because when player getting killed we need to remove him from the list.

10:24.770 --> 10:28.070
So I'm going to go here and make player input manager.

10:28.910 --> 10:34.400
On player left plus equals remove player.

10:36.250 --> 10:38.320
Let's generate method as well.

10:38.560 --> 10:40.390
I'm going to take this method.

10:40.930 --> 10:42.850
Place it over here.

10:43.360 --> 10:44.380
Very nice.

10:45.040 --> 10:45.940
Before I forgot.

10:45.940 --> 10:47.950
Let's copy this line of code.

10:47.950 --> 10:49.180
Paste it in here.

10:49.180 --> 10:51.430
Let's do minus here.

10:51.430 --> 10:55.420
And then we can go to Remove player and do something very simple.

10:56.630 --> 10:58.310
Whereas this should be named as player.

10:58.310 --> 11:06.020
And then we're going to do player uh, with the name of player script equals to player Getcomponent

11:06.020 --> 11:07.550
of a player script.

11:08.450 --> 11:10.610
Sorry, we don't need script here, just player.

11:10.610 --> 11:14.090
And then we're going to do player list, remove player script.

11:14.090 --> 11:17.270
So we're going to take it away from the list okay.

11:17.270 --> 11:19.070
Let's go and double check that.

11:20.400 --> 11:21.510
Let's go now.

11:26.240 --> 11:30.770
Uh, I don't see the list, but I made it a serialized field, man.

11:31.580 --> 11:34.610
Uh, should be should be visible.

11:35.980 --> 11:36.520
You know what?

11:36.520 --> 11:37.300
Let's just.

11:38.430 --> 11:40.140
Not over complicated.

11:40.140 --> 11:41.220
I'm sorry guys.

11:43.360 --> 11:48.100
Let's make it as private without this.

11:49.100 --> 11:52.700
And then to get the list, I'm going to make a public method once I need it.

11:52.700 --> 11:57.320
If I need this list from another script, I'm just going to make a method that would return me this

11:57.320 --> 11:58.130
variable.

11:58.670 --> 12:00.470
Okay, let's go back now.

12:02.150 --> 12:05.120
Now we have player list over here that is empty.

12:05.150 --> 12:10.910
We can go to play mode, and this default player will not be in the list because it was there from the

12:10.910 --> 12:12.860
start of the game, but I'm going to kill it.

12:13.400 --> 12:16.910
Okay, now let me join the new player to the game.

12:17.360 --> 12:22.790
There is a player with a keyboard and there is this player clone over here.

12:22.820 --> 12:29.270
Now I'm going to join another one and there is another player in the list and we can keep track of them.

12:29.270 --> 12:32.330
There is also a couple of errors over here in the console.

12:32.330 --> 12:33.110
You can see.

12:33.110 --> 12:35.000
Let's just ignore them for now.

12:35.000 --> 12:39.710
Uh, they're not so important and they're not, you know, interrupting us from the development.

12:39.710 --> 12:42.200
If we have them in the end of the section, I'm going to fix them.

12:42.200 --> 12:45.530
But in my prototype, they just fixed themselves during the development.

12:45.530 --> 12:48.140
So we have no reason to be worried about this right now.

12:48.350 --> 12:50.120
Now, let's take care of something else.

12:50.120 --> 12:51.650
Something very important.

12:51.650 --> 12:57.830
As of now, we can join unlimited number amount of players to the game and we need to limit that.

12:58.040 --> 13:03.500
Uh, this player input manager has a variable that says limit number of players.

13:03.500 --> 13:05.810
If you make it true, you can set the number.

13:05.810 --> 13:09.050
Max player count can be one, two, three or whatever you like.

13:09.630 --> 13:13.650
The problem is that this variable is read only.

13:13.650 --> 13:15.900
You cannot change it in the script.

13:16.780 --> 13:19.420
You can go here and you can go player input.

13:19.420 --> 13:23.110
Manager max player count equals to one.

13:23.110 --> 13:27.340
And you can see it does not work because it will tell you that it is not accessible.

13:27.340 --> 13:28.510
It is read only.

13:28.660 --> 13:29.020
Yeah.

13:29.020 --> 13:33.640
So we'll have to interfere into the script a little bit.

13:33.970 --> 13:36.310
Let's go over here.

13:36.310 --> 13:39.310
Let's click Edit Script.

13:40.550 --> 13:42.290
And this is unity script.

13:42.290 --> 13:45.740
This is what makes player Input manager work.

13:46.040 --> 13:49.550
We're not going to just, you know, we're not going to look at the script.

13:49.550 --> 13:51.620
It doesn't really matter what is written here.

13:51.620 --> 13:55.100
We just need to do one simple adjustment.

13:55.520 --> 13:59.450
I'm going to look for max player count variable.

13:59.450 --> 14:01.100
And you can see there is this.

14:02.270 --> 14:03.110
To me.

14:03.110 --> 14:08.870
Yeah, there is this public integer max player count that we can use to read the value, and it returns

14:08.870 --> 14:12.500
us the max player count, which is private variable.

14:12.710 --> 14:14.000
It may be confusing.

14:14.000 --> 14:14.780
Don't worry about that.

14:14.780 --> 14:15.620
It doesn't matter.

14:15.620 --> 14:18.620
We just need to make a method that will change this variable.

14:18.620 --> 14:22.160
That's it and we need it many times in our custom scripts.

14:22.160 --> 14:31.040
So let's go just somewhere here and let's do public void set maximum.

14:33.080 --> 14:35.000
Layer count.

14:35.060 --> 14:38.750
And we're going to pass int maximum player count.

14:38.750 --> 14:40.640
And it's going to do something very simple.

14:40.640 --> 14:48.260
It's going to do m player count m max player count equals to maximum player count just like that.

14:48.560 --> 14:49.670
Now we're going to save this.

14:49.700 --> 14:51.290
We can close this script.

14:52.740 --> 14:57.540
And then in the player input manager over here, we can do player input manager.

14:58.410 --> 15:00.330
Set maximum player count.

15:00.330 --> 15:04.560
So now we can use this method that we just created to set up a player account.

15:04.590 --> 15:05.910
Why do we need a method.

15:06.000 --> 15:08.190
Actually why do we want to have it right.

15:08.190 --> 15:09.990
We can just change it in the inspector.

15:10.680 --> 15:12.060
Actually we cannot.

15:12.060 --> 15:15.420
Later on in the main menu we're going to have the.

15:16.700 --> 15:20.600
And the buttons that will define how many players will be there in the game.

15:20.600 --> 15:24.770
And as you know, if you want to do something from a button on the UI, you probably should have a method

15:24.770 --> 15:29.810
because you cannot just go from a button to the inspector over here and change value over here.

15:29.840 --> 15:30.770
That's the issue.

15:30.770 --> 15:32.900
So let's enable this.

15:32.900 --> 15:36.110
Let's set max player count to four by default.

15:36.110 --> 15:38.510
And then later on we're going to change it if we want to.

15:38.510 --> 15:42.980
As of now let's just make sure it's working by doing something very simple.

15:42.980 --> 15:47.450
Over here I'm going to do public int max player count.

15:47.450 --> 15:49.760
I'm going to make it equals to one by default.

15:50.480 --> 15:54.200
And then in the week I'm going to do player input manager.

15:54.810 --> 15:58.440
Set maximum player count to max player count again.

15:58.440 --> 16:03.120
Later on, we're going to call this method from a button, but as of now this will just do fine because

16:03.120 --> 16:05.130
we need to make sure it's working.

16:05.220 --> 16:05.850
Okay.

16:05.850 --> 16:06.810
Let's go back.

16:06.810 --> 16:13.020
I'm going to delete this player from here because I want to be able to join the player from the start.

16:13.020 --> 16:15.870
Let's select player manager and let's go to play mode.

16:17.680 --> 16:20.050
So no players.

16:20.050 --> 16:21.730
Maximum player count one is here.

16:21.730 --> 16:24.280
And also it did change variable over here.

16:24.280 --> 16:25.600
So we are fine.

16:25.600 --> 16:27.310
Now I'm going to join the player.

16:27.640 --> 16:28.690
There is one.

16:32.100 --> 16:36.120
And you can see a message that says maximum number of supported players reached one.

16:36.120 --> 16:39.060
If I try to join another one, I cannot.

16:39.390 --> 16:43.140
However, this message may be irritating in the future.

16:43.140 --> 16:44.760
I don't want to see it every time.

16:44.760 --> 16:49.740
So I'm going to go ahead to this message and I'm going to comment out this line of code.

16:49.740 --> 16:51.180
If you want to uncomment this.

16:51.180 --> 16:52.410
It is very easy to find.

16:52.410 --> 16:55.920
It is just very close to the method we created before over here.

16:56.370 --> 16:57.000
Okay.

16:58.330 --> 16:59.170
Good.

16:59.590 --> 17:01.090
Uh, let's save this.

17:01.090 --> 17:02.710
Let's go back to unity.

17:02.710 --> 17:05.020
And now player will not be joining.

17:05.440 --> 17:08.890
If you have set the limit, there's the first player.

17:08.890 --> 17:09.820
It's all fine.

17:09.820 --> 17:12.880
If you click buttons on a keyboard, it doesn't work.

17:12.880 --> 17:16.000
And now we can set the number to bigger value if you want to.

17:16.060 --> 17:19.150
And that is what we're going to do from the UI later on.

17:19.150 --> 17:20.800
As of now, this is just fine.

17:20.800 --> 17:25.870
There are other things we need to do with the player manager, but it may take more than a couple of

17:25.870 --> 17:30.760
minutes, so I think it's better to do it in the next video and I'll see you there.
