Now we need to make transition this transition through our code so we can see if we select our player
GameObject in the hierarchy, we can see that an animator component is attached to this player GameObject.
So we do know that we need a reference of this animator component.
Then we can just set this parameter depending upon what player is really doing.
So what we will do we need a reference of this animator component.
So let's open up our player script and let's grab the reference first of it.
So we will go to top of our class and let's make it private.
It doesn't need to be public.
So we're going to make a private 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 something like that.
And let's go to this start function.
And let's grab the reference of that animator component.
So we're going to use this animator equal to this dot get component.
And the component we're looking for is animator.
So we're going to type this animator and let's make first packet then close that up with semicolon.
Uh so basically this dot get component Animator.
Basically we simply taking this animator component and we storing that animator component into this
animator variable we created up over here.
As you can see we storing that into this animator.
Now we have the reference and we are ready to uh, switch the animation.
So what we will do, we're going to use this movement parameter in order to switch up the animation.
And we do know that this movement right over here, it can be minus one zero or plus one means this
player can go left and right.
And if we press the space key, then, uh, we just gonna play the jump animation clip.
So we basically want to use this movement parameter in order to play our running animation clip.
So let's do it.
So we will move over to this inside this update function and let's do it over here in the bottom.
So let's make this if condition.
Let's make first bracket and some curly brackets.
So what we want to do over here what we want to check.
We want to check if our movement is greater than zero.
And we do know that if we start moving to the right direction then then movement is going to be something
like one.
So over here we're going to check if our movement is greater than zero.
If it's basically means that our player is moving.
But the problem is that if we start moving to the left direction and left direction, then this movement
right over here, it's going to be minus one.
So minus one is not greater than zero.
Zero.
If it's basically smaller than zero.
But we do know that we are moving to the left direction.
So we need to play the run animation clip.
So what we basically what we can do.
So we can simply uh use this function.
It's called math app.
So we have to use this math f dot f function absolute and then grab it into inside these brackets.
And now it's going to convert minus one into plus one even though we're moving to the left direction.
So if we hold down a key a key then we do know that that movement is going to be minus one.
And this math dot apps function is going to convert that minus one into a plus one.
Now uh, one just going to be greater than zero basically means we just moving to the left or right
direction.
Then what we need to do, we need to set this run parameter to zero and sorry run parameter to one.
And we do know that once we set this run parameter to one it's going to play the running animation clip.
So we have the reference of this animator component.
We do know.
So what we basically gonna do.
Firstly we will move over to animator component.
And we're going to use this dot set float function y this set float function.
Because if we move to unity we can see this run parameter is a float parameter.
So that's why we need to use this animator dot set float.
Basically we just moving over to this animator component.
First of this player game object as you can see.
And after that we just using that set float function in order to set the float of this run parameter.
So as a first argument it's going to take the parameter name in quotation.
So make sure you spell it correct which in my case run.
And the second thing comma it's going to take the value.
So we do know that we're going to set it to one or something like 0.2 which will be greater than 0.1.
And if we set it to something like one, and we do know that if we set it to one, it's going to play
the run animation clip.
And over here we're checking if movement is greater than zero.
F means we start moving to the left or right direction.
In that moment, we need to play the run animation clip.
Basically, we need to set this parameter to one f.
So second thing, it's going to take the value that you want to set.
So we're going to set it to one f.
So firstly we're checking if we're moving to the left or right direction.
If we start moving to the left or right direction then what we're doing firstly we're going to animator
component and we're using this set float function in order to set the float.
Uh, so we're setting this, uh, this one parameter to one, as you can see.
And now if I press down Ctrl s on my keyboard to save my script and then move over to unity, you will
see as soon as beginners start moving to the left or right direction, we will see that this player
GameObject just gonna play the run animation clip.
Why?
Because we're gonna start moving to the left or right direction.
And once we're doing, we're just going over to this animator component.
And by that we're just setting this one parameter to one.
And we do know that once we set, uh, one it's going to play the run animation clip.
So just wait.
So it should work fine.
So just wait a moment.
Once our compilation is done, we can see that.
So we're using that movement parameter in order to set this parameter, uh, that movement variable.
Or you can simply call it parameter as well.
It's basically up to you.
So it's going to take a few second to compile our script.
And once it's done we can just play our game in order to check it.
I really apologize because my system is too much slow, so that's why it's taking a little bit longer
time than as usual.
I really apologize for it.
So just wait.
If you do have a fast system, then in your case, it's gonna turn within.
Uh, something around 10s since my system is slow, so that's why it's taking a little bit longer time.
And now if we click on this play button in the top in order to start our game and try to move left and
right, we do know that that movement is going to be, uh, greater than zero sum with some unit.
It's going to be greater than zero than, uh, we just uh, setting this run parameter to one, as you
seen earlier.
So if I start moving to the left or right direction, as you can see, indeed the run animation is playing
over again and again.
But the problem is that we stuck at run animation clip.
So we also need to set back this run parameter to zero.
But when we shoot, once we stop moving, basically once that movement is going to be zero away.
And we don't know if that movement zero basically means this player just standing at one point, uh,
sorry, standing at one position.
So we need to set back this one parameter to zero.
So if we set manually this run parameter to zero in our animator tab, we can see right away it's now
playing the idle animation clip.
So we need to set it through our code.
So let's get out of play mode and let's move over to our script.
And over here we're going to check else if our movement.
So we will check else if our movement uh main if it is lesser than zero if that is lesser than zero
f or equal to zero f, we can check.
So if it lesser than or equal to zero f, if that is the case, basically means that we are not moving
at all.
And we do know that once we stop moving to the left or right direction, that movement, basically this
movement right over here, uh, it's gonna be zero f or it's it's gonna be zero.
Then over here, we checking else if our movement is lesser than or equal to zero, then what we need
to do, we need to set the that run parameter.
Basically this one back to zero.
So we're going to copy this line of code by Ctrl C.
And let's paste it over here by Ctrl V.
So we need to set this one parameter to zero.
So we're going to set back to zero.
So firstly what we're doing we basically going over to this animator component.
And then we're setting we're using that set float function in order to set this float parameter.
So in our case this run parameter as you can see.
And we're setting it to zero as you can see right over here.
So as a second parameter you need to pass the value and make sure you spell it correct or else it's
not going to work.
So make sure in quotation you put the uh put the spelling correct of that parameter name, else it's
not going to work.
So now if we save our script and then move over to unity and try to stop moving once, uh, once we
start moving and, um, and if we, if we just stop moving, then we will able to notice that run parameter.
We just setting it to zero.
Once we stop moving.
And we do know once we stop moving, that movement is gonna be zero and we're checking if it's zero
or less than zero.
Then we're just setting this run parameter back to zero.
And we do know once we set back to zero, it's going to play the idle animation clip instead of the
running one.
Because if we select from this run back to our idle, we can see in the inspector we have chosen that
condition if our run is less than 0.0.
So 0.1.
So let's click on this play button in the top in order to see.
And we do know that once we start stop moving we just setting run parameter to zero.
And we do know that zero f lesser than 0.1.
So the condition becoming true.
So that's why it's going to play the idle animation clip.
As you can see, as soon as we stop moving we can see we setting back this run parameter to zero eight
via our code, but once we start moving, we just setting this one parameter to one, as you can see.
And as soon as we stop moving, we just set it back this one parameter to zero so we can see the condition
it's becoming true.
So that's why the idle animation clip of this player game object is now playing over again and again.
But once we jump, we not playing our jump animation clip at all.
So let's make that one as well.
Basically we need to set this jump parameter to true this jump parameter right over here.
So let's get out of play mode.
And we do know we have the reference of this animator component.
So let's do it.
So let's go over to our script and let's move over to jump function where we're jumping.
So as you can see we're jumping over here.
So this jump function we calling once we press down space key basically we jumping.
So in that moment what we need to do we basically need to set this jump parameter to true.
So we will do that.
So over here we jumping.
So what we will do.
Firstly we will go to animator animator.
And now we're going to use this dot set bool function.
Set bool function.
And let's make this first packet why we using this set bool function.
Because if we move over to unity editor we can see this jump parameter is a bool parameter.
So that's why we need to use a instead of set float.
We need to use this set bool function.
And now this function in quotation as a first argument it's gonna take the parameter name.
So in our case jump and make sure you spell it correct as well.
That one which is jump in our case uh, our case.
And the second thing it's going to take the value which can be true or false.
So if we set it to true it's going to play the jump animation clip basically means we're checking that
uh checking that means we're setting it to true.
So we're going to set it to true since we're jumping over here.
So as a second argument we're going to pass through.
And let's close that up with semicolon.
And now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So whenever we're going to press down space key on our keyboard then we're calling this jump function.
And we're jumping.
And after jumping also we're just going over to animator component.
And then we're using the set bool function in order to set that bool parameter in our case that jump
parameter in quotation.
So now if we just go ahead and then click on this play button in the top in order to play our game and
try to press down space key on our keyboard, we will be able to notice that we're gonna, uh, we're
gonna play the jump animation clip.
So now if I press down space key on my keyboard, as you can see, as soon as we press down space Key,
it's played the jump animation clip.
But the problem is that we stuck in jump animation clip.
So we also need to set back this jump parameter to false.
So if we set it manually to false, as you can see it's now playing the idle animation clip, so when
we need to set it to false, once this player game object is just going to collide with this prompt,
then we're going to uh, then we will do it.
So let's get out of play mode.
And for that we're going to use Oncollisionenter 2D function.
So let's go over to script and let's make it somewhere over here and make sure you're just doing as
I did over here which is on collision enter 2D function.
So this function get called when we collide with something we do know.
And we can extract information what we collided with by going over to this collision parameter.
So what we're going to do over here.
So this function get called when we collide with something.
So let's suppose this player GameObject collide with this ground.
Then this function over here just going to get called.
And we can extract information about what we collided with with this collision parameter.
So what we will do if we select our ground in the hierarchy then go over to Inspector.
We can see tag and we we just is untagged.
So let's assign this ground as a ground tag.
So firstly select your ground in the hierarchy.
Go over to Inspector and Tag.
As you can see it says untagged.
So just click there and then click on this add tag.
Then click on this plus icon and in the tag whatever you want.
So I'm gonna create a ground tag and I'm gonna assign this ground as a ground tag.
So let's call it ground.
And let's click on the save button and make sure you select your game object one more time in the hierarchy.
And if we move over to Inspector we can see tag.
And it says untagged.
So we're gonna assign with ground as we created recently.
Now we assign it this ground as a ground tag.
As you can see in Inspector.
So now we assign it this ground as a ground tag.
And what we will do now and over here we're going to check.
So this function get called when we collide with something.
And we will, we will check before we collide.
So let's use this if condition.
Let's make first bracket and some curly brackets.
So what we want to check over here.
We basically want to check whatever we collided with.
If that game object has a tag called ground.
So we definitely gonna collide with this ground game object and we assign it this ground as a ground
tag.
As you can see in the inspector.
So means that we colliding.
So what we need to do, we need to set back this jump parameter to false.
And we do know that if we set it to false means uncheck it's gonna play the idle animation clip.
But before we do, let's print out a message if we're colliding with this ground or not.
So let's go over to our script.
Over here we will use this collision.
So collision dot GameObject dot tag.
If if the tag is equals equals to ground.
So in quotation we're going to pass this ground.
So what we're doing right over here we basically just going over to collision dot GameObject dot tag.
So collision dot GameObject over here basically means the GameObject we collided with.
So this player GameObject just collide with this ground.
So Collision's GameObject right over here basically means this ground.
And after that what we're doing, we're just accessing the tag of it of that GameObject.
And we're checking if the tag is equals equals to ground.
As you can see.
And we do know that we assign it this ground as a ground tag as we can notice in the inspector.
So we definitely gonna collide with this ground.
So now what we have to do.
We need to set back this parameter to false.
But before we set let's print out a message.
If we collide it with that crown or not for printing out, we're gonna type this debug.log.
And let's say something like we player let's say player collided.
Collided with a crown.
Something like that crown.
And let's close that open semicolon and we're gonna press down Ctrl s to save our skin.
And now let's move over to unity.
So as soon as this player GameObject just collide with this crown, you will see that message just gonna
pop up says a player collided with ground.
Because we just printing out that message as soon as we're gonna collide with this ground.
So let's select our console tab and let's click on this play button in the top.
But before you do, make sure you assign your ground as a ground tag by going over to Inspector.
As I, uh, as I explained earlier.
So now, as you can see, as soon as we collided with this ground, we can see the message in the console
tab just printing out says player collided with the ground instead of printing out message what we basically
want to do.
We basically need to set this parameter to false.
Basically uncheck and we can see if we set that to unchecked means false.
It's going to play the idle animation clip.
And if the player is moving left and right, then it's going to play the run animation clip instead
of the idle.
So let's get out of play mode and let's set it.
So instead of printing out message, we're going to set back that animator.
Uh, sorry, that jump parameter to false.
So let's double click on this message in order to move over to that line of code.
Instead of doing this, we're going to move over to our animator component.
And now we're going to use this dot set bool function.
And first thing it's going to take in quotation the bool name, which in our case jump and make sure
you spelling it correct.
And the second thing it's going to take the value.
So we're going to set it to false.
Basically means unchecked means that we're going to play the idle animation clip.
If the player just standing at one point and if the player is moving, then it's going to play the running
animation clip.
So 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 we're going to collide with this ground we will able to notice, uh, this player just
gonna play the idle or the run animation clip, depending upon what player is really doing.
If the player is moving left and right, then it's going to play the running animation clip.
So 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 we're not gonna stack in jump animation clip by now forever.
We're not gonna stack forever.
So if I pressed on space key on my keyboard, as you can see, it's playing idle animation clip as soon
as this player GameObject colliding with this ground.
Because once we colliding with this ground, firstly we're checking the tag if that game object has
a ground tag, and we definitely can see that we assign it this ground as a ground tag.
Then we're just setting that jump parameter to false.
And if we start moving, it's played right away the running animation clip instead of the idle, as
you can see.
But if we stop moving, then in this moment we're just playing this idle animation clip, since we have
done all of those things, as you can see right over here, back to run and then idle.
Idol, so I hope you get the idea.
So I'm going to get out of play mode.
So firstly, what are we doing?
Firstly we just grabbing the animator component reference.
So firstly you need to make sure that you grab the reference first.
And we're doing inside this start function.
If I, if I move over to uh top of my class, as you can see in the start function, I'm just taking
that animator component of that player game object, and we will be storing that into this animator.
And after grabbing the reference, we just are setting those run and then jump parameter depending upon
what player is really doing.
So we also need to trigger this parameter which is attack one, attack two and attack three.
And we're going to take a look on this next lecture.
We're going to take a look on on this uh sorry.
We're going to take a look on this in the next lecture.
So I hope you get the idea that how you can do it via scripting.
And I'm going to press control S on my keyboard in order to save my project.