Let's open up our anime to script in order to move this anime to game object.
Uh, so so let's do that.
So we will open up our anime script and let's go to top of our class and let's declare some variable.
So we need the speed with how much speed we want to move.
So let's do that.
So firstly I'm going to give it a title.
So just wait.
It's lagging a little bit I really apologize for it.
So just wait a moment.
Uh let's now it's done.
So let's go to top of our class.
Let's give it a header.
So header basically we just firstly we need to make square brackets.
And then we have to type this header header and make this first bracket.
And in quotation we can give the title.
So let's say something like movement like that.
Let's create a serialized field uh private.
And this will be speed.
So this will be a float parameter.
And we can call this one something like.
Move spit.
Or let's simply call it work spit work spit.
Something like that.
And by default I'm going to give a default value something around uh, two f or two unit.
And now what we will do we will move over to this update function.
So this update function runs every single frame.
So every single frame we need to move our enemy.
So I want to move this enemy to GameObject to the left direction.
And in order to move that what we have to do we basically need to use this transform dot translate function
transform dot translate and then make this first bracket.
So transform basically what.
Basically this means we're just going over to this transform component of this enemy two GameObject.
And we're using this translate function in order to move.
And this translate function in brackets is going to take the direction in which direction you want to
move.
So since we want to move to the left direction so we can simply put a vector two dot left vector two
dot left vector two dot left basically means -one inch the x and zero in the y, because we don't want
to move this enemy to GameObject at all in the y direction.
So and we want to move this enemy to GameObject to the left direction in the x axis.
So that's why we put vector two dot vector two dot left.
It's basically means -one inch the x.
So it will be negative direction and one and zero in the y.
And what we will do, we will multiply this with our time delta time to make frame rate constant because
we are doing inside this update function.
So we need to make our speed constant every single frame.
So that's why we're going to multiply with time dot delta time.
And let's close that up with semicolon.
And we also want to multiply these things with our speed.
So we can just move a little bit faster, or with how much speed we just want to move our enemy to game
object.
So let's multiply this whole thing with our workspace.
And now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
And you will see as soon as are we gonna start our game, this enemy, we're just gonna start moving
to the left direction for this enemy to.
I'm just gonna create an another ground.
Simply, I'm gonna duplicate this ground GameObject from the hierarchy tab.
And that ground, I want to check with this enemy to game object.
So that's why uh, so we're going to do that.
Or we can just simply, uh, we can do it, uh, in this ground as well.
So I'm going to make the jump height of this player game object a little bit up.
I'm gonna bump up the jump height of this player game object in order to jump, jump over top of this
enemy game object so we can easily pass through it, and it's taking a longer time.
Now it's fine.
And as soon as our anime script just gets compiled, sorry.
If I move over to unity, we can see underneath our anime script, we can see the title, which is movement,
and we can see the work speed.
By default we set it to two.
And now what we're gonna do, uh, I'm going to make the jump height a little bit up, and I'm going
to bump up the jump height of this player game object.
So I will select simply select my player game object in a hierarchy.
Let's go over to Inspector then player skip and a jump height over here I'm going to make it something
like 25.
So now that's uh that's great.
And if I just go ahead and then click on this play button, you will see this, uh, anime two game
object.
Just gonna move as soon as the game just gonna start.
It will start moving to the left direction with work speed that we created, uh, the work speed.
And we can see indeed this animated GameObject just moving to the left side, uh, with our work speed.
But it's get fall down over here.
So once the enemy something over here at the end of our round, then we need to flip this enemy to the
right direction.
But before we do, we're gonna do a little bit one thing.
So we're gonna get out of play mode.
What we basically want to do, uh, we want to create a circle around this enemy to game object, uh,
enemy to game object.
And if this, uh, if this, uh, player game object, just get inside of this enemy game object attack
range or in that circle, then we're gonna attack on this.
Uh, we're gonna play the attack animation clip.
Basically, we're gonna do the same thing as we have done with enemy one game object.
Uh, I guess, but before we do, let's do the flip thing.
Uh, so let's do it.
So we will move over to our enemy two script.
Basically, we will open up our animated script by double clicking on it, and let's go to top of our
class and let's declare some variable.
So we're going to shoot out a ray.
Shoot out a ray from somewhere over here in front of this enemy two game object.
And if that ray don't collide with this ground game object.
So something over here, it's not gonna collide at all.
So in that moment, what we're gonna do, we're gonna flip this enemy to the right direction.
And in order to create the ray, we need to use a function called raycast.
So.
But before we do, let's create a variable in order to keep track of which direction this enemy two
is are facing.
So as we can notice, it's facing to the left direction.
So we're going to create a private bool parameter to just keep track of which direction this enemy is
looking.
So let's go to our script and let's make a private.
This will be bool which can be true or false.
And we can call this one facing left.
Sorry, we need to do it over here.
So let's call this one facing left.
And let's close that up with semicolon.
And now I'm going to move over to this start function.
So in the start of the game I'm going to set my facing left equal to true.
Equal to true.
And let's close that up with semicolon y.
We set it to true in this start of the game.
Because in the start of the game this enemy as you can see it's looking to the left direction.
So that's why in the start of the game I just set, I just said that facing left parameter to true as
you can notice.
And now what we have to do, we need to create a shoot out the ray.
And in order to shoot out the ray we need to use a function called physics 2D raycast.
So let's do that and we're going to do it update function.
Or we can make a function for it.
But let's do it over here.
Instead of creating a separate function for this.
And to to shoot out the ray we need to use this physics 2D dot raycast function and this raycast function
as a first argument.
It's going to take the position from where you want to shoot out the ray.
So we're going to create a point, basically an empty shell game object of this animator game object
somewhere around here.
And from that frame, we're gonna shoot out the ray.
Shoot out the ray.
So we need we need to create that point.
And the second thing it's going to take.
So if we just move over, it's going to take the direction.
So we're going to shoot out in the down direction.
And the third argument it's going to take the distance basically length of the ray.
So we need to specify that as well.
And the third and the fourth argument it's going to take the layer mask basically with which game object
we want we want to check with.
So we're going to check with this ground.
So as a layer mask we're going to choose this ground.
So we need those variable before we shoot out the ray.
So let's make this one comment by double forward slash.
And let's go to top of our class.
And let's uh let's assign those variable first.
So before we do let's give it a title.
So let's create this header.
And we can call this one something like ground detection.
So let's call ground detection.
The title we're giving is ground detection.
Uh detection I really apologize if the spelling is wrong I think it's, uh fine.
So now what we have to do.
We need to grab the reference.
So first thing we need our position.
So let's create a serialized field private.
And for position we need to try this transform which is responsible for position rotation and scale.
And we can call this one something like ground point or check point.
Uh so let's call shootout ground check point.
So let's call it ground ground check point check point.
Or we can simply call it check point as well.
So let's call it instead ground Check Point.
So I'm going to call this one Crown Shake.
Point.
And the second thing it's going to take the direction.
So we don't need to create that parameter.
Since we're going to use vector two.
Dot down basically means uh one in the uh sorry y in the y minus one.
So it's going to be negative and zero in the x.
So means basically means that it's just going to shoot out in the down direction.
So we don't need to specify that.
So we will uh so we can just move over to our third argument.
So we need to specify the length of the array or distance.
So let's create another serialized field private.
And this will be float.
And we can call this one array length.
Or let's call it distance instead.
So let's call this one distance.
And by default I'm going to set it to something like zero point 5FA little bit less number.
Or we can just go with 0.3 as well.
Something like that.
And the fourth argument we need the layer mask.
So let's create another serialized field private, private.
And this will be Layer Mask.
And we can call this something like target layer or let's call it what is Crown.
What is crown.
And now let's close that off with semicolon.
And let's press down Ctrl S to save our script.
And let's head over to unity.
So we need to assign those variable by one by one.
So the first thing just gonna pop up our ground checkpoint.
So we will create an empty game object or an empty game object inside this animator game object.
And we're going to make that one child, uh, since, uh, we want to shoot out the ray from that point.
So we're gonna create an empty child game object inside this anime game object.
So as you can see, as soon as our animator script just gets compiled, we can see ground detection
title, and we have the ground checkpoint as well as the distance and what is ground.
So we have this.
So let's create the point first.
So I'm going to select my enemy two GameObject in the hierarchy.
Let's right click on it.
Let's create an empty empty game object.
And we can call this GameObject something like Round Checkpoint as we name it the variable earlier.
So I'm going to call it Ground Checkpoint and make sure you go to Inspector.
Then click here.
Click here if you want to choose an icon.
So let's choose this yellow one, something like that.
And let's select this move tool from the scene view.
And let's move it in front of this you can simply say in front of this enemy two GameObject somewhere
over here.
So from this point we want to shoot out the ray.
So that's why we created.
So now we will select our enemy two GameObject in the hierarchy.
And let's select this ground checkpoint and drag it over to this ground checkpoint field.
Now we have the ground checkpoint or basically this position.
And we are ready to shoot out the ray.
And what is ground?
We're going to choose our ground since we want to check with this ground.
But before you choose your crown, make sure you select your crown.
Then go over to Inspector and Layer.
You can see it will be default, so just click on click on add layer, name the layer, whatever you
want.
So call it something like Crown and after that make sure you select that game object one more time.
So in our case Crown and go over to Inspector and Assign with the layer that you recently created.
So as you can notice, I assigned this ground as a ground layer.
We can notice in Inspector.
So I will head over to my animator game object.
And what is ground I'm gonna choose this ground says since the ray I want to check with this ground
game object.
And also we need to move over to this overrides in inspector.
Then apply all.
So the changes we made, it's going to be saved into this animator GameObject prefab as well.
And now we're gonna open up our animator script because we have all of the reference by now and we are
ready to create the ray.
rate.
So we're going to remove these two, uh slashes forward slashes.
So this physics 2D raycast as a first argument.
It's going to take the point from where we want to shoot out.
So we're going to pass our current checkpoint dot position because from this position we want to shoot
out that way.
And the second thing it's going to take the direction.
So we're going to put this vector to dot down.
Vector two dot down basically means that minus one and -one inch the y.
So it's going to be down and zero in the x.
So since we don't want to shoot out the ray in the x axis or horizontally.
So that's why we pass vector to dot down.
It basically means x into zero and -one inch the y.
So it's going to be in the down direction.
And this and the third argument it's going to take the distance or the length of the ray.
So we're going to pass our distance or basically length of the ray.
And the fourth argument it's going to take the layer mask uh basically which with which game object
we want to check with.
So I'm going to pass my what is ground.
And we do know that what is ground.
Uh, as a I have chosen this crown.
So we're going to check the rate.
Just going to check with that ground game object.
So this function physics 2D dot raycast, it's gonna shoot out the ray from this position in the down
direction with this much of length.
And it's going to check with our ground.
So now what we have to do.
So this right over here, it's gonna give us true or false value.
So what we will do we're going to store it into a raycast variable because this is a ray.
So that's why we need to put it a variable which should be recast.
So what we have to do we need to type this raycast hit variable type hit 2d variable.
And we have to name the parameter.
So we can simply call it something like hit info.
So let's call it hit info.
And let's set equal to this.
So we need to store into this raycast hit 2d.
Why.
Because this physics 2D raycast is a.
It's gonna work like a ray.
So that's why we need to store the result into this array cache.
Hit 2d parameter and we just call it call that parameter.
Hit info.
So this hit info can be true or false depending upon if the array is colliding with this crown or not.
If it's, then this right over here gonna be true.
So that's why this hit info just going to be true.
Because we directly setting it as you can notice.
Uh, and if the ray don't collide with this, uh, this crown, then it's gonna give us false value.
So this right over here, uh, gonna be false.
So that's why this hit info parameter just gonna be false.
So what we want to do now in the next, uh, in the next section, we want to check if the ray don't
collide with that crown.
So what are we gonna do?
We're gonna use this if condition.
So let's make first bracket and some curly brackets.
So what we want to check over here.
We basically want to check if hit info is equals.
Equals to false.
Basically means that this ray that we created from this point.
It's not colliding with this ground game object.
And we do know that if the enemy is somewhere over here.
So if I take my enemy GameObject somewhere here, as you can notice right over here, the rain not gonna
collide at all with this ground game object.
So in that moment, what we're doing, as you can see if I hit info is equals equals to false basically
means the ray is not colliding.
And over here it's not gonna collide at all.
So now what we need to do.
We basically need to flip this enemy to GameObject to the right direction.
But before we do, let's, uh, basically print out a message.
So I'm gonna move my enemy over here and to print out the message, we're going to type this, uh,
debug debug dot log.
And we can simply say something like flip enemy to the right direction.
So let's say something like flip enemy to the right, right direction.
Direction.
And let's close that up with semicolon.
So whenever the rain.
Not going to collide with this crown then this message just going to pop up because we're checking over
here.
If hit info equals equals to false basically means that Ray not colliding with that crown game object
at all.
Now I'm going to press Ctrl s to save my script.
And now let's move over to unity.
So as soon as, uh, we will be at the end of this crown, basically that enemy two game object, then
that message just going to pop up right away says, uh, flip enemy to the right direction.
But the problem is that we're not gonna able to see our Ray.
Uh, so as you can see, script just gets compiled, but the problem is that we're not able to see our
way, and we just want to see.
So it's gonna be perfect.
And we do know that in order to create it, we need to use a function calls on draw gizmo selected.
So let's use that in order to see the array or in order to draw the array.
So we're going to open up our animator script by double clicking on it.
And let's go to bottom somewhere over here you can make it anywhere.
It doesn't need to be in order.
So you can just make wherever you would like to go with.
So we have to use this on draw gizmo selected function.
And this function just going to draw what we want to draw.
And this function just unity in build function means a unity just going to call itself.
So we want to draw the array.
But before we want to draw the array we need to check if we assign it this ground check point or not
ground check point or not.
Basically let's use let's draw the array.
So firstly we will check if we assign it that uh basically that ground check point or not.
So what we will do, we will use this if condition.
So let's make first bracket and some curly brackets and what we want to check over here.
We basically want to check if our ground checkpoint.
If it's not equal to null.
What this basically means.
So you need to use this exclamation mark and then this equal sign.
So if ground checkpoint is not equal to null it basically means that this ground check field right over
here underneath our enemy two script is not empty.
Basically we just drag it this ground checkpoint to this ground checkpoint.
So as you can see, if ground checkpoint is not equal to null basically means that we assign it.
So now we're going to draw the draw the ray.
Else we're not going to draw the ray because we need a reference of this ground checkpoint.
After that we can draw the ray.
So that's why before we drawing we're checking if ground checkpoint is not equal to null.
Basically means we just drag it our ground check to ground checkpoint field.
Basically we have the reference of that position.
And now we are ready to draw the ray.
And in order to draw the ray we need to use this gizmos.
Sorry we need to use these gizmos dot draw ray function and this draw ray function as a first argument.
It's going to take the point from where you want to draw.
So we will pass our ground check point and we have to access the position.
So we're going to type this dot position.
And the second thing it's going to take the direction.
So we will pass this vector to dot down.
It's basically means uh minus one is zero in the XX0 in the x and minus one in the y.
So basically means in the down direction.
And we will multiply this, uh, direction with our length of the ray so we can see our ray.
So we will multiply our direction with the length of the ray which is in our case distance.
And now we're going to close that off with semicolon.
So I want to draw with yellow color.
So that's why before I draw I want to specify the color.
If you don't specify then it's just gonna draw the ray with white color.
But if you want to choose a different color so you can do that before you draw the ray.
So I want to choose an.
In order to choose the color, we need to use this gizmos dot color function.
Sorry, this gizmos dot color.
And you can set equal to color that you like.
So I'm going to set color dot yellow a yellow something like that.
And let's close that up with semicolon.
And we're going to press down control S in order to save our scape.
And if we move over to unity as soon as our enemy two script just gonna compiles.
You able to see the ray by now because we just using that on draw gizmo selected function in order to
draw the ray.
So just wait a little moment and you will see that.
And we can play around with the distance basically length of the ray.
And by now we can notice.
So if I select my animator GameObject in the hierarchy and select this move tool in the scene view and
move it somewhere over here, we can see the ray by now.
so I want to make the length a little bit.
I just want to.
Bump up the value.
So I will move over to this distance parameter underneath my animator script.
And let's make it something around one.
And this is fine.
So I'm gonna move it somewhere around here.
And if the enemy is somewhere around here, so they're not gonna collide with this game object.
So that message just going to pop up says flip enemy to the right direction.
So let's have a look on that.
So I'm going to select my console tab.
And let's click on this play button in the top in order to play our game.
And you will see as soon as we will be at the end of the edge of the of this ground, you will see that
message just going to pop up says flip enemy to the right direction.
So if I just jump over, you will see.
As you can see, as soon as we we are here, the message just gets pop up.
And still the message just getting pop up says flip enemy to the right direction.
Why?
Because the ray not colliding with this ground, as you can see.
So I'm gonna get out of play mode.
So instead of doing this, we need to flip this enemy to the right direction.
So if I select my enemy to GameObject in the hierarchy, then go over to Inspector Transform and Rotation
add rotation and in the y.
If I put 180 or -180, as you can see, as soon as we put it, it flips to the right direction.
So we basically need to change the Euler angles.
So we basically do gonna do the same thing as we have done with our enemy one game object.
So we're gonna approach the same way.
So I'm going to set back to zero.
So to do that instead of printing out message we need to flip.
So let's double click on this message in order to move over to that line of code.
So instead of printing out message we just want to flip.
And in order to flip.
But before we flip we need to check which direction we going.
So as you can notice, this enemy just facing to facing left.
Basically this enemy looking to the left direction.
So before we flip, we also need to check in which direction this enemy is facing.
So before we flip we will check.
So let's use this if condition.
Let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check if facing left is true equals equals to true.
Or you can just leave like this basically means that this enemy is looking to the left direction.
So what we need to do now so the enemy is gonna be over here.
So basically we need to move over to Euler angles.
And we need to set the y to -180.
And we do know once we're gonna set that it's going to flip to the right direction.
And after flipping to the right direction we need to set that facing left parameter to false.
Why?
Because if I set it to -180 or 180 sorry Of 180.
So in this situation, this enemy will no longer just gonna face to the left direction.
So that's why after flipping this enemy, we need to set the facing left parameter to false.
Because in that moment, this enemy would two game object just gonna look to the right direction.
So that's why.
So I'm going to set back to zero.
And let's move it somewhere around here.
And let's go to our script.
So to flip that we need to use this transform then dot Euler angles.
And this is going to have x x y and z angle.
So sorry z axis.
So we're going to set equal to new vector three which also going to have.
So which also gonna have x axis y axis and the z axis.
So in the y what we need to do we need to put a we basically need to put one ATF.
And we do know that as soon as we're gonna put one ATF minus one ATF, it's gonna flip to the right
direction.
And we don't want to mess around z axis.
So we're going to put zero f in Z.
And we're going to do the same thing in the x axis zero f.
And now after flipping this we need to set this facing left parameter to false.
Why?
Because as soon as we're gonna flip this enemy to the right direction, it's no longer able to look
to the left direction.
So that's why after flipping this, now, we're gonna set facing left equal to false.
False.
And let's close that up with semicolon.
And now I'm going to press down Ctrl s on my keyboard in order to save my script.
And then move over to unity.
You will see as soon as this enemy is just going to be at the end of this crown, this enemy just kind
of flips to the right direction.
So let's have a look on that.
So just wait a moment and I'm going to take this enemy over here and let's have a look.
So let's click on this play button in the top.
And in order to play our game.
And let's take a look on this.
So just wait a moment.
And if we jump over.
So it's gonna flip as soon as we can see.
As soon as enemy was over here, it just get flipped to the right direction and we can see if we just
select my enemy two GameObject in the hierarchy, move over to Inspector Transform and Rotation.
We can see in the Y is 180.
And now if the enemy is somewhere over here, we again need to set the Y to zero.
So what we basically have to do if the enemy is over here, let's suppose it's looking to the right
direction.
So enemy just gonna be over here.
So they're not gonna collide with this ground game object at all.
So what we need to do now, we need to set back y to zero.
And we do know that if we set back to zero, we can see this enemy just gonna look to the left direction
one more time.
So we need to set back to zero as well as after, uh, flipping this enemy to the left direction, we
need to set that facing left parameter to true.
Why?
Because as you can see, as soon as we're gonna flip to the left direction, we will look to the left
direction.
So that's why we need to set that parameter one more time to true.
So that's why we created that facing layer parameter.
In order to check which direction this enemy to game object is facing.
So basically that is the thing.
So let's do it.
So now what we're going to do, we're going to open up our enemy script once again.
And we will check else if our enemy if our enemy looking to the left or sorry if our enemy is looking
to the right direction.
So then what we're going to do, we we can simply copy this line of code by Ctrl C, and we can simply
paste it over here by Ctrl V.
So in the Y what we can do, we can simply put zero f.
And we do know that as soon as we're going to put zero F it's gonna flip to the left direction.
And after flipping to the left direction, we're gonna set this facing left parameter right over here
to true once again.
So we're gonna set it to true.
So firstly what we're checking we're checking if hit info is false.
Basically means the array is not colliding with this ground.
And in the else condition we're checking if we're looking to the right direction.
Then we're setting again to zero.
And after that we're setting facing left parameter to true as you can see.
So now we are done.
And let's press down Ctrl S to save our script I'm going to press Ctrl S to save my escape.
And now let's move over to unity.
So you will able to notice this armature schooner petrel around this ground.
Uh ground.
It's one one.
It's gonna gonna be at the end of the left direction.
It's gonna flip to the right direction.
Basically, this enemy is just gonna start moving to the right direction.
If the enemy to Two is something over here.
So let's check it.
And 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 this anime two is just gonna patrol around this ground, uh, depending upon which direction it's
going.
If it's at the end, then it's gonna flip itself.
So let's have a look.
So if I jump over it, we can see it's gonna flip.
Now it's flipped to the right direction.
And if we if the enemy is here, it's gonna flip one more time to the left direction, as you can see.
Indeed.
And if I pause my game by clicking on this pause button in the top, then go over to Inspector.
As you can see in the Y, we just set rotation back to zero.
And if if you want to see that private parameter which is bool parameter that we created which is facing
left, then you need to move over to inspector.
In the top right you can see three dots.
So just click there and then click on this debug mode.
And now we're going to able to notice our, uh, private parameters.
And we can see facing left as soon as we click on that debug mode underneath our enemy two script,
we can notice facing left is checked.
Basically means we just set back to true, uh, set back to true.
And we can check that out.
And you will see this parameter just going to be uncheck or check basically means true or false depending
upon which direction this enemy is going.
As you can see, as soon as enemy is over here then this facing left just getting true because it's
looking to the left direction.
And once the enemy, sorry, once the enemy is, uh, somewhere around here, uh, then the facing left
just gets false because this enemy just flips to the right direction, as you can notice underneath
your enemy to script.
And let's suppose you just want to go back in inspector mode then what you can do.
So firstly I'm going to get out of play mode by clicking on this play button in the top.
And to go back.
Just go over to Inspector one more time and then go over to top right.
Click on that three dots and then click on this normal in order to go back in normal mode.
And one thing I have to mention that if you don't use this hit info, basically, if you just put exclamation
mark right in front of this hit info parameter, it basically means the same thing.
Basically means hit info is false.
So you can simply just put this exclamation mark.
It basically means false or it's basically the same thing.
Hit info equals equals to false means the same thing.
So I hope you get the idea that how you can do it and make sure if you change anything inside this anime
script in the hierarchy, then go over to Inspector Overrides and then apply all.
And also you also press down Ctrl S on your keyboard in order to save your project.
So I hope you get the idea that how you can do it.