Welcome back to the scavengers video series.
In the previous video we looked at setting up
the project and demonstrated the finished product.
In this video we're going to create animations
for the player and enemy units
from the provided sprite sheet
and also setup the player and enemy prefabs.
So let's create a new scene
and we're going to start by creating the player prefab,
along with the animations the go with him.
So we're going to choose Game Object - Create Empty
and we're going to label that Player.
In our folder Sprites we have a sprite sheet,
now this has already been sliced
in to multiple sprites.
For more information about sprite sheets and
sprites you can see the information linked below.
In this case we're going to grab
the first set of frames here, which are the
player idle animation,
and I did that by clicking the first frame and then just
shift-clicking the last frame that I wanted.
I'm going to take these and then drag them on
to the player prefab.
And then we're going to give this a name,
we're going to call this PlayerIdle,
so this is the animation that's going to play when
the player is not doing anything.
We're going to put this in our Animations folder.
You'll notice when we did that that two components
were added to the game object, a sprite renderer,
which is going to display the current sprite frame.
And an animator which is going to allow us to play animation.
So when we drag those frames on these two
components were added, it asked us to save our
animation which we saved in our Animations subfolder.
It also created what's called an animator controller,
which is going to allow us to manage what
animation is currently playing.
We're going to actually move that to our Animator Controllers folder
And we're going to add some more animations to the player.
Let's go back to sprites.
And in this case I'm going to select
our two PlayerChop animation frames,
drag them on to the player,
and we'll call these PlayerChop.
We're going to put this in our Animations folder.
Now notice when we do this this hasn't added
another animator controller, instead it's
added that animation to the existing
animator controller that's already on the player.
We're going to repeat the process with the PlayerHit animation.
And next we can check to make sure that that's working.
So we can see, there's our player idling,
now the animation is a little fast
so what we're going to do is we're going to open
the referenced animator controller by double clicking on it,
highlight the PlayerIdle state
we're just going to set the speed to 0.5,
which is going to be half of our normal speed.
Notice that our other two animations have
been added as states,
and later on in the series we're going to
work out how to control changing
between those states via script.
Let's check it now.
There we go.
With the animations done for the player
we're going to go ahead and apply
a tag to the player, of Player.
Now the Player tag is predefined in
all Unity projects, we've also added
a couple of additional tags, food, exit,
enemy and soda.
We've done that by going to Add Tag
and typing those in to these fields.
We're going to set the tag to Player
and we're also going to set the layer.
Here we've got our default layers,
and we've added an additional layer called Blocking Layer.
This is the layer on which all
collisions are going to be checked
and so we're going to go ahead and select that
for the player, and we added that the same way,
by choosing Add Layer and then typing that in.
So we'll go ahead and select blocking layer.
We're going to do one additional layer
related thing for the player, which is
we're going to set the sorting layer for the sprite renderer.
Now we've predefined some layers here,
we've got a Floor layer, which is going to be the background.
An Item layer which will be rendered in front of
the floor, which is going to be the food and the soda.
And then a Units layer for the player and the enemies.
We're going to assign the player to the units layer.
Next we're going to add two components
to the player, we're going to add a box collider 2D
and a rigidbody 2D.
The box collider 2D is going to allow us to
check collisions against the player.
And the rigidbody 2D is going to allow us
to move the player using the physics system.
We're going to set the rigidbody 2D to Is Kinematic
since we don't really want the player to be flying
and bouncing around, we're just going to have him moving
in neat grid-based spaces.
So we're going to control that using a script
and set this to Is Kinematic.
We're also going to adjust the size
of our box collider 2D
to make it a little bit smaller so that the
player won't accidentally collider with
things in adjacent spaces,
only when they're trying to move in to a space
occupied by another collider.
There are a few other components that we're going to add to the
player as we go, but for now that's a good starting point.
So we're going to take that and we're going to
drag it down to our Prefabs folder to save a prefab.
Now that we've created our prefab
we can temporarily delete the player from the hierarchy
and we're going to create our first enemy.
We're going to go ahead and choose Game Object - Create Empty
and we're going to name this Enemy1.
Let's start by adding our animations to Enemy1.
I'm going to go back to sprites
shift-click the frames of the EnemyIdle animation
and drag them on.
We're going to call this Enemy1Idle.
This is going to automatically add the components that
we need and then we're going to add the other
frames of our animation as well.
Add our Attack animation.
We can double check that that's working.
There he is, looking good.
And now let's add our box collider and rigidbody as well.
We're also going to set the rigidbody for
the enemy to Is Kinematic
and we're going to apply the Enemy tag
and assign the enemy to our blocking layer.
We're also going to set the sprite renderer sorting layer to Units.
With that done we can drag our enemy down
to the create a prefab of the first enemy.
And instead of deleting this one we're actually just going
to modify it to create the second one.
Rename it to Enemy2.
And we're going to drag in
our Enemy2Idle frames.
And our Enemy2Attack frames.
Now when we drag our animation
to our enemy we created a new animator controller.
We're going to move that to our Animator Controllers folder.
Next we're going to open up our Enemy1 animator controller
and we'll see inside that all of our
animations have been added here as states.
Now what we're actually going to do is we're going to
delete the Enemy2 states
from this, we're going to shift-click on each
of those and then right-click to delete them.
Now it's going to ask if we want to delete the selected assets
we're going to go ahead and choose delete.
And what we're going to do now is because
Enemy1 and Enemy2 are going to share the
same state machine, they have the same two states
of Idle and Attack.
What we're going to do is we're going to create what's called
an animator override controller,
which is going to allow us to use this same state machine
and the same script to control it
but just play different animations
in each of the states.
So we're going to go ahead and choose
Create - Animator Override Controller.
We'll call this Enemy2
And so the first thing that we need to specify
is what controller are we going to be overriding,
so we're going to choose the Enemy1 controller and drag it in.
And we'll see here, here are the two
original animations that we're going to play,
Enemy1Idle and Enemy1Attack,
and so we're going to override those with
Enemy2Attack
and Enemy2Idle.
In Enemy2 we're going to change
the controller of our animator to
now use our animator override controller.
Now if we play the scene.
There we go.
Finally let's drag Enemy2 down to our prefabs folder
to create a new prefab.
Next we can delete Enemy2 from the hierarchy
and we can save our scene.
We'll call it Main
and save it in our empty Scenes folder.
Now that we've got our basic prefabs created
for our Player and Enemy unit types
we're going to move on to creating the other
tile prefabs we're going to need to layout the game board.
In the next video we're going to create
our floor, wall, food, soda
and exit prefabs.
Subtitles by the Amara.org community