We will create a anime script in order to attack on this player game object.
So let's do that.
So we're gonna move over to project tab assets and then script folder.
Let's right click there create and then Monobehaviour script and name it to something like anime.
I already created this anime script as you can see.
So now we need to attach.
So let's select our anime one game object in the hierarchy.
And we're going to drag this anime anime script in this add component section.
And now we can see we attach.
And let's open up this script by double clicking on it.
So what we need we need our player transform.
So basically you can simply call player position.
So we can just keep track of where player is.
And if the player gets an attack range of this enemy game object, then this enemy need to play the
attack animation as well as it need to fire on this anime game object, or it's just need to shoot on
this enemy game object.
So we're going to do that thing.
So let's open up our anime script and we what we need our player transform.
So let's go to the top of our class and we're going to declare, or we're gonna grab the reference of
our player position.
So let's create a public that will be transform, which is responsible for position.
And we can simply call it something like player or player position.
And also we're going to create a second parameter or we're going to call it something like attack range.
And it's just going to keep track of the basically we can define an range and you can simply call a
attack range.
And if this player game object gets inside of that attack range, then this enemy game object need to
fire on this player game object.
So let's go to script.
So we're going to define or declare another variable which will be float.
And we can call this one to something like attack range.
So we're going to call it attack range.
And by default I'm going to give a default value something like five f or five unit.
And let's close that up with some my colon.
And let's press control S in order to save our skin.
And now let's move over to unity.
So we're going to able to see that underneath our anime script.
We need to drag our player.
Transform means player position into that slot.
And we're also going to able to see the attack range.
So as soon as the anime script gets compiled, we can see underneath our anime script and it says Player
Transform.
So we need to select our player game object in the hierarchy and drag it right there.
Now we can see we have player transform means position and attack range.
I'm going to set in Inspector something like let's set it to seven unit.
But you can increase the increase this value as well.
It's basically totally up to you.
So now let's open up our anime script and let's move over to this update function and what we want to
do.
We're going to use this physics 2D.
So physics 2D dot overlap circle overlap circle overlap circle and make first bracket and then close
that output semicolon.
Basically what we want to do.
We're going to create a circle around this anime game object with this much of attack range radius.
As you can see, we set it seven.
So with seven unit we're going to create a circle around this anime game object.
And if this player game object collides with that circle or just gets in attack range of that radius
than this anime game object need to attack, or it just should play the attack animation or the fire
animation of this anime game object.
So let's go to script.
So this physics studio overlap circle.
We're going to use this function in order to create the circle.
So we need to use this physics overlap circle function in order to create the circle from this anime
game object center position.
So as a first argument it's going to take the position from where we want, where you just want to create
the circle.
So we're going to create our transform dot position.
And transform dot position basically means the current position of the center position of our enemy
game object.
Comma as a second argument, it's going to take the radius with how much radius you just want to create
the circle.
So we're going to create the circle around this attack range radius.
So we can simply call this one as well attack radius something like that.
But I'm going to leave it to attack range.
So I'm going to pass as a second argument attack range.
And the third argument it's going to take the layer mask or basically which game object you want to
target.
So we're going to target this player game object.
So we need to assign this player game object as a player layer.
So how you can just do that.
But before we do let's go to script.
And we need to go to top of our class.
And we need to assign or declare that variable as well.
So I'm going to make this one public and that will be layer mask.
And we can call this one to something like target layer or attack layer.
So I'm going to call it instead target layer.
And let's close that up with semicolon as a third parameter I'm going to pass the target layer and let's
press down Ctrl S in order to save our script.
And let's move to unity because we need to assign Assign those variable first one by one, and then
we can just create the circle right away.
But before we do we're going to assign this player game object as a player layer.
So how you can do that.
You need to select the game object first in the hierarchy.
And then you have to do it.
So we're going to select our player game object.
And if we go to Inspector top right we can see layer.
And it says default.
So we need to assign this player as a player layer in order to attack this player game object.
So that's why.
So let's go to click on this layer and then click on this add layer.
And we're going to create a another layer for this player game object.
So I'm going to call this one something like player.
And hit enter.
And make sure you select the game object one more time the player and then go to Inspector and Layer.
As you can see it says default.
So we're going to assign with player.
And we're going to do with this game object only.
So that's why we're going to click on this.
Yes or no.
Sorry.
We're not going to change the children.
Instead we're going to do no this game object only or we basically just doing with this player game
object only.
And if I select the child game object and then move to Inspector, we can see this.
It says Layer player, but I'm going to remove that instead I'm going to set it to default.
And now what we will do, we will select our enemy game object.
And we can see underneath our enemy script a target layer pops up and it says nothing.
And we do know we assign it this player as a player layer.
So I'm going to choose that player.
So let's click there.
And I'm going to choose this player so that the circle only going to check for this player game object.
Basically we want to damage our player.
So that's why we just only have chosen that player layer as you can see as a target layer.
So now let's move to a script and we can see we use this function physics 2D dot overlap circle.
In order to create the circle around center position.
With this much of radius and we have only chosen target layer.
And we do know as a target layer we have chosen player.
So the circle only going to check for that player game object.
So this can be true or false depending upon if the circle is really colliding with this player game
object or not.
If it's a, then this is gonna give us a true value.
Else it's gonna give us a false value.
So what we will do, we will store the result into a collider 2D.
Why?
Because this is work like as a collision.
So that's why we're gonna store the result into a collider 2D variable.
So let's store that.
So we're going to say we're going to set Collider 2D equal to call info.
Or you can simply say call information or collision information.
And we're gonna set equal to this.
So this call info can be true or false depending upon what this function gives us or return us.
If the circle is really colliding, then it's going to give us a true value.
So that's why this call info is just gonna be true right away, because we directly setting it, as
you can see.
And now what we're going to do, we're going to check in the next line.
We will check if so make first bracket and some curly brackets and what we want to check over here.
We want to check if call info is true.
So we're going to say if call info is equals, equals to true basically means the circle is really collided
with this player game object.
Uh so call info is going to be true means the circle is really colliding with this player game object.
Then what we need to do this player game object need to attack on this.
Or it should fire on this player game object.
So for now we're going to print out a message if it's perfectly working or not.
So we're going to check.
So we're going to type debug dot log in order to print out a message.
Then make first bracket and close that up with semicolon.
And we're going to print out a message something like.
So I'm going to print out attack attack on player and make sure you just close that off with semicolon.
And we're going to press down Ctrl S to save our script.
And let's move over to unity.
But we're not going to able to see the circle that we created around this enemy game object.
So what we need to do, we need to use another function in order to draw the circle around this A game
object.
So as soon as the anime script gets compiled, we're not able to see our circle.
So it would be nice if we able to see our circle with this much of radiance.
So let's go to script.
And for that we're going to use another function and it's called on draw gizmo selected function.
We don't know.
So first thing we uh, we want to draw.
And in order to draw we need to use this gizmos dot draw function draw wire sphere function.
So make sure you are doing as I did over here.
Else it's not going to work.
So make sure you just use this function on draw gizmo selected.
Uh and this function is unity callback function.
So you need to make as I did over here.
Else you're not going to able to draw the circle as a first argument.
This gizmo straw where sphere.
It's going to take the position from where you want to draw the circle.
So I'm going to pass transform dot position means the center position of our anime game object.
And the second thing it's going to take the radius.
So we're going to pass this attack range because with this attack range, we're just creating our radius
or that circle with this much radius as you can see.
So as a second argument I'm going to pass my attack range.
And let's close that up with semicolon.
And if you want to choose a different color then before you draw the circle you can choose the color
as well by using this gizmos dot color.
And you can set the color you would like to go with.
So I'm going to set equal to something like color dot red.
And you can choose any color that you like.
And let's save our script and head over to unity.
So uh, we will see as soon as our enemy script is gonna get compiled, we will be able to see that
circle will be drawn from this enemy GameObject center position with this much of a radius, which is
attack range, as you can see.
And as soon as gets our, uh, our enemy script gets compiled, we can see the circle has been drawn
from this enemy center position, and we can play around with this attack range.
Basically attack radius you can call as well.
So I'm going to set something like this much.
And I guess it would be nice.
So let's select the console top console tab in order to see the message.
And let's click on this play button in the top to play our game.
So as soon as this player game object is going to collide with this enemy circle or this circle, that
message just going to pop up right away says attack on player.
As you can see, the message just getting pop up every single time because we're doing inside that update
function.
So let's get out of play mode and what it's need to do this enemy game object, it's need to attack
on this player game object, or it's basically need to shoot on this player game object.
I hope you get the idea that how you can do it or how you can just do that.
Let's go to overheads and then apply all.
So the changes we made over here, it's going to be saved into this enemy one GameObject prefab as well.
And I'm going to press down Ctrl S in order to save my project.