What we're going to do now.
Instead of printing out message, we basically need to get a little bit of a distance, basically.
We will get closer to our player game object, and once the enemy somewhere is over here, or the player
just get closer to, uh, to this enemy game object somewhere over here, then this enemy need to stop
walking towards our player and it's need to play the attack animation clip.
But before we do, we also have a problem.
That enemy is not gonna look towards the direction our player just gonna go.
Let me show you.
So let's click on this play button in the top in order to play our game.
And let's have a look on this.
So as soon as we're gonna get a closer to this enemy, basically enemy just not looking in the direction,
uh, our player is going or player, uh, or this enemy is not facing this enemy too.
So if I get closer, firstly, we need to make the speed a little bit up.
So I'm going to select my anime to GameObject in the hierarchy.
Let's go over to Inspector Anime Script, anime two script.
And I'm going to make this bit something like uh one.
So as you can see, as soon as we get inside of this attack range radius of this anime two game object,
this anime just stops moving.
And if we jump over or top of it, as you can see, it's not looking in the direction player is going.
So we need to flip this anime to the right direction as well.
So we will get out of play mode.
To do that, we're going to approach the same way.
So what we can simply do, we can simply copy our, uh, flip function from our enemy one script, and
we can just paste it inside this enemy two script.
So let's do that instead of making a from scratch or doing it from scratch.
So we will open up our anime one script so you can select your anime one game object in your basically
prefabs folder.
So in my prefabs folder I'm going to select my anime -- game object and let's open up the anime one
script, go over to Inspector and then double click on this anime one script by basically open that
up quickly.
And over here we're going to copy the flip function.
So let's find it.
So we have the flip function as you can see.
So what we simply do we're gonna copy this by Ctrl C and we will go over to our anime two script.
And so let's open that up and we're gonna paste it inside this anime two script.
And let's do it somewhere over here by Ctrl V.
We're gonna paste it.
Now, as you can see we created and but the problem we have that we don't have the player position.
So we need to grab the player position reference.
But before I do I'm gonna arrange like this.
So we need our player position player position.
So we need a reference of transform of this player game object, so let's grab it.
To do so, I will move over to the top of my class as somewhere over here.
Let's make a serialized file.
I'm going to make it over here serialized field.
Serialized field.
And I'm going to click here.
This so serialized field private.
And we're looking for player position.
So we're going to type transform which is responsible for position.
And we can call this one player or player position.
Since over here we can see that this is player.
So that's why we're going to name it.
We're going to name as a player we we will leave it as player name.
As you can see now the error is gone by now.
So we also need to call this flip function in our update function.
So when we're going to call it once our player is in attack range is player in attack range.
So in that moment basically if the player inside of that circle basically inside of this animator circle,
then we will call the flip function.
So that's why.
So we're going to do it over here.
So let's call the flip function.
And let's make.
First bracket and close that up with semicolon.
Press down Ctrl S on your keyboard in order to save your script and then move over to unity.
So what we need to do now underneath our enemy to script attack checking title, we will, uh, get
uh, you can say we will get a transform field and by default it will be none.
And we just, uh, we just named that as player.
So we will select our player GameObject from hierarchy from the hierarchy.
And we're going to drag over to that field.
So we're going to have the player transform.
Or you can simply say the player position.
And then uh we we basically doing that basically uh flipping the enemy in the direction player is going.
So just wait a moment.
Once our compilation is done, we can go ahead and drag our player Clear transform.
So just wait a moment.
And now enemy two script is compiled by now.
So underneath our enemy two script, we can see in the bottom, uh, player and it says None transform.
So we will select our player game object in the hierarchy and let's drag it over to that field.
And now we can see we have the player transform basically player position.
And if we go ahead and then click on this play button and I'm gonna bump up the work speed a little
bit, I'm going to make it a, a b a a a.
You can say a, a max value, something like I'm gonna bump up with, uh, let's do it.
Let's go with, uh, two unit of speed.
Now we're going a little bit faster.
And if we move over to the right side, we not able to see, uh, that thing, basically, we have done
some mistake.
So if we move over to console tab, we if we get out of play mode, we can see we have a error message.
So if I scroll down to the top, I don't know where we're getting that error message.
Sorry.
We will go to.
So we have transform okay.
So what we need to do we need to.
Oh just go over to overrides and then apply all.
And now this is just going to be fine.
So if we go ahead and then click on this play button in the top, it's going to be fine by now.
So let's take a look on this.
Make sure you select your player object from your hierarchy tab.
And then drag it over to this M2 script in your player slot.
So you're going to have the reference.
So let's do it.
So we still are not able to flip this anime that direction.
So let's get out of play mode and we will move over to our script.
And let's check it over here.
As you can see we created a facing left right over here.
So what we're going to do we're going to call this flip function.
Instead of calling right over here we're going to call it in outside of it.
So let's call that function here top of our update function.
And now we're going to press down Ctrl S in order to save our script.
You what you can simply do you can just cut this line of code and you can paste it in update function
as well.
It's basically up to you.
So I'm going to press Ctrl s on my keyboard in order to save my project.
Sorry.
Save my script.
And let's head over to Unity editor one more time.
And now let's take a look on it.
It should be work.
Fine.
If it's not, then we need to do some settings.
So just wait a moment.
So wait a moment.
And now.
So let's have a look.
I guess this is fine.
Okay so let's go to unity.
And I'm going to make the work speed of this anime game object to something like one.
Let's go to overrides and then apply all.
Let's click on this play button in the top to play our game.
And let's and let's have a look on this.
It should work fine or other other way.
We need to do something.
So let's get closer.
We can see now as you can notice, this anime two game object just flipping in the direction player
is going.
So as you can notice.
So I'm going to get out of play mode.
So basically what you have to do you need to move your this uh flip function, flip flip function uh
in the top of your update function.
Don't put it inside this like is player in attack range if it's false.
Basically we just call it over here.
And we can notice that if this player attack in attack range is false, basically means a enemy is over
here.
So in that moment we just flipping but we don't want to do that.
So that's why.
So what we can simply do we can put this flip function as well inside this else condition.
In the else condition.
What we're doing we're saying player in attack range.
So we can put this flip function right over here instead of doing here.
But I'm gonna leave it in top of my update function and it's gonna work fine.
So basically we have done the same thing as we have done with enemy one script.
I hope you get the idea that how you can do it, and we can move this flip function as well inside this
else condition.
Else else if our enemy.
Sorry if our player game object inside of this enemy to attack range, then what we're doing, we simply
printing out this message saying player in attack range.
So we can just put the flip function here over here.
So the and the flip function just gonna flip the enemy in the direction player is going.
So we can put this line of code which is flip.
So let's.
basically press down Ctrl X for cut.
And we're going to paste it inside this else condition by Ctrl V.
And now if we press Ctrl S on our keyboard to save our script and then move over to unity, it's gonna
work fine.
Basically our enemy to game object just gonna flip in the direction player is going.
So let's have a look on this and let's have a look on this first.
So what I'm going to do now, uh, I'm going to click on this play button in the top in order to play
our game.
And let's take a look on this.
So if our if we are over here then you will see this enemy just gonna flip in the right direction.
So let's have a look.
So we will get closer to this enemy game object first.
And if I jump over top of it we can see enemy just flipping in the direction the player is going.
Uh, player is going.
So what we need to do, we need to do some, uh, changes.
Uh, but the problem is that Collider is, uh, not, uh, perfectly fit.
Uh, we can see that our anime to fit, uh, just overlaps with this ground, uh, overlap with this
white ground.
So we need to adjust our box collider 2D inside this anime to game object.
So let's get out of play mode, and let's select our animator GameObject in the hierarchy.
Then go over to Inspector Box Collider 2D component.
Then click on this Edit Collider.
And we're gonna edit the Collider as the size of our anime sprite, and make sure you just make it as
the size of your sprite, so it's not gonna overlap with other sprites.
So basically that is the thing.
And if you just change the thing, make sure you just go over to Inspector Overrides and then apply
all and that's gonna be fine.
And we will press down Ctrl s on our keyboard in order to save our project.
And we can just move over to console tab in the top left.
We can see clear, and if we want to clear all of the messages, then we can simply click on this clear
in order to clear out all of the messages.
And if we go ahead and then click on this play button, you will see the fit of this player game object
not gonna overlap with this white ground, so it's gonna be fine by now.
So let's have a look.
So make sure you just adjust your box Collider 2D as the size of your enemy sprite.
Now we can see it's not overlapping at all.
And once our player in attack range in attack range radius, uh, basically the circle collided with
this player game object.
We can notice this enemy game object just stops moving.
And we need to play some other animation clip as well.
So I'm going to get out of play mode.
So I hope you get the idea that how you can do it.
And I'm gonna press down Ctrl s or one more time in order to save my project, uh, on my keyboard.
So whatever I have done so far, it's gonna be saved.