Welcome back my fellow game developers to a brand new extremely long video.
I know you're sick of that.
But as always, it's extremely worth it because we learn so much.
First of all, we create an enemy.
We add a range to it.
We add a way to draw that range.
And we also run the game. We can shoot the enemy.
Obviously he doesn't die for now,
but we can get into the range.
You can see right here the player is in Chase range.
It's being sent every single time.
If we get out of this little bubble, there you go,
you see that the player is out of range.
So let's not waste any more time.
And let's get started with this long video.
Oh, so it's time for some enemies.
Let's get started.
The first thing that we're going to do is we're going
to right-click create an empty game object.
Make sure that its position is reset.
And the rotation and scale and et cetera.
And we'll just call this the enemy for now until we decide which enemy will want to use.
Next thing we'll need to do is we'll need to go into the sprites.
We're going to right-click create a folder in here for the enemies.
And inside of the enemies will go into the resources and
we are going to drag in the monster and creatures.
And let's just put it in here.
So we'll wait for everything to import. We'll open this up.
And as you can see, we have four types of enemies
that I've downloaded from open game arts.
Of course, as always,
you can go find your own enemies.
These are the ones that I've used and they will be in the link, in the resources.
And what should I choose?
I think I'm going to choose four now,
the, let's see which one.
I really haven't thought about this.
The enemies.
Let's just choose the skeleton and be very generic.
So as you can see, we have attack three death either et cetera, et cetera.
So I'm just going to go into the idle.
I'm going to make it multiple Sprite Editor apply.
And let's go ahead and slice it up,
not automatically, but by a grid size.
And if you were a bit confused by what I did last time,
I just check the length of the entire sprite.
So it's 600 and I divide it by how many skeletons we have.
So we have four or one single row,
so divided by four.
That means it's 150 by 150, as always.
For some reason, I'm surprised.
So we go into the splice, slice.
Splice, think that's movie splice, sure, slice.
There we go. Apply.
Now we have our little skeletons right here.
I'm going to drag it.
And the scene, whoa,
that's a huge skeleton, isn't it?
That's irony.
If you haven't picked it up.
Anyways, we'll make it 50. There we go.
We still can't see him because we need to have and apply.
Let's go ahead and create a layer in here which will be the enemy or enemies.
And I'm going set the bullets to render in front of
the enemy and also the player to render a plot of the enemy.
And let's go ahead and set the enemy right here.
So there you go.
It's still a very small enemy.
Let's save that. Let's make him a bit bigger.
So an idle 50, Let's make them 32.
Apply that.
Maybe even bigger.
Let's make him 16. So 16.
Yep. There we go.
Okay.
That's too big.
When D maybe 20.
Yeah, I think 20 is good enough.
That's changed this two points. See if that helps.
Yep, there we go. He looks a lot better,
still a bit grainy,
but no worries about that.
We have him for free.
Anyways.
Let's see, this is the body.
This would be the body.
And I'm going to child it to the enemy game object.
So now we have the enemy right here and the body.
Let's just set him to 00.
The position is relative,
of course to the enemy game objects or now we move around
using the enemy and you can see that he is rendered behind the player,
behind the gun and he should be also rendered behind the bullets. Go right.
Now that we have this,
next thing we need to do is obviously at
some behavior or a way to control the behavior of our enemy.
So we're going to go into the assets, the scripts,
we're going to right-click in here,
create a new folder maybe,
and let's call it the enemies.
Enter.
Maybe also create one in here for the player,
which will be the player controller.
And let's see, we'll also create one for the bullets and time now.
So create a new folder for the bullets.
And we are going to move these into the bullet.
Why?
Because we have the bullet,
the player bullet controller and the impact effect.
So and inside of the enemies will right-click,
create a new C Sharp script,
and we'll call it the
And EMI controller hit Enter.
Wait for this to compile. Okay, great.
And now in the enemy,
we can go ahead and add a component which is the enemy controller.
And we're going to open up the script.
Okay? So something you might have noticed that all the tabs are gone.
That's not because I closed them,
That's because we've changed the directory of them.
So we moved, for example,
the player controller from the folder scripts into the folder player scripts.
That's why we don't have it,
but don't worry, it's still,
if we double-click, we can easily open it up.
Let's continue on.
What do we need in here inside of the NMI controller?
While we need, first of all,
a variable that will keep in check the enemies speed.
And we will need a variable which will represent the enemies rigid body.
So I'm going to do just that.
These are very simple things.
No need for a lot of explanation.
This is a serialized field,
a float, and the enemy speed.
And another variable which will be a private rigid body 2D.
And I just remembered.
And EMI, rigid body.
The body doesn't need to be capital.
Save that.
And I just remember that we forgot obviously to
add a couple of things to the enemy, which is here.
We are going to add a component which will be a box collider.
And let's go ahead and make it.
So maybe without the shield,
usually it's better to keep the box colliders a bit smaller than the actual body.
This will make the player of your game wanted to become more accurate.
And we can also add the shadow, by the way,
under the enemy, which is a fairly good idea.
I'm going to add another component,
which will be the rigid body 2D.
Obviously we are going to make the gravity scale 0.
Save that, and we'll close both,
both of these down.
Move the script under the two components,
or we can just leave it there.
What's the speed of the enemy?
What's the speed of the player first, okay,
It's then, let's make the enemy speed 5.5 of that.
Save that. Okay, great.
So now we have the enemy.
We have a rigid body and a collider on him
so we can shoot him and interact with him physically.
Now what you want to do is we want to start moving the enemy towards the player,
but not all the time,
only when the player gets close enough.
So we need a variable for the range at
which the player will be at an order for the enemy to stop chasing him.
And we also need the direction in which the enemy will be moving.
So first thing first,
we're going to create a serialized field,
which will be a float with the player or the player chase range,
let's call it the player chains range.
So the range which the player should be to start chasing him.
And we'll create a private factor three.
And this will be the direction to move in the direction to move and save that.
Okay, great.
Let's go back in here.
And what's the chase range? And you know what?
Let's keep it for later on.
And I'll show you a very nifty way of how we can draw.
So how are we going to know what the chase ranges?
So I can, for example,
start making the change range five or ten, or 21.
But what does it mean?
What does it mean the chase range in here.
So do we have to every time calculate these little boxes
in here to determine how far or close declare is.
Well, of course not.
What we're going to do is we're going to draw
a circle in here that will only appear in the scene mode,
but not inside of the game mode.
How are we going to do this?
If we go back and here we have something called the draw gizmos.
And you can find that in the Unity documentation.
And the here we go,
you can see that we have something under the mono behavior,
which is onDraw gizmo select.
And you can see right here implement the onDraw gets selected to
draw gizmo object is selected.
Notice what we said, if the object is selected and you can see right here,
we have the gizmos test and how we can use it.
So we create a void onDraw gizmo.
And in here we can create the gizmo color so we select which color we want to be drawn.
And what do we want to draw,
which is the sphere.
So we can click on the troposphere and we can see it right here,
which is the public draws fear,
draws a solid sphere with center and a radius.
So let's implement this.
And use it to draw our own circle or sphere around the enemy and know the range of it.
And if we look closely at the method and here we can see that draws fear,
we need a center and we need a radius.
So what should the radius be?
Think about it while we create this method.
So in here, we're going to say on draw gizmos selected.
And we are first of all going to choose a color.
So it's going to be the gizmos dot color
and it's going to be equal to the color, let's say red.
Save that.
Next we are going to access the gizmos dot Draw wire, sphere.
So why are sphere look at that draws a wireframe sphere with a center and the radius.
So it's a bit different than the sphere,
but because we are playing in 2D,
we don't need to draw the entire sphere,
we just need the wire around it.
And let's see what should be the center.
The center obviously should be the transform dot positions or
transport is the transport of the object which has the enemy controller.
I know I've said it a million times,
but for those of you that are still confused,
I know that I was a bit confused when I when I started doing
this a couple of years ago, a lot of years ago.
And I remember, so where does this come from?
Well, it's the transform of the object that has the enemy controller on it,
which means it's this one, V enemy.
This is the transformer talking about back in here.
What is the radius?
Well, have you thought about it?
What should be the radius?
Obviously, it's the player chase range.
We are going to save that.
We are going to go back into Unity.
And well, whereas the circle you might be asking, well,
if you remember a couple of videos ago we turned off the gizmos.
Maybe you already saw it,
you didn't notice, but if we click it, there we go.
And why did we turn it off?
Because we had this camera and annoying icon of the camera.
What we can do is we can open the gizmos.
You can see that we can deselect a lot of things in here,
but one of the most important ones is we can make these icons are a bit smaller.
So there you go.
We no longer see the annoying camera go, correct?
So look at that big, huge circle.
Let's make it a bit smaller.
And you can see that it immediately changes.
I'm going to make it yea big.
I'm going to set the player there.
And the next thing that we need to do is we need to calculate the distance
between the player and the enemy and see if it's less than the range.
If it's less, we can start chasing the player.
If it's not less than,
we do not chase the player.
So what's the first step to do?
Well, we have the range,
we have the transform position of the enemy.
The next thing we need is a reference to the player.
How are we going to get the reference of the player?
Well, we first of all need to understand a method that I'm going to show you,
which is the object dot find objects of type.
And what this does,
the object t means that it depends on what the object you are looking for.
So when you find the object,
d becomes that type of object.
So the object, the first act of loaded object that matches the specific type.
So it returns the first loaded object of the type types or you can give it your type.
And if we look here,
we can see a couple of examples.
So let's say we want to find the Canvas renderer.
If you don't know what the Canvas renderer is,
don't worry, we will look into it later on.
But for now, look the Canvas renderer.
We create a canvas renderer pipe variable with the name Canvas,
and we use finite object of type and we get the canvas render.
So now we get a reference to it.
And because we only have one player,
we can use this.
If we had multiple players,
we couldn't have because,
well, obviously we find only the first act of one.
So look this method up,
find it, study it, understand it.
And then you will have to do this challenge.
You'll need to find the player object.
So create a variable of type transformed to represent the player we want to
chase and start use find objects of type 2, find the player.
The type should be the player controller.
Why? Because we need to find some specific type of the player.
So how do we identify the player?
He's just like all the other objects.
What makes him unique?
It's not just the name you can find through the name or the body or the on.
We need to use something specific.
And that's something specific is the player controller script.
That's why the type of the object you want to find is the player controller.
And finally use dot transform on the object you find and
assign it to the variable we've created, y dot transform.
You should know this because the type that we are going to create is the transform.
Why is it transform?
Well, because we want to get the distance.
How do we get the distance?
Well, obviously because it's a transform.
So pause the video right now.
Go do the challenge.
Okay, so first thing, first,
we are going to create a private transform and the player to chase.
Now inside of start,
we are going to say that the player to chase is equal to find objects of type.
And the type that we are looking for is the player controller.
And when we do find him because declared of two Chase as a transform,
we're just going to access the transform immediately from me.
So I hope you did the challenge.
I hope it wasn't too hard.
I know there was a bit of tricks in here,
even though it's the first time you use find object of type,
I'm sure that you were at least capable
of doing this part if you couldn't have figured out this,
don't worry about it was due time, will understand everything.
But now, the next step is to go ahead and calculate the distance.
So I'm going to create first of all, an if statement.
And then I'm going to write vector 3 dot.
And you can see that we have a method in here which is the distance and
it returns the distance between a and B.
So we're going to open this up.
What is the a?
It's going to be the transform dot position. And what is the B?
It's going to be the player to chase dot position.
There you go.
And what is the condition if the distance between the transformed position,
which is the enemy and the player to chase,
which is the player and it's less than V player chase range.
Then we are going to be moving our enemy.
But that is for another video.
For now, I'm just going to use the debug dot log.
And I'm going to say player is in Chase range.
And an else statement which we'll say that duplicate this line, move it down.
And it's going to say,
player out of Chase is out of chase range.
Save that back in our game.
Let's know what I just want to make something.
I'll make this public just for now,
just so we can see what happens.
So you'll notice that on the enemy right
now we have a player to chase, but there is nothing.
We are not going to add anything.
We're just going to run the game. There you go.
You can see that now the doom player, if I click on it,
there is the reference and now the skeleton,
our enemy has a reference to the player.
I can even shoot him.
So I'm going to go into the console.
What is this false?
Let's double-click.
Okay, So this one and player is out of chase range.
Let me set the game window next to
the scene when those so we can see clearly the circle in here.
So if I come close enough,
There we go, you can see that now player is in Chase range.
If I go out, player is out of range.
If I go and play as NJ strange,
there you go, everything is working perfectly fine.
So I hope you enjoy it.
And the next video will be actually moving our enemy,
not just printing something to the debug, to the log.
Let me remove this debug dot log.
Save that.
Let me make this private again because we don't need to see it.
I just wanted to demonstrate.
Don't forget to stage all your files.
And before I go,
What did I also want to tell you all.
I hope you enjoy it.
See you next time.