Welcome back my fellow game developers
to a brand new video.
And in this video we are going to
create a patrolling mushroom.
So now when we maximize on play and run the game,
you'll notice that if we walk around,
we find this mushroom starts chasing us and we get away,
just starts walking around from 1 to the next,
you feel as if he has a
predetermined us that he is going to be walking in.
So as you can see,
he walks from 1 to the next.
It keeps looking at us what those scary, scary eyes.
And if we get close,
he starts chasing us and shooting
with his little red box.
So let's not waste any more time and let's get started.
Oh, case sold, time for the last enemy.
And we have the Copeland wanderer.
We have the cowardly flying I.
And now time to introduce the rolling a mushroom.
Okay, so what does the patrol in
mushroom do while the patrolling mushroom
actually has a couple of
points that he walks to and from.
So we will add a couple of points.
So he moved from 1 to the next, to the next,
to the next to the next to the next,
to the next and so on.
And as he does this,
if the player gets close,
then we start chasing him.
So, and if the player gets in
the range with the range of shooting,
without getting in the range of the chase,
we also start shooting.
So let's get started.
The first thing that we're going to
do is we're going to go in
here and we're going to add another part.
So this is for the enemies that patrol.
And in here, first of all,
we're going to obviously like always
create a Boolean and say
that it should patrol, should patrol.
I don't know why I forgot where's
the P S on the keyboard.
Anyways.
Then we continue on with a serialized field,
which is the transform.
And it's an array of patrol points.
There we go. And finally,
we are going to create a variable to
keep track of the current patrol point.
So current patrol point or points or don't know,
it's just a point because it's a private integer.
Okay, great.
Now if we scroll down in here where we are chasing,
where we are wondering,
where we are running away.
We also need to check if we are patrolling.
So I'm going to show it to you as a challenge
and your challenge is to point and the right direction.
So first of all,
check if we should be patrolling.
That's pretty easy.
Then you'll need to determine
the direction the enemy should be moving.
And so it's the same way
as we did actually end the chasing enemy.
So you'll need to the position of the point and then
remove from it or subtract from
it the position of the actual enemy.
But then how are you going to
know when you got to this point?
While you'll need to keep track of the distance
between the enemy and the point he's going towards.
And if he's closed so around a feast less than
0.2 F increase the patrol point
so the enemy continues walking.
So I hope you understand what I meant
in nearby increase the patrol point.
We are going to start off at a certain patrol point,
and then we are going to keep increasing,
setting a new patrol point using
the current Patrol point that we've created.
If you feel that the last one where we increase
the point as a bit too hard for you, then no worries.
At least tried to do the first three.
I know the last one as I bit hard,
but if you feel up to it,
go to the Chaldean.
Okay, welcome back.
This was one of the hardest challenges I know. I admit.
So in here we're going to
create if we should be patrolling.
Whereas it then we are going
to set the direction to move in.
And it's going to be equal to patrol
points on the current patrol point.
But position minus the transform
of the current enemy dot position.
And then we are going to go ahead and create a,
or know what, let's just keep it up here.
The vector 3 dot distance,
it's the distance between the transform that
position and the patrol point dot position.
Copy that, paste them in here.
So now we have the distance,
and if that distance is less than 0,
so we need to,
first of all, so we'll call this the vector three nano,
actually, sorry, sorry, it's a float.
And the distance and
the me point is equal to the vector 3 distance.
So if the distance enemy point is less than 0.2 F,
so it's very close.
We don't want to have exactly 0.
We want to start moving to the next point.
As soon as we get to the first one,
we are going to increase the current patrol point by one.
And then we're going to check if we're at
the last current patrol point or I mean,
if the current neutral point is
greater than the patrol points,
patrol points dot length,
then we are going to set
the current patrol point back to 0.
So, you know, when we have
only a single line under the if condition,
then we can just put it like that, save that.
So what's happening in here?
We check, should we Patrol?
Yes, sure, we get the direction which is
the patrol point of position minus transform.
So we, just like we chase the player,
we keep track of the distance.
When that distance is less than 0.2,
we increase the patrol point.
That means that we again start
the update all the way back here.
This increases by one,
we get a new patrol point.
They transform, the position is removed,
we get the distance, we check,
is it then when we get to the last one,
we start going back the first one.
So we save that and let's go set up everything.
So I'm going to go in, where is it?
Here?
Wait for the script to compile,
and this guy is a trawler.
So should patrol.
Now let's go ahead and create a couple of points.
Now, this is a matter of debate.
What I like to do is just have the patrol points in here.
So right-click Create.
And why is it a debate point?
While mainly because you want to have for
each level its own patrol points.
So for example, you could create it inside of the room.
So you have the door,
the enemies, and the patrol points,
or you can just add them on the actual patrolling enemy.
And then every single time you put him in a new level,
you just suddenly patrol
points yourself. That's up to you.
I'm going to call this dV patrol, sorry, control points.
And then I'm going to create
an empty game object in here.
This will be the first control point.
So Patrol 0.
And why am I adding these brackets?
Well, because now when I duplicate this one,
I have the one immediately inside of these.
So this is a really cool trick.
Set it in here.
Send the second one here.
Third one here, this one here,
this one here, this one here.
And there we go.
I think that's enough points for today.
So there we go.
We start off 12345678,
and then back at the start.
And now I go to the patrolling enemy.
I will lock him down.
I will make sure to know before I apply the changes,
let me just set everything
in the Petro Points, save that.
Apply the changes, run the game.
And I don't want to maximize because I want to CV,
I actually want to look here.
So run the game.
Let's see how that looks. So there you go.
He starts walking.
And he keeps walking.
Girl. So not doing anything very smart.
That's because I think
we should keep track of the current point.
So I'll make this public just for
a couple of seconds so we know what is happening.
So C, run the game and we're at 0.
He goes to, should be going to the 0,
which is where is it?
I can see, oh, whoa,
so there is a problem and
what is the problem you might be asking?
Well, the obvious problem,
there's your patrol point, right?
If I move the enemy.
So this is something very clumsy for me.
The Patrol point moves with the enemy.
Why? Because the enemy is now
a parent of the patrol points.
So my smart behind
is going to put it inside of the rooms.
Yep. Open prefabs.
We don't want it anymore.
Okay. So now you are going to
make me do a lot of things, right?
So right-click prefab, unpack completely.
Now we can move this to the rooms and
now we can oh, wait a second.
Patrolling.
Okay. So we unpack patrolling points,
set them for the room.
Sorry for that guys.
That was a huge mistake.
I think that mistake should not be forgiven.
Seriously, that would not be forgiven.
So now we are going to,
where is it, The patrolling enemy?
If you can see right here,
if we double-click on him,
the prefab has this.
So what we'll do is we are going to delete
the patrolling mushroom and then create our own again.
So there we go.
So now when we run the game,
we should see a much better patrolling system.
There we go. You can see that he goes to the
first as soon as he gets there, There we go.
When he goes to the next point where
all the patrol points the second, the third.
Now to this point and look, as soon as he gets there,
he started going to the last point,
he shoots arrows. We are in the way.
He tries pushing us,
but we are stuck on the whatever it is.
So I move away.
Obviously, this is not the smartest enemy in the world,
so he will be
getting into things you need to make sure to put
the patrol points in ways that he will
not encounter any solid objects or obstacles.
So as you can see, he walks around, he shoots at us,
and he walked through his appropriate patrol point.
Now obviously you'll notice thir,
something that these spikes are rendering over the enemy,
which is something that we don't want.
This is why all the time I was
setting every game object like the health,
like the spikes on the player layer,
that was very wrong.
We should set them on a level object.
It's pretty easy to fix later on.
But just so you know,
this is not something that we want any ways with that,
out of the way, let's go back in here.
And one thing we want to do is just
like we had for the wandering enemy.
We want to also be chasing the player.
So in here, and if we're not chasing,
and if not is chasing, we patrolled.
And in here we chase the player.
Should chase player.
Which what are we selecting?
Sorry, so patrolling mushroom should chase player.
And that's also what was the yellow 10?
So the yellow one was to keep chasing the player.
So red, yellow.
And let's see, let's save that.
Let's run the game that's tried to have him JSON.
So let's make the game window bigger.
So we walk around.
We stand next to him.
There we go. You can see he starts chasing us and
shooting the little balls at us.
For some reason they are not shooting correctly.
I feel something is definitely wrong with the balls.
Maybe. I don't know. I see.
Yeah, yeah. Of course there is
something definitely wrong with the balls.
So even though when we get out of range,
he keeps patrolling, then when we are close,
he shoots at us.
So obviously there is a problem with the fire position.
I think it's hitting the head of the enemy.
It's interacting with it.
I'm not sure what the problem is, but for now,
this is something that we will need to
adjust either extremely fast.
So I don't know, maybe we do have a problem in here.
But the main thing here is that
we wanted to create the petroleum mechanics.
With that, I hope you enjoy,
I hope you had fun creating different types of enemies.
Now, our gain is really taking shape.
As you can see, we have the core mechanics now.
We have multiple enemies that move in different ways.
And obviously, games like these that are so
amazing and fun need something called a main menu,
a pause menu, and transitions between them.
So that's what we might be doing in the next section.
That out of the way,
I hope you enjoy it.
Make sure to commit your changes and I'll see
you in the next one.