Welcome back, my fellow game developers.
In this video, we added
a shooting range. It's pretty simple.
We've already done this.
You can see right now the enemy no longer
shoots us unless we stop in this red circle.
When we do stuff in,
he starts throwing his sorts arrows.
If we're out, he does not throw them.
Now, let's not waste anymore time.
This video isn't just about creating the range.
We also had an extra,
let's call it lesson.
And here, an extra point to create.
We use the knowledge of our methods.
So let's not waste any more time
talking and let's get started.
Oh, okay, so time to add a shooting range to our enemy.
But before we do that,
there is one thing that we need to
take care of and that is that
the sword wasn't getting
destroyed when it hit some kind of trigger.
And you might be thinking,
well, why is that happening?
Even though we have the enemy bullet
on trigger, everything is working.
There is a small detail that
I should have told you I made the mistake.
We do need a rigid body 2D in here,
but we need to make it kinematic.
So now when the flying salt
has a rigid body to the end Is Kinematic,
it will be destroyed when it hits
a wall with that out of the way.
Excuse me for not saying that before I
told you you shouldn't have a rigid body.
We should, but it should be kinematic.
So let's continue on. What do we need?
We need to have the ability to show
the player only at a certain range.
How do we do that?
By giving you a challenge?
And your challenge is to create
a shooting range with color.
So create a float variable for the shooting range.
Before we can shoot,
make sure we are in the shooting range by checking
the distance between the enemy and
the player and draw a yellow.
I'm not sure why I said the
yellow because we already have yellow.
Choose whatever color you want and make sure
that that is the shooting range.
Makes sure that the enemy doesn't shoot
unless the player is in that shooting range.
So pause the video right now and go do the challenge.
Okay, welcome back.
So I'm going to go into the enemy controller.
And where do we shoot? There we go.
This is the time between shots.
And let's go ahead and added.
I think this is a good place to add it.
So I'm going to create a serialized field,
which will be a float,
and it's going to be the shooting range.
Save that.
And then I'm going to scroll down in here.
I'm going to check if we are not
a male attacker and we are ready to shoot,
then I'm going to go ahead
and up here right before we check for the condition,
create a variable which is going to be the float.
Or know what, let's not create a, an extra variable.
Let's just put it in here.
We'll add another condition.
And that condition will be that a
vector three and calculate
the distance between the layer to chase.
There we go the player to chase
dot transform dot position.
And we are going to check for
the transform dot position of the enemy.
And if it's less than the shooting range,
then we are going to start the condition.
Now you've noticed that this line is a bit long.
So what we can do is simply go this,
hit enter, go in here, hit Enter.
So now they are on separate lines.
We can see them more clearly.
If you want, you can keep them on the same line.
And this is a better way of looking at the conditions.
We can also add this right here so we know everything.
With that done, we're going to save oh,
and we need to draw the longest most.
So I'm going to go ahead and duplicate these two lines.
And I'm going to make the color not yellow.
Let's make it blue.
And this is going to be the shooting range.
Save that back in our game.
Let's see the enemy.
Now, what do you want the shooting range to be?
Of course, it will depend on how the game will be.
So for example, you can make
the shooting range a bit bigger,
but maybe just maybe you would want
the player chase to be around like that.
So the enemy might need to chase the player a bit.
And when he is in the range he started shooting,
or we can do whatever you want.
It depends on your game.
Will just add the slide here,
circled labeled bigger, save that, run the game.
And now we should see that the enemy doesn't
shoot until we get an range.
So there we go.
He starts shooting at the player.
If we go out, does he continue shooting?
No pay, doesn't. He stops.
We are in his fruits.
We are out. He stops shooting. Okay, great.
So that working, I
believe we still have a bit of time and
we will use that time to
arrange everything that we have in here.
So what is the problem?
You can see everything is working.
We don't have an errors.
But the code is very API.
You can see right here that we have
this update and this update does a lot,
and this is not something that we want an even.
And the player, you can see player controller.
We have a lot of lines of code and they are messy.
What happens in here?
What do we do?
Do we should do a move to
replay what is happening in here.
So we are going to,
of course fix this by creating or
using our new found knowledge of creating methods.
What we can do is,
for example, let's say where is the player walking?
So this is the movement we normalize, we give velocity.
So from the first line to this line,
we are moving the player.
Let's go ahead and select all of these.
I'm going to select these lines.
I'm going to right-click.
And you can see right here that we have
the quick actions and refactoring.
So when I click it,
you can see that it gives us
the option to extract a method.
I'm going to click on Extract Method
and now you have a new method.
What do you want to call it?
Let's call it the moving player.
Hit Enter. So now you
can see that all the lines are gone,
but they are stored in this little nifty,
very handy method down here.
So now we know that we start off by moving
the player and then we have all of this.
So what happens in here,
the mouse position and screen point,
we calculate the offset,
we get the angle,
we rotate the arm and then we check if we
should go from one direction to the next.
What else do we have in here?
Let's separate first of all, the shooting.
So we're going to separate the shooting.
And what did we call this the moving player.
And let's call this the layer shooting enter.
That's called this instead of moving player.
That's called this, the player
Moving and control RR, of course.
And how do we want to separate?
We can call this animating the player if we want.
And we can call
this one setting the player in the right direction.
And this one will be.
But for example, what should we call this?
So honestly, this is all based on your preference.
How do you want to make it?
So I'm going to call this the right-click.
And it's called the pointing.
Pointing in our case.
So obviously you can see right here what happened.
We have this outmost position and out scream point.
That means that we are returning these values.
Why are we returning them?
While? Because we use
the Screen point somewhere else in our code.
That's just said apply for now,
double-click on stream point and there you go.
You can see that we are using the mouse position
and the screen point in this if condition.
But we are calculating it
inside of the point in pointing.
I didn't change the name.
So you can see this is where we
calculated and this is where we need it.
So we create this out,
but I don't want this.
I feel It's a bit bad.
Oops. Too much.
Control Z, layer.
Moving enter.
We save that.
Now let's go ahead and use all of these we right-click.
And what shall we call this huge method?
We will call it, what do we do in here?
Pointing a gun at mouse, at Enter.
So you need the name of the method
to describe what's happening.
And finally, we can say that this is
animating the player Extract Method.
Animating V player hit Enter and whoa,
what a huge difference that
just made seriously look at the update,
how clean it is now,
we know that the player is moving.
He's pointing the gun at the mouse.
We are animating the player and the player is shooting.
And maybe you can even extract
an extra method inside of
the pointing guns to make it even cleaner.
Because for example, what we're doing in here is we are,
may be rotating or
pointing the player in the right direction.
You can even extract these two methods,
even make this more clean.
So her time as a challenge,
I'm going to give you the liberty of
extracting the methods in the NMI controller.
And I'll see you in
the next video where I will have already done this.
Maybe I'll show you whatever you want.
Do it yourself, have fun.
There's no pressure in here.
Just have this practice of doing things on your own.
Of course, I will show you the results later on,
but I hope you enjoy it
and I'll see you in the next video.
Never forget to commit your changes. My body.