We have a little bug.
Let me show you that to you.
So let's click on this play button in the top to play our game.
Let's jump over top of this anime game.
Object anime.
Just not gonna look towards in the direction player is going.
So we need to flip our enemy in the direction player is going.
Once our player gets in attack range of this enemy one.
Let me show you.
As you can see, it's playing the arrow animation clip.
So if I jump over on it, uh, as you can see, enemy is not looking to the right direction.
The player is just going.
So we need to flip this enemy in the right direction as well.
If the, uh, if the player is, uh, player is going to the right direction.
So let's get out of play mode.
So what we will do, we're just going to create a parameter to just keep track of which direction this
enemy is facing.
And then we're going to check we also need our player transform or basically player position.
And we also are just going to create our parameter in order to check which direction this enemy is facing.
So let's open up our enemy script by double clicking on it.
And let's go to top of our class and let's create those two variables.
So the first thing we need our player position.
So let's create another serialized field.
For position we need to type transform serialized field private.
And this will be transform simply position.
And we can call this one something like player position.
Or let's simply call it player.
And the second thing we need are our parameter.
In order to keep track of which direction our enemy is facing.
So let's create another private parameter.
It doesn't need to be public and that will be bool, which can be true or false.
And we can call this one something like facing left facing left.
And we're going to close that up with semicolon.
And we will move over to this stat function.
So we do know that stat function get called in the start of the game.
So in this start of the game we're gonna set facing left equal to true.
And let's close that up with semicolon.
Why we have done this.
If we move over to unity.
As you can see we're looking to the left direction.
So that's why facing left in the start of the game I just set equal to true.
As you can see.
Now we're going to save our script by Ctrl s.
Now let's move over to unity and let's have a look.
So we will be able to notice that uh notice that.
So underneath our enemy one script a another filter is gonna pop up and it will, it will say something
like player transform.
So we need to drag our player transform or player position into that field.
So we will have the player reference, uh player position reference.
And after that we can just flip our enemy one in the direction player is going.
So just wait a moment and once it's done, we can just drag the reference into that slot underneath
our enemy one script.
So now our script just gets compiled, as you can see, and we can see underneath our enemy.
One script underneath our attack title player and it says None Transform.
So let's select the player GameObject in the hierarchy and let's drag it over to that field.
Now we can see we have the player transform, or you can simply say we have player position and now
we can just flip that.
So let's open up our enemy one script one more time, and let's make a function in order to basically
flip our enemy.
So let's make it somewhere around here.
So for function we're going to type y.
And we can call this function to something like flip.
And let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check that this uh let's suppose if I select my player and then select this move
tool in the scene view.
And let's move it somewhere around here, as you can see in this In this time, the player position
in the x axis will be greater than position in the x axis, as you can see.
So enemy x position will be lesser than enemy player X position less than our player x position.
So what we can simply do right over here we we will check.
So first thing we will check.
So let's make if condition then first bracket and some curly brackets.
So what we want to check over here.
We basically want to check if our transform dot position dot x if it's lesser than player.
So we will going to player than position.
And we're going to access the x axis.
So what we're doing over here we basically checking if a transform dot position dot x basically means
enemy position in the x axis.
If it's lesser than player position in the x axis.
As you can see, we are going over to player and then accessing the position and then accessing the
x position, and we checking if our enemy position in the x axis is lesser than player position in the
x axis.
So if the player is somewhere around here, as you can see, it's going to be play enemy position in
the x axis will be lesser than player position in the x axis, as you can see.
At the same time, we will check if our enemy is facing to the left direction.
If that is the case, then we need to flip our enemy to the right direction.
So we also want to check.
So what we will do.
So we're going to put this an end condition.
And we will check if our facing left is true.
So you can if you just leave it like this it basically means true basically means that this enemy is
looking to the left direction.
And we do know that in the start of the game, we just said that facing left to true means we are looking
to the left direction.
So right now what we have to do, we need to flip this enemy to the right direction, and we need to
set that facing left parameter to true false.
Uh, because once we gonna flip this admin to the right direction?
Facing left parameter will be false base.
Or you can say this enemy one will be no longer able to look to look towards to the left direction.
So that is the thing.
And to flip this enemy.
So if I select my enemy one inch the hierarchy, then go over to Inspector Transform and then rotation
and in the y.
If we set it to something like -180, as you can see, as soon as we put this enemy just gets flipped
to the right direction.
Basically, we're doing the same thing as we have done inside this player script.
So we're gonna do the same thing.
So I'm going to set back to zero.
So we basically have to access the Euler angles.
So I'm going to select my player and let's move it over here.
So if our enemy position in the x axis is lesser than player position in the x axis, at the same time
our enemy is looking to the left direction.
So if that is the case.
So what we need to do, we need to flip our enemy.
And in order to flip our enemy, we need to use this transformer.
Euler angles which gonna have x x axis, y axis and the z axis.
So we will set equal to a new vector three which gonna have x axis y axis, sorry y axis and z axis.
And let's close that up with semicolon.
So if we put in the y -180 as you seen it's just gonna flips to the right direction.
So in the Y we're going to put -188.
And in the zero we don't sorry.
In the z axis we don't want to do anything at all.
So that's why we're gonna put zero f in the z axis.
And we will do the same thing in the x axis.
And after flipping this enemy to the right direction, we need to set that facing left parameter to
false.
Why?
Because this enemy will no longer able to look towards to the left direction.
So that's why after flipping, we're gonna, uh, we're gonna set that facing left parameter to false.
So after flipping we will set our facing left lab equal to false.
Let's set equal to false.
And now we need to call this flip function.
But when we should call this flip function.
Once this player game object gets in attack range of this enemy game object.
Basically we need to call it over here.
As you can see, if call info true basically means our player gets in attack range.
Now what we will do we will call the flip function.
Let's make first bracket.
Then close that up with semicolon and flip function.
Just gonna do this do these things.
Firstly it's going to check the distance uh distance in the x axis.
If it's lesser than player position in the x axis.
At the same time enemy is looking to the left direction.
Then we flipping our enemy to the right direction and after flipping we just setting setting this facing
left to false.
Because in that moment we're going to look towards to the right direction.
So that's why we set facing left parameter to false.
Once we flip our enemy to the right direction.
And now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So as soon as our player just gonna surpass our enemy one GameObject in the x axis, you will see this
enemy just flips to the right direction or in the direction the player is going.
Basically, it's gonna flip to the right direction.
And so just wait a little moment and once it's done, we can just go ahead and check.
Check that out.
And now I'm going to take my player and I'm gonna move it a little bit further away from this enemy
one GameObject.
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 just wait a moment and let's get in attack range and let's surpass this enemy.
So if we surpass this enemy, as you can see, as soon as we have done it, enemy is now looking to
the right direction.
But if we move to the move over to the left direction, this enemy is not flipping at all.
Back to our left direction.
So we also need to set this y to zero.
So if I select my enemy one inch the hierarchy, then go over to Inspector and set this y rotation to
zero.
As you can see, as soon as we're going to do it, it's going to flip to the left direction one more
time.
Basically this enemy.
So we also need to set back this y to zero.
And also we need to set back our facing lab parameter to true.
Why?
Because as you can see in this moment we will look towards to the left direction.
So that's why we need to flip our sorry.
So that's why we need to set facing lab parameter to true.
One more time.
Because in this condition we just want to look towards to the left direction.
So firstly we need to check the distance distance between them in the x axis as.
Then we can just do that.
So let's get out of play mode and let's move over to our script.
And over here we will check.
So we're going to use this else if condition.
So else if.
Let's make first bracket and some curly brackets.
So what.
We want to check over here.
We basically want to check if our current position of this enemy is greater than x in the x axis, is
greater than player position in the x axis, and basically means player is over here, then what?
We need to do this, we need to.
If the enemy is looking to the right direction, then firstly we need to set back this y to zero.
And if we set it as you can see it's going to look towards to the left direction.
And also we need to set back that facing left parameter to true.
Because in this situation, as you can see we're gonna look towards to the left direction.
So now we're going to do that thing.
So else if we will check if our transform dot position dot x basically means current position of that
enemy in the x axis a if it's a if it's greater than player position in the x axis.
So we will go to player.
Then we're going to access the position, then we're gonna access the X position.
If it's the case, means player is somewhere over here.
And sorry, enemy is somewhere over here.
And player is here.
So as you can see, player, an enemy in the x axis is going to be greater than player in the x axis.
And we're checking over here as you can see.
So current position of that enemy in the x axis.
If it's greater than player position in the x axis at the same time we will check.
So we're going to use this in condition and we will check if we just look if this enemy is looking to
the right direction basically means facing left parameter is false.
So what we will do we will check.
If so you need to put this exclamation mark basically means a.
And then you have to type the parameter name which is facing left.
It basically means that it's simply means that facing left parameter is false.
Or you can simply do these things as well.
Which is facing left equals equals to false basically means that our enemy one is looking to the right
direction, or we can simply do these things.
It also means the same thing.
We can just put exclamation mark right in front of this facing left.
It basically means that facing left parameter is false, means our enemy is looking to the right direction.
So right now what we have to do.
Firstly, we need to set back y to zero.
So this enemy one just going to flip towards to the left direction one more time.
And after it we need to set that facing left to true.
Because uh, if we flip our enemy to the left direction.
So this enemy is just going to look towards to the left direction as you can see.
So that's why.
So we will simply copy this line of code by Ctrl C and let's paste it over here by Ctrl V.
So we will set in the Y.
So let's set back to zero F.
And after flipping this enemy to the left direction we're going to set this facing left parameter to
true.
Because in this situation we're just going to look towards to the left direction.
And now let's press down Ctrl S in order to save our script.
And now let's move over to unity.
So as soon as we're going to surpass our enemy one inch the x axis, then enemy one just looks to the
right direction.
But once our player is left direction from this enemy GameObject, then this enemy is just going to
look towards to the left direction.
Or you can simply say in the direction player is going enemy just gonna follow that.
But or enemy just gonna flip that.
So let's click on this play button in the top in order to play our game.
And let's get inside attack range of this enemy one game object.
So once we're gonna over here somewhere around here, then enemy is just going to look to the right
direction.
So let's take a look on this.
So now if I surpass my player game object, as you can see, as soon as we doing this, enemy just looks
to the right direction.
But once we move to the left direction.
We can see this enemy.
It's now just a flip to the left direction as well.
And if I just do it one more time, we can see now the the enemy just facing the direction in the direction
player is going.
Once our a our player in attack range, and if we just get outside of the attack range then this enemy
just doing nothing.
It just standing and then playing the idle animation clip over again and again.
But once we get in attack range, it's just playing the attack arrow fire animation clip, and we can
see that enemy one is now just looking towards or just flipping in the direction player is going, because
we're doing inside this flip function, as you can see, and we only calling this flip function once
our player get in attack range, as you can notice right over here.
So I hope you get the idea that how you can do it and I'm gonna get out of play mode.