Hello guys.
Welcome to the video.
In this one I want to take care of the visuals of the player.
We already have some functionality.
He can move left and right.
And when I visually show that he is moving or standing, we're going to do that with the help of animations.
Animator is a very simple component, and it's not that difficult to understand, but it can be a bit
too much when you just starting.
So first of all, we would have to go to window find Animation Animator.
This is the animator window we're going to use.
Then we're going to go to Window Animation.
Animation.
And this window is responsible for controlling the animation itself.
Like here we uh control an animations one by one.
And here we have entire set up for the animator.
Now let's actually make one of the animators and apply them to the player.
We're going to go here and make a folder.
Animations.
Then inside of this folder we want to make.
Animator controller.
Animator controller and let's call it player.
Now let's select animator itself and drag this one.
On the animator like so.
Now it will have an animator controller component with controller assigned over here.
Very good, very very good.
Now if we select this animator and go to animator over here you can see we have animator opened.
And now we need to make some animations and use them.
We're going to make player idle first.
So we're going to click create over here on the assets.
Make sure you select animations folder.
So we have it all organized and I'm going to name it as Player idle.
And I'm going to use camel case.
So player idle let's save it.
And now we have default animation for the player.
But it is empty.
So we need to assign some sprites.
Over here we can go to graphics.
A main character.
Virtual guide and idol.
So luckily for us, creator of these assets prepared everything for us.
If you go to open Sprite Editor, you can see that these sprites are well cutted and we can use them
as they are.
But it may happen that in the next project you get the assets and they are not prepared like that.
So I'm going to show you how to prepare assets for animations real quick.
Let's click reset over here.
Now it has nothing there.
No setup nothing.
Just an image.
It is altogether and blurry.
First of all, to make it not blurry, you would have to go to compression.
Choose none and filter mode no filter.
Hit apply.
Now it is crispy and nice.
Next step would be what to change pixel per unit, just like we did in the beginning.
So let's do pixel per unit 16.
Very nice.
Now let's go and choose sprite mode multiple here.
This is very important.
And after that we can click Open Sprite Editor on the sprite editor.
We need to slice them so they can be used one by one.
I'm going to choose here grid by cell size.
And I know the size is 32 by 32.
So let's slice it hit apply.
And now we have them ready to use just like we had before.
Now let's select all of the sprites here.
Just like that.
And drag them to this animation.
If we go to scene now and click play, we can see our character is moving in the idle animation, although
it's kind of too fast.
We can go here, click Show Sample Rate and change sample rate to what we want.
I know for this animation it is better to have sample rate 15.
So I'm going to type 15 and press enter.
And now this is the idle animation.
Let's go ahead and place character somewhere here and go to play mode.
And here.
He feels alive now.
Very nice, very cool.
Although if we try to move.
This is what is happening.
There is no movement animation.
So first we have to create an animation and then we have to set up transition between animations so
we can transit from idle to move.
Very easy to do.
Let's just uh.
Select animator, go here, click create new clip and type player move.
Make sure you select correct folder.
So I'm going to go to Assets Animations and make it here.
Nice.
Now we need movement sprites and we have them over here.
Run.
So I'm going to select them all.
And just move them here.
Uh, let's see the animation.
Too fast.
Let's change it to 15.
Um, kinda.
Okay.
Maybe it could be a little bit faster.
So let's try 17.
Yeah.
Feels okay.
Very good.
So now if we go to animator, we can see we have two animations.
And somehow we need to establish connection between them.
And somehow we need to tell if we need to change from one animation to another.
There are a couple of ways to do that.
First, I'm going to show you one of the most common ways, and then I'm going to show you a nice way
to do it how I like to do it.
Okay.
So let's go to parameters.
Click plus.
And over here we're going to choose Boolean.
And I'm going to name it as Ease running.
So now we created a parameter for the animator.
Uh, let's click one of the animations.
Right button.
Make transition to player move.
So now we have two animations and we have transition between them.
And condition for the transition would be assigned.
Over here.
We're just going to click.
Plus is running true.
Then we need to make transition back.
Is running.
Pulse.
Now somehow we need to change this parameter over here in the animator through the script.
This is what we need to do.
We're going to go to player.
And if we want to work with animator, that is over here.
First of all, we need to get access to that animator, same as we did with the rigid body.
That can be achieved by just creating a variable type of animator.
So private animator and let's give it a name.
And I'm used to work with the name anim.
So that's what I'm going to use.
Anim now how do we get this?
If it's a private and it is not on the same GameObject, it is a child.
Well actually we can go back to script and type here.
Anim equals to get component in children animator.
Now let's save this, and let's just make sure we got the component by going to play mode.
And we're going to check debug over here.
Debug we have animator assigned.
And this is the animator.
Right.
Very good.
Now we have access to the animator and somehow we need to change.
This parameter is running.
Let's try to make a public boolean.
Is running.
And then in the animator over here I'm going to type enum set bool.
So we set in boolean of the animator.
Then we need to give a name of a boolean and be sure not to make a mistake here.
And I usually do lots of mistakes in the animator.
So I'm going to go here and copy this name over here.
Then go back to script open quotation marks and paste this in.
And then as a parameter we want to have we need a value.
We need to pass a value here.
First we say what do we want to change.
Then we want to say how we change it.
And we're going to give it a value of boolean we just created is running.
Let's save this.
Go back to unity.
Select normal and we have Isrunning boolean here.
Let's go to play mode.
And we can see he's standing.
Now I'm going to click this.
True.
It was changed here to true.
And he is moving.
I'm going to click it again and he stops.
Very cool.
Now somehow we need to make it change.
Not when we press in boolean in the inspector, but you know, automatically by itself.
Well, we can do it in some simple way.
First, I'm going to show you how you could do it with a if statement, but I'm going to say in advance
it's not very good way.
I'm just going to show you how not to do it.
And then I'm going to show you a nice way to do it.
Okay.
So we're going to type if.
RB velocity dot x not equals to zero means we are moving because velocity of the x is not zero, it
is -1 or -1 or bigger.
And that means RB is moving.
So we're going to do is running true.
Else in other case if it's a zero then is running false.
Let's just do it this way.
And then I'm going to show you a better way okay.
Let's save this.
Go back.
Hit the play button.
And see the animation we stand in.
I'm going to move and I have animation there.
Very nice.
So first of all, you can see transition is not immediate.
And even if I stop running.
There was a bit of a delay between animations that can be easily fixed.
If we go over here as the exit time and check.
And transition duration should be zero.
Same goes for another condition.
Transition duration zero has exit time and check.
Now when I move, it immediately changes to the movement animation.
Very cool.
Right?
Now this is working and this is nice, but it's really written.
Not that good.
We could do it better.
There is a way of assigning boolean.
Just by typing what it should be equals to.
If we want it to be true, uh, we're going to type is running which is a boolean equals to.
And here whatever you type will be condition for the boolean to be true.
As example we want to say is running equals to RB velocity dot x not equals to zero.
So in this way we say that this boolean will be true if rb velocity is not equals to zero.
If there is any other value here, it's going to be false.
Let's go ahead and see this.
To the very cool rate.
Now this works very well and it can be done a bit better with the help of a blend tree.
But I want to show you how to work with the blend tree in the next video.
So as of now, let's just clean this up a little bit and jump to next video.
Okay.
We're gonna select these two lines of code.
And this time I'm not going to make method manually.
Actually I want to click Alt Enter to call Quick Actions and then choose over here extract method.
So now we took those two lines of code and wrapped them up in the method sort of automatically.
Now let's give it a name and I'm going to name it as Handle animations.
Nice.
Let's keep it like that now it is a bit cleaner.
This method is responsible for animations.
This method is responsible for movement.
Also, we don't really need to make a boolean here because we can just pass value to this animator like
it is.
We can remove this boolean.
We can remove information from here and we can just go to this parenthesis and type here.
Our velocity dot x not equals to zero.
And even like that it will accept this information as a boolean true or false.
If this not equals to zero it will be true.
If it is equals to zero and it will be false.
So we sort of control in boolean that way and it will work as well.
We can go ahead and quickly check this.
But other than that we good with this video and we're going to jump to the next one.
And it's working very well.
Now let's go to the next video.
We're going to talk about Blend Trace, something very useful and very helpful especially for this project.
I'll see you in the next video.