Welcome back my fellow game developers to one of the
more exciting and long, very long videos.
But trust me, it's very worth it.
In this video, we are going to inject more life and to our game by creating animations.
So now when we start our game,
we don't just stand there like a statue.
You can see that we are moving.
So our player is going up and down,
up and down, up and down.
And not only that,
whenever we start walking, look at that.
He starts moving his feet around.
How cool is that?
Seriously, now we don't have just the player standing fair lucky statue.
He actually has life, his breathing.
He's walking, he's looking around,
he's pointing his gun,
he's ready to start shooting.
He still needs a bit of tweaking,
but we'll handle that in the next video.
So what actually, on the end, which video?
Let's not waste any more time thinking and let's get to animating.
Oh, okay.
So currently when we start our game,
if we move around,
as you can see, we are very stale.
We don't have any animations in particular,
we don't see the player changing states
when he starts walking or when he's standing either.
So we're going to change that.
We are going to add animations to our player.
And we already have a couple of spreadsheets and here,
those that will determine our animation.
So you can see the idling, the walking.
We're going to put them side-by-side and play these sprites as we walk,
as we stand still,
as we do the torpedo movement.
So let's get started with animations.
First thing first, let's understand what animations are.
A couple of key components that we need.
First of all, we have the animator controller,
which allows you to arrange and maintain a set of animations clip.
We have the animator component which we add to any game object that we want to animate.
We have the actual animations,
which are several sprites are rendered one after the other.
We'll also see that we have a another type of animations that allow us to
move objects and then remember it and play it at certain times.
And finally we have the sprite renderer.
Now, why am I reminding you of the sprite renderer?
Because in just a bit,
you'll need to understand which sprite renderer would actually trying to
change in order to create the animations that we have for our DOM player.
Okay, enough talking.
Let's get started with animations.
So let's go into Unity.
And the first thing that we need to do is we need to get the windows for the animation.
So we're going to create the animations window.
Let's dock it around here.
And let's create a,
another window for the animator.
There you go.
So the animator as where we are going to set all of the animations.
And the animations as where we will actually create the animations.
Let's get started. First thing first,
we're going to choose our doom player.
And as you can see immediately in the animation window,
we have to begin animating, do player,
create an animator and animation clip.
Great, Let's do that.
We're going to hit Create.
And as you can see, we are currently in the time both invader file and the assets.
I'm going to create a new folder in here,
which is going to be the animations.
Or animation's.
There you go.
And in here create a folder called the player.
And we are going to name our clip.
Let's start by creating a player,
I think. Okay, great.
So now we have our new animation.
And if we look on the dome player,
you can see that we have a new component next to the rigid body and the capsule,
which is the animator.
So if we go into the scene,
nothing changes but inside of the animator,
just to refresh everything,
you can see that we now have these little rectangles in here.
And what are these?
Well, we have the any state,
the entry and exit.
Let's not worry about the any state and the exit.
Let's focus on the entry and the actual player idling.
What this means is as soon as we start our game,
we immediately transition into the player idling.
And you will see that every single time we have an animator,
we always have a default animation that we start off with.
And in this case, we have the player idling starting immediately from the entry.
And as you can see, it's connected by this line with an arrow.
This is called a transition.
And later on we will be making our own transition to go from one animation to the other.
Okay, let's continue on.
The next thing we want to do is we actually want to create the animation.
So in order to do that,
we need to get our idling sprites right here.
Those three, we want to add them to the player and start playing with them.
But there is a small detail in here that we need to take into consideration.
We are going to be changing the sprites of the player.
And the sprites that we are changing are actually on the body,
not on the immediate.
Doing player game object.
So through this animation,
will need to add a property in here.
And let me just explain this window right here.
This is the timeline for the animation.
So you can see from 030,
this is 2.5th and 2 one right here.
This is a whole second.
You have the play,
the go-to next keyframe, et cetera.
You have the Record button here and you can create
a new clip by clicking on this drop-down right here.
So let's add a property.
When we click it, you can see that we have access to
all the components that are present on the player or the doom player game object,
we can change the weapons are the Chicago,
the arm, the body, everything.
So I'm going to access the body.
And you can see that I can access the sprite render on it.
And if I scroll down,
I can actually access the sprite which is on the body right here.
So I'm going to click on the little plus sign here.
If I make this a bit bigger,
you can see that I can actually see
this little sprite and this is what we're going to change.
So if I hit Play,
you'll notice that nothing changes because this sprite is exactly the same as this one.
So let's change that.
I'm going to dock the animations and here just to have a little bit of space,
I'm going to delete the last one,
Keep this right here.
I'm going to select these three and add them to the timeline right here,
and I can get rid of the first one.
So now let me just make these, okay, But apart.
I'll dock the animation back here.
And now if I tried to play the animation, there you go.
You can see that our player starts to move.
How cool is that?
What is happening in here?
If you look closely,
let me just pause this for a second.
I'll just make the time between each a bit far away.
So now when we play,
you'll notice that we are going from one sprite to the other.
And you can make sure of that by looking right here on
the inspector of the body and see that we are going from either one,
either to idle, want idle two.
And this one right here,
which is the three,
is barely being played because as soon as we get to the end,
we then immediately start from the beginning.
So a good, let's say principal to have on creating animations is that all the time,
the first frame should be copied by pressing Control C and B,
the last frame also.
So now you'll notice how the animation looks much better.
So I'm going to play the animation again.
There you go.
You can see that now we are idling.
We can see 1, 2, 3,
1, 2, 3 playing one after the other.
So now we have the animation of the player idling.
And if we run our gain,
you can see that as soon as we start,
the player starts either link.
And even while we are moving,
we are still idling.
Even if we look the other way,
we are still idling.
And if we check the animator,
you can see that all the time we are idling.
I'll lock it right here so we can see it clearly and zoom out a bit.
So this is the player idling all the time while we are standing, still.
Great.
Now, the next step is to go ahead and create the walking animation.
And it's going to be your challenge.
Your challenge is to create another animation.
So get the walking sprites from the main sprite.
If you haven't already slice that up,
choose the doom player in the hierarchy and create a new clip like I showed you.
Add all the walking sprites and separate them.
Play around and make the walking look good.
And if you are interested in an extra challenge,
you can go ahead and create the torpedo animation.
I'm not sure if I'm going to keep using the torpedo animation.
Maybe it should be something different,
maybe v, rolling around animation.
I don't know with that non,
pause the video right now,
go try out the animation.
Don't worry, even if you're a beginner,
just try it out.
See a couple of steps.
How I created the idling.
Try it yourself, have fun, enjoy.
And that's it.
Pause the video right now and go do it each challenge.
Okay, so now we are going to create the second animation,
which is the walking.
So I'm going to click in here.
I'm going to create a new clip.
Go to the animations player, choose idling,
and instead of idling,
I'm going to set it as the walking.
So now player is walking.
Very good.
Okay, so now we are going to add a property in here.
We are going to access the body because remember
the sprites that we are changing are on the body.
Sprite renderer, the sprite because we are actually changing each single sprite.
And we are going to add,
let's move the animation in here.
Maybe this is not the best place to add an animation,
but we'll just have to do with what we have for now.
Go to the project,
which are our walking.
So I think these last four are the walking out.
Just add them in here.
I'll remove the last one and the first one.
That started the animation back down.
Let's choose all of these and I'm going to spread them out
with an interval of 10. Let's make it 10.
So 10, 10, 10.
And I'm going to copy the first one and set it as the last one.
And now when we tried to play the animation,
there we go, you can see the walking of the player.
And correct.
Now, how are we going to transition?
And if you actually click on the animator,
you'll see that we now have a new clip in here which is the player walking.
But how are we going to transition from widening talking?
This is the $1 million question.
Before I tell you how we do that,
let me just show you what we've created in this Animations folder inside of the player.
So we have the doom player,
he has an animator,
he has a controller.
And if I click on the controller, by the way,
whenever you click, for example, on the don't player,
you can see the rigid body where it is or the weapons on,
can get a reference.
We click on the controller.
This is the controller and these are
the animations of the player and the walking, the idling.
And you can see right here, we can do a bit of a modification for the cycle offset,
the loop time, the loop poles.
But for now, we're not going to delve deep into that.
For now, let's focus on the animator and how we are going to create transitions.
How are we going to decide when we transition?
This is also very important.
So we are going to create a transition in here by right-clicking on the player idling.
In here we have the make transition and we are going to set it on the player walking.
So let me zoom in a little bit.
So now you can see that we have this arrow going from player idling to play or walking.
And we can also create a transition from the player walking to the player idling.
So now we can go back and forth between these two animations.
Let's click on one of these arrows and look and see what we have here.
So we have something called the exit time.
If you hover over it,
you can see that this transition has a fixed exit time.
What does exit time mean?
It means if I go from the idling to the walking after a certain amount,
do I want to automatically go from the walking to the idling?
And our case, we don't want to do that.
We want to transition back whenever we actually stop walking.
So I'm going to turn off the Has Exit Time.
I'm going to do the same on the way down.
Then we are going to look for the transition duration.
And you can see actually a preview,
a small preview window in here.
So when I play,
I'm idling and then I start walking.
We have the transition duration.
So how do you want to finish the animation
of idling before we transition into walking and our case,
I think we don't need that.
So I'm going to set the transition duration to 0
and also to that in the transition of the walking.
So in both transitions,
both arrows going to and from the walking,
we are going to remove the exit time,
remove the transition later on when we do need them.
I'll make sure to tell you about it. Don't worry.
Now, if we scroll down,
we can see in here that we have something called the conditions.
And this is under which condition do we
transition from the player idling to the player walking?
And under which condition do we transition
from the player walking to the player either link?
So we are going to have to add a condition.
How do we add a condition in here in the transitions?
If we look again to the animator,
we can see that we have the layer step and we have something called the parameters.
And in here we can add certain parameters that we can then
access from our code and make the conditions.
So I'm going to click and tried to add one of the parameters.
You can see that we have float,
integer, Boolean, and a trigger.
Now, float and integer explain themselves.
A Boolean, as you know,
is a true or false and a trigger is just like what the word means, a trigger.
For now, let's focus on the Boolean.
So when I click on the Boolean,
you get the option to call it whatever you want.
So this will be the Boolean that transitions from either link to walking,
I'm going to call it, is.
Walking, hit Enter.
And now you can see that I can turn it on,
which means it becomes true,
or I can turn it off,
which means it becomes false.
So I'm going to click on the transition,
going from player ID link to walking.
And I'm going to click on the condition.
And as you can see, because we only have one parameter,
it immediately thinks that we need the walking or determines which is true.
So when is walking is true,
we transition from player idling too walking.
And if we are walking,
we want to transition to idling.
We want to have a condition where is walking is false.
So now when is walking is true,
we move from player Ivan into play or walking.
When player is walking and is walking becomes false.
We are back at idling.
Let's convert that into code and add behavior to it.
I'm going to open up my script.
And in here the first thing that we need to do is get a reference of the player animator.
So I'm going to create private animator, the player animator.
And he may door.
Now it's private.
How are we going to add?
We can't access it from the inspector in here.
If I click on the dome player on the script,
we don't have access to it.
Well, we can use something called the get component.
So in here I'm going to write player animator is going to be equal to get component.
And we are going to add the component type that we want to get.
So what's happening in here?
Let me hover over the debt component.
It doesn't have an explanation.
I'll tell you what the explanation as you can also go ahead
and find on Unity's documentation, it's very easy.
What it does is as soon as we start,
we get the component,
which is of type animator attached to
the game object that has the player controller as a script.
So effectively what we're doing is this script is using VI get component right here.
And getting the animator,
which is this one.
And now we have a reference to it.
So now we can access many things using this animator.
And you can also go through unit is
documentations and see all the different types of methods,
properties, and various things that we can do with this animator.
So what are we going to do in this animator?
We want to check for a condition.
We want to check if we're walking.
We want to set the Walking to true and then transition into the walking animation.
So I'm going to create an if condition in here and check if
the movement input is different than vector to 0.
So vector to the 0 is just like vector 3 right here.
Dot one, which means 111 vector two dot 0 means 00.
This means if the movement input is not 0,
that means we do have walking right here.
We have velocity, we have movement.
We are going to get the player animator.
We are going to use something called the set Boolean.
And in here you can see that it takes either an ID
or a different way of using the set booth.
If I click on here,
you can see that I can use a string name.
And this is what I'm going to do.
And you can see that we have a string reference.
And what did we say about shrink references?
Whenever we have one, we always go,
we copy it because if there is any mistake,
the IDE will not tell us what the problem is, what the error is.
The IDE, I mean the unity or the Visual Studio.
And in here because we are walking.
Oh, and I forgot.
What does this exclamation mark mean?
What does exclamation mark equals mean?
So if you see right here,
we've said that when mouse positions of x is less than this green point,
we could have said that if the mouse position that AX is equal,
whenever we want to compare two values,
we can set them as equal, equal.
That means is the most position x the same as the mouse point dot x.
But in here we decided to keep it as less than or equal.
But what if we want to say,
if it's not equal?
Well, then we use the exclamation mark equals.
This means that whenever movement input is different than the vector two dot 0,
we have the is walking set to true.
Else.
That means when the movement input is equal to 0,
we are going to duplicate this line.
And if we hold down Alt and use the arrow keys,
we can put it in here.
And we are going to set the is walking to false. We'll save that.
Let's go over this one more time because I forgot to tell you
about this and just kept on using it, which is very bad.
So I hope that wasn't too confusing.
Anyways, we create an if condition that we learned in a previous video.
We check if the movement speed is different than a vector 0,
say different than using an exclamation mark and equal.
So if movement speed is different than 0,
that means we are walking.
We are going to access the player animator who are going to use the set Bool method.
And we are going to access the is walking parameter and be setting it to true.
And remember, when we set it to true,
we have the transition in here that says if B is walking is true,
we go from idling too walking,
and we've done the same using an else.
Else means when movement input is equal to vector 2,
2, 0, that means we stopped walking is working as false.
We go from player walking to play or idling.
Let's see if that works.
So we start the game.
We can see right here that we have player idling.
If we start walking, there you go.
You can see that now we are walking and it's evident by the way,
the legs of our player are moving.
How cool is that?
So now we have animations.
We have transitions from one animation to the other.
We've created two animations.
Maybe you've created the third one on yourself.
I'll not do it right now,
although it later on for future Michel to handle that problem,
I feel lazy right now.
But there is one more thing that we need to take care
of if we go right here and to the dome player.
And let's go to Animations.
If we choose the idling,
for example, and we play it,
you'll notice that we have our gun and
our arm standing still right there not doing anything.
This is not good at all.
It makes the game look very, very bad.
So, and also when it comes to the walking,
so you can see right here that the arm is moving,
the player is moving,
but the arm and gone are very rigid.
In the next video,
we are going to be fixing that.
So before I go,
make sure to stage everything,
make sure to commit added and animations to
the idling and walking in your case also
the torpedo animation to the player, commit the changes.
And I'll see you in the next video.