Welcome back my fellow game developers
to a brand new video.
And in this one, as you can see,
we've created a little flying freaking demon,
but this time he is not a very brave.
He is a bit cowardly,
so he starts running away when we get close.
So now I walk around,
I get close and as you can see,
he starts running away and throwing his poop at us,
I think, or dirt or I don't know what that is.
So as you can see, we tried to shoot him.
We tried to get close, obviously his stock by the wall,
but you can see that when we
approach from a different direction,
still his stock on the wall,
but no worries about that.
You see that he is running away from us.
So let's not waste any more time and let's get started.
Okay, so as you can see,
I've created for more enemies,
the cowardly flying, I,
the goblin wonder one.
I don't know why it's called Wonder one,
but anyways, that's his name.
And of course we have the skeleton and
finally we have the patrolling mushroom.
So we are going to be using
all of these in every single video.
And as I've said, I don't want to go
through the process of doing this with you.
All I did is I just duplicated the skeleton,
change the body sprite,
change the animator, the animation and all of that.
I also made sure to change and here
the animation and so on and so forth.
So now they all chases and do the same.
But what we're going to do is we're going to
remove the cowardly or the goblin Wanderer,
the patrolling mushroom, and the skeleton enemy.
And we're just going to keep the cowardly flying I,
and this is what we'll use to create our cowardly enemy.
So without any further ado,
let's go ahead and open up the control.
Oh, and by the way, I've also created
a projectile for each one of those.
So each one has his own projectile.
I've created that too.
But anyways, and let's
open up the enemy controller in here.
And let's get started with a couple of goats.
So the first thing that we need is we need to go up
here and we'll need to add a bit of space.
And we're going to say that these are the enemies that
runaway and it means that run away.
And in here I'm going to create
two serialized fields which are going to be
a Boolean for the should run away.
And then another serialized field for
the float run away range.
So R1 way range.
And obviously because we want to have
a certain range at which
the enemy will start running away.
Now, there is another thing that we need to add
because we are creating enemies that should run away.
We also should know
the enemies that should be chasing the player.
So I'm going to again create another one in here,
which is going to be the,
or know what up here we've created something.
And he made the attacker within the news that
yet player move direction,
player to chase player chase range.
Okay, so in here I'm going to create
this and have the enemies that chess player,
enemies that chase the layer.
And in here I'm also going to create
a serialized field for the Boolean.
There's going to be the should chase player.
There we go. So now we have
the option to choose one of these two.
And let's see what we need to do before we continue on.
We need to add this should chase player condition.
So where do we add it?
First of all, we need to add it obviously
in when we are giving velocity.
So in here we update
looking animating enemy moving towards the player.
So there we go, moving towards the player.
And here we need to make sure that we are also
having and we should chase the player.
So in here we get the direction which is the player
to chase position minus
the current position of the enemy.
And then we go there and we said the is chasing.
And in here as you can see,
I have redundant code.
These two lines are exactly the same.
So what I'm going to do is I'm
going to create a variable in here,
which is going to be called,
so distance is a float.
So we're going to create a float,
which is going to be the distance,
distance player, enemy.
And it's going to be equal to the vector distance,
et cetera, et cetera.
And instead of this long line,
I'm going to say that it's the distance layer enemy.
Add a bit of space.
There we go. And now double-click in here,
copy that, paste it in here it's the same, right?
So transform position, player
chase dot position, exactly the same.
There we go.
We save that.
And also in here we need to make sure that he is
chasing and is the should chase player.
There we go, save that.
And with that done,
let's continue on and see what we have.
Now in here.
We already know what happens
if we should be chasing the player.
But what happens if we should
be running away from the player?
I'm going to create a separate if condition in here,
which is going to say.
If we should be running away or we should run away.
And where does it end the distance?
Player enemy is less than the runaway range.
So the player is in range,
then we are going to create the direction to move in.
But this time, instead of getting the direction
from the player to chase
position to the transform position,
what we'll do is we'll set
the transform dot position minus the player
to chase dot versus player to chase their hours,
player to chase dot position.
So what this will do is it will
create the same direction to move in,
but in the opposite way.
So this will be negative.
So instead of moving towards the player,
we are going to be moving away from the player.
So we're going to save that.
We are going to go back into our game.
And let's see what we have in here.
So we need to change a couple of things.
So we need to make sure that any day now.
And the new update of unity is very, very annoying.
I've not so many mistakes,
so many glitches in it.
I've had many crashes.
So you can download a better version than the
2020 0.3.15 F2 or whatever it is,
make sure you do that
because I'm not liking this version.
I mean, look how slow it is anyways, any ways.
First of all, is chasing.
We need to make it private
because I don't want to be seeing it all the time.
We don't need to change it private.
And the male, the attacker.
Now what because we won't be implementing
it in this area,
in this part of the course.
Let's just remove it just so we
have a bit more space in here.
So in here we can choose to chase the player
and we need to make sure that we should run away.
And what is the runaway range?
Well, I'm going to issue you a challenge
so that we know what the runaway range is.
And that is to create
a blue circle around the enemy or whatever they want.
So find the onDraw gizmos in it,
create a condition for
the enemy that should be running away.
And choose the blue color or any color that you
want and draw, obviously a sphere.
And as an extra challenge makes sure that we do
not draw any unnecessary circles.
So create also a condition for the chase range,
for example, we don't want to see it if
we are not chasing player.
So that pause the video right now and go to the chat.
Okay, welcome back.
So I'm going to scroll down and he also mainly attacker.
Attacker was actually being used in here.
Okay, no worries about that.
We will remove this.
Okay, so back down here on the,
on drug is most.
So in here we can see that we get the player chase range.
So if we should chase player,
then we are going to,
of course, choose these two and put them
inside the player keep chase.
This is also something that should be inside of
the player chase range.
Or I mean in the should chase player.
There we go. If the shooting range okay,
for that, Let's keep it as it is.
And finally, we're going to
create an if statement in here,
which we'll say if we should runaway,
then we are going to get
the gizmos dot color
and it's going to be equal to color also.
We are already using blue,
so let's use yellow, green, green.
So there we go, green.
And we're going to copy this line in
here because it's almost the same except for the,
of the shooting range, the runaway range.
Save that.
Remove the extra spaces in here.
And now we have a way to
measure the distance that we are going to be
running away with k. So there we go.
We have the blue range,
which is the one for the shooting.
Okay, great. And then we have the runaway range.
So okay, so when the player gets in this range,
the enemy should start running away.
So that's run the game.
And let's see how that works.
So now we are in the game.
We come close to the enemy.
There we go, you can see that he is running away from us.
If I tried to move faster, he starts moving.
Well, that's because he is slow.
Obviously we are going to make him a
bit maybe faster because he's a flying enemy.
So now when I tried to come close,
he keeps getting away.
So hey, well, and he even
called us because as we are chasing him,
he is throwing projectiles.
Honest.
And also one thing I forgot to mention,
I changed the five position.
Nope, it's the same place.
I'm going to make it on top of his head or maybe,
you know what, let's just keep it where it was.
I think that was good. Anyways, I hope you enjoy it.
I hope you understood what is happening in
here because where is it?
Okay?
So if we should run away, instead of,
we have the player position and
bigger than the transformed position.
Or at least not, not just
bigger it on the other direction.
If we choose to remove
the transform position from the player to chase position,
we get a positive,
so we are moving towards the player.
If we remove our position from the player's position,
then we are moving away from the player.
So with that done, I hope you enjoy it.
And the next video we will be creating the wanderer.
So I'll see you then.
And don't forget, of course,
to commit your changes every single time.