In the previous lecture, I left you with the challenge of creating a spawn to spawn patients.
All right, so what we're going to do now is to actually write that, and I'll show you my method.
Yours might be a little bit different, but as long as it gets the patients instantiated and onto your
map, then we're all good.
Okay, so first of all, we actually need our patient to be a prefab.
Now, if you remember, he did start out as a prefab, so we've made some changes to our basic patient.
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
into the inspector and we want to override all of these things that we've done to our patient.
So click on apply all to apply those back to the prefab.
Okay, so now let's create a spanner in our hierarchy.
Let me just scroll all of these things up.
We're going to right click and create an empty of spanner.
Okay?
And that's going to be the position of where our spanner will be.
Now, it's currently just positioned there, so let's drag it over here.
Now, if you want to see where it is, besides these axes, you can use the trick that I've done before
with the waiting area and the reception, which is to add a cube mesh renderer or a box collider or
something so that you can actually see it.
Now it needs to be outside of the hospital and it needs to be near the ground.
Now the ground's at zero.
So over in the inspector I'll have a look at my Y for that.
I'm going to actually set it to, let's say seven.
It doesn't matter if it's slightly up in the air because they will spawn there and then just drop down
to the ground and then move in.
All right.
So let's leave that there now.
We're going to create the code for that.
So create C-sharp script and call it Spawn.
And let's go ahead and open that up and edit it.
It's going to require, first of all, our game object.
So our prefab.
So it'll be public game object patient prefab.
And then how many patients we want.
So let's put that in here public int num.
Patients.
And then down in start we're going to create a for loop for int i equals zero.
I is less than num patients.
All right.
Plus.
And then inside this loop, we will instantiate our patients.
Okay, so we want to instantiate, and we're going to use our patient prefab.
And let's see, we want to put it at this stop transform position, which is your spawn of position
and turn in identity for basically a default rotation and we spell transform correctly.
Now the problem with this that I'm just thinking now is if we loop through this all in the start, we're
going to end up with what, 100 patients straight away?
Now, that might be what you want.
I'd rather have this spawning them at particular intervals.
So instead I'm going to go down here and put void.
Let's go spawn patient.
And inside of here I will move this instantiate code.
So control x, put that down there.
Now that actually makes our NUM patients and our loop superfluous.
But now you've seen how you would do that if you wanted to create a whole bunch of patients all in one
go.
And let's just leave that there actually.
And we will do that anyway.
So we will then.
Put this instantiate back up there.
So this is all your initial lot of patience.
And then what we're going to do is invoke.
Spawn patient.
After, say, 5 seconds and then inside of spawn patient, we will get it to invoke itself.
So you keep getting patients being spawned.
And now in here we're going to have random range for the time between patients.
So let's say we want patients every 2 to 10 seconds that's going to do it.
So now you can see two ways that you could actually spawn, right?
So we'll save this and let's go back into unity and we want to get our spawn script and drag it onto
our spawn over here.
And then we're going to assign our patient prefab, which is back in our prefabs folder.
So if we go to our Prefabs folder, we've got our patient there, we can drag and drop him into the
patient prefab and the number of patients.
This is the initial number that get created and then one gets spawned every 5 seconds or whatever it
was set to.
So let's create an initial amount of five and then they're going to drop out of the sky basically and
start coming into the hospital.
Now, this little guy here that's already in the hospital, you can delete him or just turn him off
if you want to use him for testing things a little bit later.
Okay.
So let's just press play and have a look at what we get here.
So there's our five that are initially spawned and they're all coming into the hospital and then here
comes the extra ones.
So if we go over into our game view here, let me just set this to a high resolution image for you.
Now they're going to come and register and then once they've registered, they will move away.
After they shuffle past each other and then go into here.
Right.
So some little bits of debugging information at this point.
If you find that they are not getting to their destination, skipping over steps or whatever, like,
you know, it's going to happen as you're trying to test this.
The most common thing in this particular scenario, or the use of the nav mesh is that the agents can't
reach their destination.
For example, down here, we're getting a big pile up in this waiting room.
Now, remember that the waiting room has a specific location, which is one little tiny point.
If I select that and let's just come over to here, it's right there.
So that means the agents have to get to that specific point to be considered in the waiting room.
And if we have a look in the code for this, let's just pause.
We're going to go back into the agents scope code and have a look at our G agent code in order to consider
that they've actually reached a destination.
If we have a look in the late update, we've got this line here for remaining distance.
So it's going to consider that you've reached a destination waypoint on the nav mesh.
If you're within one distance.
Now, if you're using unit sized agents like I am, I find that one is quite acceptable.
You might want to make this a little bit bigger if you find that your agents just aren't reaching that
location.
Now, the other thing that could happen is if you set this to 20, then the agent will consider himself
having reached a waypoint when he's like 20 units away, which will make the actions look a bit odd.
So it might make an agent look like he's reached the waiting room when he's still standing out here,
which is something that you don't want.
So that's that's one thing.
Now, any agent that doesn't reach a waypoint will still be trying to execute one of the actions.
Okay.
And you can tell if you select an agent.
So let's find someone who's actually still doing stuff.
This guy here and over in the inspector, let's find his parent patient.
The go to waiting room, go to hospital and register.
If you have a look on all of those things, you'll see that there is a public tick box at the bottom
called running that will tell you which action he is still trying to run.
So this particular agent over here, he's at the reception desk.
So he's obviously still registering.
And if we have a look at his register, it is tick to say that it is still, in fact, running.
Now, if we press and press pause and wait for a moment until he's finished, you can see now that that
has been ticked and he's now going to the waiting room instead.
Okay.
So now whilst this going on, let's pause again another way to have a look at debugging.
This is if we go into the console, you'll see that it's printing out the plans.
And so here you can see it says the plan is go to hospital, register, go to waiting room.
So this plan is actually happening in order and I've collapsed down what's going on in the console because
it will just be writing this out for every particular agent.
But this is showing you that this is what the plan is that they've come up with, which is the plan
that you wanted them to come up with.
So that is at least all good, right?
So that is getting a whole bunch of agents running and spawning them.
When we come back in the next lecture, I'll have to talk about a little bit more of debugging these
scenarios because that's actually quite a big part of this, and we'll begin working on getting our
nurse to run.