Welcome back my fellow game developers to a brand new video.
And as you can see,
we have now a yellow circle.
What does this yellow circle do when we run our game?
You can see that if I go inside the yellow circle,
nothing happens and just make the enemy speed a little bit less.
If I go into the red circle as normal,
he starts chasing us.
I tried to get away.
I'm inside the yellow circle,
but he is still chasing us.
What is happening?
The only way to win is to go outside the yellow circle.
Also, that's how you get away from
the enemy because he started now it doesn't want to chase you anymore.
So let's not waste any more time.
Let's get started.
And I forgot to tell you that you need to commit your changes at the end.
Don't forget to do that.
So let's start k.
So now we have the enemy knowing
where the player is based on a certain distance he is from him.
When the player gets on a certain distance,
a certain chase range,
the enemy will know and he will start chasing the player.
Well, he doesn't do that physically,
but we can see from the logic and the code that he knows,
how are we going to do this?
First thing first, we are going to determine the direction this enemy will be moving in.
So if the distance is less than the player chase range,
I'm going to stop the debugging and I'm going to save that the direction to move in,
it's going to be equal to the player chase range or nonalignment Eclair to chase layer
to chase dot position minus the transform position.
So this is just like the formula that we use.
The 2 the gun and the right direction,
we choose the position,
we remove the position of the player to chase.
We subtract the position of the enemy from
the player to chase position and that way we get the direction.
So it's similar to the way we are pointing our gone,
you can go back and see how that works.
Now what we're going to do after we know the direction that we're moving in,
we are going to normalize that direction.
Because we don't want the enemy to be faster when he moves diagonally.
So we normalize.
And then we are going to access the rigid body.
So the enemy rigid body get the velocity and it's going
to be equal to the direction to move n times the enemy.
Speak.
Save that.
And now let's go back to our game.
Let's see how that works.
I'm going to set the game view next to the scene view so we can see how the range works.
When we get into range,
I'm going to run the game.
So now as I approach the enemy,
yep, there we go, nothing happens.
Why is that?
Well, because we have the small error in here and that the object,
we don't have a reference to width.
Why is that?
Because we are trying to access the enemy rigid body,
but we haven't set it.
So the NMI rigid body,
the dot is equal to the debt component, rigid body 2D.
There we go.
Okay, now everything should be working fine.
Let's stop the game, run it again.
And now we're getting close. And there you go.
You can see that the enemy is actually chasing us.
And obviously we need to fix a couple of things,
but it is working.
What happens if we are out of range?
I'm trying to get out of range.
Okay. So it doesn't work.
Let me just first of all,
well at least it's working.
It's not perfect, but it's working.
So let me set this one.
Let me make the range a little bit smaller because I want to demonstrate something.
And let's make the speed around too so we can escape.
I'm going to run the game.
I'm going to get close to the enemy and then I'm going to stop, I'm runway.
So as you can see,
the enemy is still walking and finally he hits a wall.
So what is the problem in here?
The problem is that the enemy will still have the momentum with them.
So I get close, I move away.
And even if I'm not and the range is still walking mindlessly, now,
this would be not very bad if he was a zombie,
but apparently he's a skeleton,
so he should stop.
Okay, I should stop though jokes.
And let's see what we can do.
So in here, what we should do is when we are in range,
we are chasing the enemy.
If we're out of range,
the direction to move in will be equal to vector 3, 0.
There we go. That was the direction to move in and vector three,
okay, Yes, it was. So save that.
So now when we get out of range,
we should stop chasing the player.
So let me know what.
Let's use a different view, a different layout.
We can use the two-by-three.
There you go. This is a nice way also to view the Unity engine.
So you can see right now change this from three aspects to 19 by 1920 by 1080.
Now we can see it scene view clearly and the game view clearly.
We run the game and we get close and to the chase range.
There we go.
You can see he keeps chasing us when I'm outside of that range.
You can see that he immediately stops.
There you go. You can see as soon as I'm in, he stops.
When I'm out, he stops chasing.
Okay, great.
So what that done?
I hope you enjoy it.
Now we can make the chase range a little bit bigger
because we know if we get far away enough from the enemy.
There you go.
You can see he stops chasing girls.
Now, you can add an extra layer of intelligence to the enemy if you want,
you can create a stop range.
For example, let's say we have this range,
we have another range.
And if the player,
if we can get out of that range,
the second one, then the enemy will stop chasing us.
And that's going to be actually a challenge for you.
So how do you like the transitions to the challenge now, there are better, right?
I got you off guard there, didn't I?
So I hope create a keep chasing range variable.
You'll need to draw that range using the gizmos.
You need to create a boolean is chasing.
That is true when chasing and false when we're outside the certain range.
And finally, you will need to create an else if statement that checks if
we're chasing and we are inside the keep chasing range.
So I hope you understood the challenge.
It's a bit hard.
It will need a bit of configuring,
but I'm sure that you are up to the challenge.
So pause the video right now and go to the Shalon case.
So first thing we're going to do is we're going to create a new variable in here,
which is going to be the keep chasing.
And we'll call it the player keep Chase range.
And then we're create a Boolean.
And here which we are going to call,
I'll make it private or know what,
let's make it public so we can see it and then we'll make it private.
And public boolean is chasing.
So the next step,
what was the next step and the challenge that we see, draw,
okay, so we're going to draw the circle right here.
I'm just going to take this and duplicated.
So Control D as always,
let's make it yellow.
And this will be the keep chases range.
Save that.
And now what do we have to do next?
We need to create a Boolean as tracing with that.
And okay, so when is it true?
It's true in here when we are inside the range and we start going after the player.
So is chasing is equal to true.
Then what happens if we go outside this range?
The is chasing becomes false.
But in between, we want to create an if else or else, if.
Else, if the vector or the distance of the player
and the enemy is still less than v0, keep Chase range.
And so this is for, is chasing.
What we're going to do is we are going to keep the direction as it is.
There you go.
Let's save that. Let's look one more time and this code and see if everything works.
So we've created the range,
we've made it yellow.
What happens in here is when the player enters the range of chasing,
the direction will become the player to chase.
And then we are going to set the is chasing to true.
Then we are going to check if the player can get
outside or if the player is still inside the keep Chase range.
And we are chasing,
then we will have the direction to move and still in the player.
Else.
If we are outside,
both the players chase range and they keep Chase range,
then is chasing is false.
And what's great about this is if we go inside of the player keep Chase range,
the enemy will not follow us because it's chasing as false.
So let's see if that actually works.
And I'm going to increase the keep Chase range.
Let's make it.
Yep, there we go. I think that's good enough.
I'm going to run the game.
And now let's see if I walk inside the keep Chase range.
Nothing happens.
If I walk inside the chase range,
the enemy starts chasing.
If I am, I can actually push him.
If I am outside the chase range,
he still follows me.
What if I go outside both of the ranges?
He stops following.
And you can see right here that is chasing is false.
So he doesn't chase me.
When is chasing is true?
He keeps chasing until I go outside the yellow circle.
So I hope you do the challenge.
I hope it wasn't too hard.
I know when I did this challenge,
I felt that it might be a bit too hard,
a bit too confusing.
But I hope you understood what the point of the Boolean was.
I hope you understood how the else if statements here worked.
If you didn't re-watch the video,
I think I explained it well enough,
so I hope you enjoy it and I'll see you in
the next video where we will be actually animating our enemy.
Because as you can see,
he's very boring currently.
Until then, have a nice day and I'll see you then.