WEBVTT

00:07.240 --> 00:14.800
In the previous lecture, I left you with the challenge of creating a spawn to spawn patients.

00:14.920 --> 00:20.150
All right, so what we're going to do now is to actually write that, and I'll show you my method.

00:20.170 --> 00:26.110
Yours might be a little bit different, but as long as it gets the patients instantiated and onto your

00:26.110 --> 00:28.000
map, then we're all good.

00:28.120 --> 00:33.840
Okay, so first of all, we actually need our patient to be a prefab.

00:33.850 --> 00:40.750
Now, if you remember, he did start out as a prefab, so we've made some changes to our basic patient.

00:40.760 --> 00:46.270
So you want to go and click on your patient and then let's just drag that in and I'm going to come up

00:46.270 --> 00:52.210
into the inspector and we want to override all of these things that we've done to our patient.

00:52.210 --> 00:58.570
So click on apply all to apply those back to the prefab.

00:59.020 --> 01:04.630
Okay, so now let's create a spanner in our hierarchy.

01:04.630 --> 01:07.480
Let me just scroll all of these things up.

01:08.020 --> 01:13.400
We're going to right click and create an empty of spanner.

01:14.080 --> 01:14.310
Okay?

01:14.440 --> 01:17.440
And that's going to be the position of where our spanner will be.

01:17.440 --> 01:21.210
Now, it's currently just positioned there, so let's drag it over here.

01:21.220 --> 01:27.370
Now, if you want to see where it is, besides these axes, you can use the trick that I've done before

01:27.370 --> 01:34.480
with the waiting area and the reception, which is to add a cube mesh renderer or a box collider or

01:34.480 --> 01:36.220
something so that you can actually see it.

01:36.250 --> 01:41.710
Now it needs to be outside of the hospital and it needs to be near the ground.

01:41.710 --> 01:43.030
Now the ground's at zero.

01:43.030 --> 01:46.690
So over in the inspector I'll have a look at my Y for that.

01:46.690 --> 01:49.540
I'm going to actually set it to, let's say seven.

01:49.540 --> 01:53.920
It doesn't matter if it's slightly up in the air because they will spawn there and then just drop down

01:53.920 --> 01:56.650
to the ground and then move in.

01:56.830 --> 01:57.310
All right.

01:57.310 --> 01:59.380
So let's leave that there now.

01:59.380 --> 02:01.960
We're going to create the code for that.

02:01.960 --> 02:06.100
So create C-sharp script and call it Spawn.

02:06.670 --> 02:10.210
And let's go ahead and open that up and edit it.

02:10.840 --> 02:14.950
It's going to require, first of all, our game object.

02:14.950 --> 02:16.030
So our prefab.

02:16.030 --> 02:21.820
So it'll be public game object patient prefab.

02:24.860 --> 02:27.740
And then how many patients we want.

02:27.740 --> 02:31.370
So let's put that in here public int num.

02:32.630 --> 02:33.740
Patients.

02:35.130 --> 02:42.060
And then down in start we're going to create a for loop for int i equals zero.

02:42.090 --> 02:47.040
I is less than num patients.

02:48.160 --> 02:48.460
All right.

02:48.520 --> 02:48.790
Plus.

02:48.790 --> 02:49.570
Plus.

02:50.080 --> 02:55.120
And then inside this loop, we will instantiate our patients.

02:55.310 --> 03:02.470
Okay, so we want to instantiate, and we're going to use our patient prefab.

03:03.040 --> 03:11.830
And let's see, we want to put it at this stop transform position, which is your spawn of position

03:11.830 --> 03:21.430
and turn in identity for basically a default rotation and we spell transform correctly.

03:21.430 --> 03:28.300
Now the problem with this that I'm just thinking now is if we loop through this all in the start, we're

03:28.300 --> 03:32.770
going to end up with what, 100 patients straight away?

03:32.800 --> 03:34.520
Now, that might be what you want.

03:34.540 --> 03:38.170
I'd rather have this spawning them at particular intervals.

03:38.170 --> 03:42.160
So instead I'm going to go down here and put void.

03:42.880 --> 03:45.370
Let's go spawn patient.

03:47.090 --> 03:51.890
And inside of here I will move this instantiate code.

03:51.890 --> 03:55.800
So control x, put that down there.

03:55.820 --> 04:01.010
Now that actually makes our NUM patients and our loop superfluous.

04:01.010 --> 04:06.350
But now you've seen how you would do that if you wanted to create a whole bunch of patients all in one

04:06.350 --> 04:06.920
go.

04:07.010 --> 04:09.440
And let's just leave that there actually.

04:09.440 --> 04:10.910
And we will do that anyway.

04:11.300 --> 04:12.920
So we will then.

04:14.540 --> 04:16.730
Put this instantiate back up there.

04:17.870 --> 04:20.000
So this is all your initial lot of patience.

04:20.000 --> 04:22.190
And then what we're going to do is invoke.

04:24.200 --> 04:25.640
Spawn patient.

04:32.680 --> 04:41.020
After, say, 5 seconds and then inside of spawn patient, we will get it to invoke itself.

04:45.180 --> 04:49.530
So you keep getting patients being spawned.

04:50.220 --> 04:56.640
And now in here we're going to have random range for the time between patients.

04:56.640 --> 05:03.840
So let's say we want patients every 2 to 10 seconds that's going to do it.

05:03.840 --> 05:07.590
So now you can see two ways that you could actually spawn, right?

05:07.590 --> 05:16.650
So we'll save this and let's go back into unity and we want to get our spawn script and drag it onto

05:16.650 --> 05:19.350
our spawn over here.

05:19.440 --> 05:26.190
And then we're going to assign our patient prefab, which is back in our prefabs folder.

05:26.190 --> 05:31.980
So if we go to our Prefabs folder, we've got our patient there, we can drag and drop him into the

05:31.980 --> 05:35.130
patient prefab and the number of patients.

05:35.130 --> 05:41.130
This is the initial number that get created and then one gets spawned every 5 seconds or whatever it

05:41.130 --> 05:41.950
was set to.

05:42.000 --> 05:48.750
So let's create an initial amount of five and then they're going to drop out of the sky basically and

05:48.750 --> 05:50.550
start coming into the hospital.

05:50.550 --> 05:56.250
Now, this little guy here that's already in the hospital, you can delete him or just turn him off

05:56.250 --> 05:58.980
if you want to use him for testing things a little bit later.

05:59.220 --> 05:59.700
Okay.

05:59.700 --> 06:04.290
So let's just press play and have a look at what we get here.

06:04.290 --> 06:10.320
So there's our five that are initially spawned and they're all coming into the hospital and then here

06:10.320 --> 06:11.700
comes the extra ones.

06:11.700 --> 06:17.610
So if we go over into our game view here, let me just set this to a high resolution image for you.

06:17.790 --> 06:23.100
Now they're going to come and register and then once they've registered, they will move away.

06:24.390 --> 06:28.710
After they shuffle past each other and then go into here.

06:29.070 --> 06:29.520
Right.

06:29.520 --> 06:34.290
So some little bits of debugging information at this point.

06:34.290 --> 06:43.250
If you find that they are not getting to their destination, skipping over steps or whatever, like,

06:43.410 --> 06:46.710
you know, it's going to happen as you're trying to test this.

06:46.860 --> 06:55.620
The most common thing in this particular scenario, or the use of the nav mesh is that the agents can't

06:55.620 --> 06:57.700
reach their destination.

06:57.720 --> 07:02.380
For example, down here, we're getting a big pile up in this waiting room.

07:02.400 --> 07:10.190
Now, remember that the waiting room has a specific location, which is one little tiny point.

07:10.200 --> 07:16.020
If I select that and let's just come over to here, it's right there.

07:16.020 --> 07:23.550
So that means the agents have to get to that specific point to be considered in the waiting room.

07:23.550 --> 07:27.870
And if we have a look in the code for this, let's just pause.

07:28.020 --> 07:38.910
We're going to go back into the agents scope code and have a look at our G agent code in order to consider

07:38.910 --> 07:41.040
that they've actually reached a destination.

07:41.040 --> 07:46.230
If we have a look in the late update, we've got this line here for remaining distance.

07:46.230 --> 07:51.900
So it's going to consider that you've reached a destination waypoint on the nav mesh.

07:51.900 --> 07:55.190
If you're within one distance.

07:55.200 --> 08:00.570
Now, if you're using unit sized agents like I am, I find that one is quite acceptable.

08:00.630 --> 08:06.270
You might want to make this a little bit bigger if you find that your agents just aren't reaching that

08:06.270 --> 08:07.440
location.

08:07.830 --> 08:14.520
Now, the other thing that could happen is if you set this to 20, then the agent will consider himself

08:14.520 --> 08:20.370
having reached a waypoint when he's like 20 units away, which will make the actions look a bit odd.

08:20.370 --> 08:26.040
So it might make an agent look like he's reached the waiting room when he's still standing out here,

08:26.040 --> 08:27.840
which is something that you don't want.

08:28.380 --> 08:29.810
So that's that's one thing.

08:29.820 --> 08:38.790
Now, any agent that doesn't reach a waypoint will still be trying to execute one of the actions.

08:39.120 --> 08:39.510
Okay.

08:39.510 --> 08:43.170
And you can tell if you select an agent.

08:43.170 --> 08:46.170
So let's find someone who's actually still doing stuff.

08:46.170 --> 08:53.610
This guy here and over in the inspector, let's find his parent patient.

08:54.090 --> 08:57.240
The go to waiting room, go to hospital and register.

08:57.240 --> 09:04.380
If you have a look on all of those things, you'll see that there is a public tick box at the bottom

09:04.380 --> 09:11.460
called running that will tell you which action he is still trying to run.

09:11.550 --> 09:16.170
So this particular agent over here, he's at the reception desk.

09:16.170 --> 09:18.060
So he's obviously still registering.

09:18.060 --> 09:23.430
And if we have a look at his register, it is tick to say that it is still, in fact, running.

09:23.430 --> 09:30.330
Now, if we press and press pause and wait for a moment until he's finished, you can see now that that

09:30.330 --> 09:35.250
has been ticked and he's now going to the waiting room instead.

09:36.390 --> 09:37.140
Okay.

09:37.140 --> 09:43.680
So now whilst this going on, let's pause again another way to have a look at debugging.

09:43.680 --> 09:51.030
This is if we go into the console, you'll see that it's printing out the plans.

09:51.240 --> 09:57.810
And so here you can see it says the plan is go to hospital, register, go to waiting room.

09:57.810 --> 10:04.950
So this plan is actually happening in order and I've collapsed down what's going on in the console because

10:04.950 --> 10:08.010
it will just be writing this out for every particular agent.

10:08.010 --> 10:13.110
But this is showing you that this is what the plan is that they've come up with, which is the plan

10:13.110 --> 10:15.300
that you wanted them to come up with.

10:15.300 --> 10:18.600
So that is at least all good, right?

10:18.600 --> 10:24.570
So that is getting a whole bunch of agents running and spawning them.

10:24.570 --> 10:30.600
When we come back in the next lecture, I'll have to talk about a little bit more of debugging these

10:30.600 --> 10:37.950
scenarios because that's actually quite a big part of this, and we'll begin working on getting our

10:37.950 --> 10:39.300
nurse to run.
