Let's create a enemy script in order to check the distance between our player and this enemy.
So to do that, let's head over to our project tab, then assets folder, and we will head over to our
script folder.
So it's a little bit lagging I really apologize.
So I'm gonna head over to my script folder.
Let's right click there then create a create.
And then you need to move over to scripting right over here.
And then click on this Monobehaviour script.
And you can name that script to something like anime one.
I already created a anime one script, as you can notice right over here.
So I'm going to select my anime one GameObject in the hierarchy, and let's select this anime one script
and let's drag it over to this Add component section in the Inspector.
Or we can simply click on this add component, and we can find the script by typing the name of the
script, which is in our case anime one.
So we can simply type that and it's gonna pop up.
So I hope you get the idea.
So I'm not gonna click on it in order to attach it.
Attach it twice since I already attached, as you can see.
So to get rid of this plus icon we will go over to overrides and then apply all.
And let's open up the enemy one script by double clicking on it.
So what we need first thing we need our player position so we can know that where player is.
And also we're going to create a circle around this enemy one game object.
And if that circle uh, if uh, if our player game object gets inside of that enemy circle, then this
enemy is just gonna shoot out the array on, on top of this player game object and this player game
object just gonna take damage.
So what we're gonna do, uh, firstly, we will create a circle, and also we need player position.
So instead of doing that, let's open up our enemy one script.
And we will create a circle from center position of this anime one game object to create a circle.
We do know that we need to use physics 2D function.
So let's move over to this update function, because we do know that update function runs every single
frame.
So every single frame we need to check the distance.
We need to check if we if our player game object get inside of that circle or collided with that circle
or not.
So to create circle we need to use this physics 2D physics 2D circle function.
And we already use it a couple of times previously.
So we're going to do the same thing basically.
So which is physics 2D dot overlap circle.
So overlap circle.
So as a first argument it's going to take the position from where we want to create the circle.
Second thing it's going to take the radius.
And the third argument it's going to take the layer mask.
So let's go to top of our class and let's assign those variable.
So I'm going to go to top of my Uh, top of my class.
Before we create our variable, let's give it a title.
So we need to type this header, make first bracket, then header in order to give title.
And let's say something like uh circle, uh attack.
Let's simply call it attack.
Something like that.
And so, so what we need, we're going to create our circle center position of that anime game object.
So we don't need any point reference.
Then we can pass a transform dot position which basically means center position.
So the second thing we need which is radius.
So let's make a serialized field private.
Uh, private.
Sorry, this will be private float.
And we can call attack range or attack radius.
Let's call it attack range.
Let's call it attack range instead of calling attack radius.
Attack range.
And by default I'm going to set equal to something like five unit.
And the third argument We need our layer mask.
So let's create a serialized field private layer mask.
And we can call this one damage layer target layer.
Or we can call this one attack layer as well.
Uh, so I'm going to call it uh, something like what is player what.
Since I want to damage my player.
So I'm going to say what is player sorry what is player.
And let's close that up with semicolon.
And now move over to this update function and let's remove these two slashes.
So the first thing it's going to take the position from where we want to create the circle.
So we will create the circle center position of this enemy game object uh enemy one game object.
So we will simply move over to this transform and then position.
So let's do it.
So we're going to pass transform dot position basically means center position of that enemy.
Second thing it's going to take the radius.
So we will pass our attack range.
So instead of calling attack range, we let's call it attack range radius.
It's going to be fine.
So let's call it attack range radius.
So I'm going to put it over here as well.
Range radius.
And now that's fine.
And the third argument it's going to take the layer mask.
So we will pass our what is layer and what is layer.
We're going to choose later our player layer.
And before we choose our player layer we need to assign our player as a player layer mask.
So this function is going to draw the circle.
Or it's going to create the circle around center position of that enemy game object.
And if that circle collide with this player then what we want to do we want to damage it.
So basically this enemy need to play that animation clip which is firing arrow.
For now let's print out a message.
So firstly we will store the result into another variable.
So this function right over here we do know it's going to give a true or false value, depending upon
if the circle is really colliding with this player game object or not.
For now, we're going to print out a message.
So we will we will store the information.
So let's create a collider 2D.
This is going to be Collider 2D.
Why?
Because this over here it's going to work like Collision or Collider.
So that's why we need to put it a in a container or in a variable with the same type.
And we can call this one call info or collision information.
And let's set equal to this.
So this call info can be true or false depending upon if the circle is colliding with this player game
object or not.
So what we're going to do now we're going to use this if condition.
And let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check if our call info is true.
If call info is equals equals to true something like this.
Or you can just leave it like this.
It's also basically means to, if that is the case, basically means our circle is just colliding with
this player game object.
Then what we want to do, we basically want to damage our player for now, or we're gonna shoot out.
Ray.
Sorry.
Shoot out our, uh, arrows from that enemy one game object to our to, uh, to our player game object
on top of our player game object in order to damage our player.
For now, we're going to print out a message.
So we're going to type this debug dot log.
And let's say something like let's make quotation.
So debug dot log sorry.
And let's say something like that damaging or shoot uh fire.
Let's say something like fire uh fire arrow, arrow.
And top of that I'm going to add the name of the game object that circle collided with.
So we're going to use this collision call info parameter in order to extract information like the name
of the game object the circle collided with or the circle collided with, or anything we can extract
with this call info parameter.
So let's head over to script.
So we're going to use this call info dot GameObject dot name.
And let's close that up with semicolon.
So call info dot GameObject basically means the GameObject the circle collided with.
So if the circle collided with this player game object.
So they're basically collision dot GameObject means this player GameObject.
And after that we just accessing the name of the game object, which in our case will be player.
So as you can see in Inspector.
So the message in the console tab should pop up Fire Arrow.
And the name of the game object is player.
So Fire Arrow on player.
Let's instead of saying Fire arrow we're gonna say fire arrow on a name of the game object, which is
player.
So the message should be pop up in our console tab says something like fire arrow on player.
And now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So just wait a little moment.
So just wait.
Basically doing the same thing as we have done with player attack once we created that attack function
as you've seen earlier.
So we just approaching the in the same way and we will use it multiple times.
Uh, we will uh gonna talk about it later.
For now, what we have to do, we need to assign our player as a player layer.
And also, it would be nice if we able to see our circle that we created from center position of this
enemy, one game object as you've seen in the code.
So we basically have to, uh, basically need to make, uh, that draw.
So we're going to use that on draw Ismost selected function, which is unity in build function.
And I really apologize for it's taking a little bit longer time because my system, you know, it's
a little bit slow.
So that's why it's taking a little bit longer time than as usual.
But if you have a fast system then in your case it's just gonna do within, uh, some seconds.
So just wait a moment till I can explain what we can do later.
So now it's a compile, and, uh, so just hold on a little bit.
Now it's compile.
But the problem is that we're not able to see our circle, so it would be nice if we able to see our
circle.
And we do know that in order to create the circle or to draw the circle, we need to use on draw drug
is most selected function.
So let's open up our anime one script one more time, and let's move over to in the bottom.
And we can do it anywhere.
You anywhere we like.
It doesn't need to be in order, but let's do it in the bottom somewhere over here.
So the function is on draw.
So on on draw is most selected function.
So this function right over here, it's gonna take uh basically uh whatever we want to draw, we can
do inside this function.
So to draw a circle we need to use this gizmos gizmos dot draw where sphere function and this function
as a first argument.
It's going to take the position from where you want to draw.
So we're going to draw the circle center position of that anime one game object.
So we will pass Transform.position.
It's basically means current position of that anime game object or center position of that anime, one
game object.
And the second thing it's going to take the radius.
So we will pass our attack range radius.
And let's close that up with semicolon.
And we also can choose a color before the color we would like to draw with.
So we can simply use this gizmo dot color.
And we can set the color equal to that you like.
So I'm going to draw with yellow color a yellow color.
So I'm going to say something like color dot yellow.
And let's close that up with semicolon.
And now this is fine.
And we can simply press down Ctrl s on our keyboard in order to save it.
And we can move over to unity.
And as soon as our ship just gonna compile, you will able to notice the circle will be drawn from that
enemy one game object center position with our attack range radius.
So just wait.
And we don't need to use, uh, basically we don't need to use assigning since we're just drawing from
center position.
So that's why.
Um, so basically we're not going to get any error.
As you can see, as soon as our skip gets compiled, we can see our circle.
And if we move over to anime one script in Inspector, we can see what is player is nothing.
So we will choose our player.
But we don't have any player layer, so we need to create that.
So we will select our player GameObject in the hierarchy first.
Then go over to Inspector.
In the top right we can see layer.
So let's click on this uh default.
Then click on Add Layer and name the layer whatever you want.
So this will be player.
So I'm going to call it player.
Let's hit enter.
And let's select our player game object one more time in the hierarchy.
And let's assign with the layer with which we recently created.
So I'm going to assign with player.
And I'm going to click on this no this game object only.
Basically I want this player only I don't want in this ground check or this attack point.
And you can see in the inspector layer is default in this attack point and this ground check.
But if we choose our player, we can see we assign it this player as a player layer in Inspector we
can notice.
And now we will select our enemy one game object and and in our attack what is player.
We will choose our player.
So the circle just only going to check with this player layer.
I'm going to drag it a little bit far away from this enemy one game object my player.
So let's select our player in the hierarchy and let's select the move tool in this in view.
And let's move it somewhere around here.
And now if we select our console tab and then click on this play button, you will see as soon as we're
gonna collide with this yellow circle or the circle that we created from this enemy, one game object,
the message just going to pop up in the console says, uh, fire on, uh, fire arrow on.
And the name of the game object, which is player.
So if we get collide with this circle.
As you can see, as soon as we just overlapped or collided with this circle.
We can see in our console tab a message just pop up says Fire Arrow on, and the name of the game object
and the name of the game object is player.
So that's why the message just getting pop up is saying fire arrow on player.
As you can see.
So now instead of printing out message, this player, this enemy, one need to play that enemy arrow
fire animation clip as you seen.
So basically what we have to do, we need a reference of this animator component of this enemy one game
object.
So if we select our enemy one in the hierarchy, then move over to Inspector.
We can see an animator component is attached to this enemy one game object.
And we do know that this component is responsible for playing our animation, switching animation and
all of this.
So let's get out of play mode and we need to grab the reference of this animator component.
So let's do it.
And let's clear all of the messages.
Instead of printing out message we need to set that parameter.
So if I select my anime one inch the hierarchy move over to my animator tab.
So we basically need to set this in range parameter to true.
But before we do we need a reference of this animator component and we will grab it in through our start
function.
And after that we will set this parameter which is in range.
And we do know that if we set this in range parameter to true, it's just going to play that animation
clip which is a fire arrow animation over again and again of this anime one game object.
So let's open up our anime one script by double clicking on it, and let's go to the top of our class
and let's grab the reference first.
And that doesn't need to be public, so we can make it private instead.
State, uh, private.
And the component we're looking for is animator.
So we're going to type this animator and let's call this one animator.
And let's close that up with semicolon.
And now let's move over to this start function.
So this start function get call in the start of the game.
So in this start of the game we're just gonna grab the reference.
So we will set our animator equal to this dot object dot get component.
And the component we're looking for is animator.
So inside these arrows we need to pass the component name which in our case animator.
Let's make first bracket and then close that with semicolon.
So this dot game object dot get component.
Basically it just reference referencing itself.
So the anime one script is attached to this anime one game object.
So this dot game object basically means this anime, one game object itself.
And then we just using get component and the component we're getting this animator as you can see.
So basically we're just taking that animator component of this anime one, this one, and we're storing
into this animator variable.
As you can see.
So now we have the reference of that animator component.
And we are ready to are ready to play that animation clip, which is uh, which, which, you know,
fire arrow.
So instead of printing out passage we need to set that in range parameter.
So firstly what we have to do, we need to move over to our animator component.
And we're going to use this set bool function as in order to set the full parameter.
So as you know that in range parameter was bool parameter.
So that's why we need to use this set bool function.
So firstly we just moving over to animator component and then we're using dot set bool function.
And this function as a first argument.
It's going to take in quotation the parameter name.
So in quotation uh the parameter name.
So the parameter name is and in range.
So make sure you, uh, spell it correct in range.
And the second thing, comma, it's going to take the value.
So we will set it to true basically means checked.
And we do not.
Once we set it it's going to play the, uh, fire arrow animation clip of that anime one game object
over again and again.
So we will set it to true.
And now let's close that up with semicolon.
And let's press down Ctrl S to save our escape.
And now let's move over to unity.
So firstly what we're doing we're just going over to this animator component of this anime one game
object.
And after that we just setting this in range parameter, as you can see in animator tab.
And we're setting it to true uh, means equal means check.
And we do not want we're gonna set it.
It's gonna play that animation clip which is the attack arrow fire arrow fire animation clip over again
and again.
So let's click on this play button and let's collide with this circle.
And as soon as we're gonna collide with that you will see this in range parameter which is setting it
to true.
Means take and and our enemy one.
Just going to play that animation clip over again and again.
So now if I collide with this circle you will see.
So just wait.
It's lagging a little bit.
So as you can see, as soon as we just colliding with this, uh, circle of this enemy one game object.
Enemy one just playing that, uh, arrow fire animation clip over again and again.
And you can take a look in animator tab as well.
But the problem is that if we don't collide with this, uh, this circle of this enemy, one that we
created, still this enemy one just playing that arrow fire animation clip over again and again.
So we need to stop playing that.
Instead, we need to play the idle animation clip of this enemy one.
So basically what we need to do, we basically need to set back this in range parameter to false, and
we can see as soon as we set it to false or uncheck.
Now this enemy one is playing the idle animation clip over again and again.
Why?
Because if we select the back transition and go over to Inspector, scroll down to.
Condition.
We can see that we have chosen that condition.
If in range is false, then it's going to play the idle animation clip over again and again.
So we need to set back this in range parameter to false.
Uh, false through our code.
Once we not colliding with this enemy circle that we created.
So let's get out of play mode and let's move over to our script.
And over here, as you can see, if call info true basically means the circle colliding with that player
game object.
Uh, else if our circle.
So else we're going to use.
Let's make a curly brackets and we will, uh, do it over here.
We basically want to check else our circle.
Uh, basically else our player is not colliding with this enemy circle.
Then what this enemy one needs to do is need to play that ideal animation clip.
Basically, we need to set back this in range parameter to false.
So let's go to a script.
So we will go to animator component first.
Then we're going to use this set bool function animator and set bool.
And as a first argument it's in quotation.
It's going to take the parameter name.
And the parameter name is in range.
And make sure you spelling it correct.
Second thing it's going to take the value that you want to set.
So we're going to set it to false false.
Or you can simply say uncheck.
And now we will close that off with semicolon.
And if we save our script and move over to unity, you will see as soon as we're going to leave this
Collider 2D that we created from this enemy, one GameObject basically gets outside of it, this enemy
one.
Now we're gonna play that idle animation clip instead of the arrow fire over again and again.
Why?
Because we checking that if call info is false basically means we're not colliding with this circle.
So now let's check it.
To check that, let's click on this play button in the top in order to play our game.
And let's take a look on this.
So as soon as we collide with this circle of this enemy one, this we setting this in range parameter
to true basically means we just playing that arrow fire animation clip over again and again.
So if I collide with this circle, as you can see, it's now playing over again and again.
But as soon as we leave this circle, we can see by now this enemy one is now playing the idle animation
clip instead of the arrow fire.
And we can take a look in animator tab as well, but make sure you select your enemy one GameObject
in the hierarchy first, then move over to animator tab and you can see.
So as soon as we collide with this circle, it's now playing Arrow Fire and once we leave it, it's
playing that ideal because once we leave it, we do know that we're just setting back this inrange parameter
to false via our scripting, as you seen.
So I'm going to get out of play mode.
So I hope you get the idea that how you can do it.
And let me explain one more time.
So if I move over to my script, as you can see over here.
So firstly we're creating that circle from our enemy center position.
And we're checking firstly if circle colliding basically means if call info is true means that circle
is colliding with that player game object, then we're just going over to animator component of our
enemy one game object, and then setting that in range parameter to true.
But once the circle is not colliding with that player, then what we doing else we doing, we just moving
over to animator component and we setting that in range parameter to false as you can see uncheck.
So I hope you get the idea that how you can do it.