Creating Your First Unity Game – Rainy Knifes Part 1: Creating And Animating The Player Character

Table of Contents

Creating Your First Unity Game – Rainy Knifes Part 1: Creating And Animating The Player Character

Reading Time: 16 minutes
Level: Beginner
Version: 2020.3.1 LTS

Help Others Learn Game Development

Share on facebook
Share on twitter
Share on reddit
Share on linkedin

After going through our introduction to C# and Unity beginner series, in this tutorial series you will create your very first Unity game.

This is a beginner tutorial and no prior Unity or C# experience is required, however, it is required that you go through the C# beginner series that you can access by clicking on the link above.

I always make these first games as simple as they can be while implementing all the basic stuff we learned about Unity and C# programming.

The goal of the game is to avoid the Knifes that are falling from the sky as long as we can. When one of the Knifes hits the Player, the Player will die and the game will be restarted for us to play again.

Here is a preview of the game that we will create:


Download Assets And Complete Project For This Tutorial

In the first part of this tutorial series we will import the project files that you can download by clicking the Download assets button above, and we will animate our main character.

Let’s get started.

Importing Assets

The first thing that you will do is create a new 2D project from Unity Hub window, give it a name Rainy Knifes and save it in your desired destination.
 
 If you don’t remember how to create a new project using Unity Hub, you can refresh your memory by going back to “Creating A New Unity Project” title in the  Downloading Unity And Taking A Look At Its Interface lecture.
 

If you want to have the same tab arrangement as I do, you can take a look at the “Rearranging Tabs In The Editor” title in the same lecture.

After you clicked on the “Download assets” button above, unzipp the downloaded file and inside you will find two zip files:
– Asset files
– Finished project file
 
Asset files will contain all the files we need to follow the tutorial, and the finished project file will contained the finished project e.g. the project at the end of this tutorial.
 
I include the finished project file because you can download it, open it in Unity and inspect the project, compare your code to mine to see if you made a mistake and so on.
 
This is what I do every time at the start of the tutorial post so I am explaining what each folder means.
 
Because from now on, when I say import the assets in the project, you will know that these assets are located in the assets folder that you downloaded to follow the tutorial.
 
Now drag the unzipped folder called “Sprites” inside the Project tab of your new project:
 


Slicing Sprites With Sprite Editor

Inside the Sprites -> Player folder you will find two images, or better yet sprites. One is called Idle the other is called Walk.
 
You can see the preview of the sprite in the bottom right corner of the Inspector tab after you clicked on it:
 

Of course, where your Inspector tab is located depends on how you arranged your tabs in the editor, which is something we talked about in Downloading Unity And Taking A Look At Its Interface lecture.

The first thing that you will notice with both sprites is that they are not a single image, but multiple images put into one. This is called a sprite sheet.

A sprite sheet is used to save storage memory as well as graphics memory when the graphics processor renders the images.

Simply put, using a sprite sheet image is more performance efficient for your game than having all those images as separate files.

Of course, there will be situations where you will use separate images instead of a sprite sheet, and we will see those examples as well.

Now, the question is, how can we separate every image from the sprite sheet to be able to use it as individual images?

That is very easy to do by using the Sprite Editor.

First we need to select the desired sprite, and in the Inspector tab for the “Sprite Mode” we need to change it from “Single” to “Multiple”:

Img 2

When you do that, make sure that you press the “Apply” button at the bottom right of the Inspector tab:

Img 3

And from now on, any time we make a change to the sprite settings, be that for the Sprite Mode, or some other settings, we need to press the Apply button for those changes to happen.

Now that we changed the sprite mode, we can press the “Sprite Editor” button to open the sprite sheet in the Sprite Editor:

Img 4

In the Sprite Editor window you can click on the “Slice” button at the top left corner:

From there, in the new pop up window you can leave the “Type” to Automatic, and press the “Slice” button so that Unity will slice e.g. separate the images inside the sprite sheet:

After Unity slices the individual images in the sprite sheet, you can click on every image, see its width and height in the “Sprite” property window in the bottom right corner.

You can also change the name of every image in the sprite sheet which is something that we will be doing in more complex games:

Finally, to save the changes you made you need to click on the “Apply” button at the top right corner of the Sprite Editor:

You will need to do this every time you slice a sprite sheet, or if you make any changes within the sliced sprite sheet like changing one of the images names and so on.

Now repeat the same process for the other sprite sheet image called “Walk”.

And from now on, whenever we need to slice a sprite sheet, this is the process that we must go through.

When you finish, click on the small arrow on the right side of the sprite sheet indicating a drop down list, and when you do you will see this:

All individual images have been separated inside the sprite sheets, and now we can use every image as a single entity.

The Player Game Object And Sprite Renderer

Drag the first image from the “Idle” sprite sheet inside the Scene tab:

Img 9

When you do that, automatically a new game object is created in the Hierarchy tab with the same name as the individual sprite that you dragged in the Scene.

If you select that game object from the Hierarchy, you can inspect its properties in the Inspector tab:

Img 10

You will see that the game object has a Transform, and a Sprite Renderer component.

The transform component is the default component that comes with every game object you create.

It has properties like position, rotation and scale for moving, rotating and resizing the game object.

The Sprite Renderer component is a component that renders or draws sprites on the screen and this is how we present images in our game.

Renaming The Player Character

First things first, let us rename the Player character. In the Hierarchy tab, on the “Idle_0” Right Click -> Rename:

Img 11

You can also left click 2 times on “Idle_0” but not fast as when you are opening the folder, but have a small delay between the two clicks and you will be able to rename the game object.

Since this is our main character give this game object a name Player.

Changing The Reference Resolution In The Game Tab

Next what I always do in every game that I create is that I change the reference resolution in the Game tab.
 
You can think of the reference resolution as your working area of the game, this is what you will see with the Main Camera.
 
Of course, this applies for static games e.g. games where the camera is not moving, but in a game where the camera is moving you will see more of the game field as the camera moves.
 
Click on the “Game” tab and right below it you will see a drop down list that says “Free Aspect”, click on it and a list with available resolutions will appear:
 

The current available resolutions is depended on the platform for which you are creating the game. When you start a new project the default platform is always desktop.

You can verify that by click on the File -> Build Settings:

Img 13 - Fixed

From there the “Build Settings” window will appear and you will see that the current platform for which we are developing the game is PC, Mac & Linux Standalone:

Img 14

When we start creating games for mobile phones we will change the target platform for Android or iOS, but I just wanted to show from where the default resolutions in the Game tab come from.

You can also create custom resolutions by clicking on the ” + ” button at the bottom, that’s why you saw the “Full HD” resolutions in my window:

In the new window, under “Label”, give your custom resolution a name, e.g. Full HD, 4K and so on.

Leave the “Type” to “Fixed Resolution” and under width and height enter values for your custom resolution and press “Ok” to create it:

Now you will see your custom resolutions in the list as well. I am going to use full hd portrait resolution(1080×1920) from this point on.

Resizing The Player Character

If you take a look at the Player character in the Game tab you will see that he is enormous.

There are two ways how we can change the size of the Player character.

The first one is going back to the sprite sheet itself. Click on the “Idle” sprite sheet and in the Inspector tab under “Sprite Mode” you will see a property called “Pixels Per Unit”:

Img 17

What this property represents is how many pixels of the image will be drawn in one Unity unit.

Currently this number is set to 100. If you change the number to a higher value the Player will get smaller, and if you change the number to a lower value the Player will get bigger:

Don’t forget to hit the “Apply” button every time you make a change to the sprite settings.

You can play around with the numbers for “Pixels Per Unit” and see the outcome in the Game tab.

If you don’t see the Player character in the Game tab after setting the resolution to full hd portrait mode, just set the Player game object position in the transform property to 0, 0, 0 for x, y, and z:

Img 18

I am going to put “Pixels Per Unit” back to 100, and show you another way how we can change the size of the game object.

We already mentioned that the scale property inside transform component of the Player game object can be used to resize the Player.

You can simply play with the x and y scale value, and observe how the Player will change its size in the Game tab:

I am going to leave the scale for x and y at 0.4, and this is how we are going to resize the Player game object.

Attaching Components To Game Objects

In the lecture about math operations with variables we talked how you can attach components to game objects.

I am going to repeat that process here and from now on we will use this as a reference to know how components are attached on game objects.

Because we need to animate the Player character, the first thing we need to do is attach the “Animator” component on the Player object.

So click on the Player in the Hierarchy tab, and in the Inspector tab click on the “Add Component” button:

Img 19

In the search bar of the pop up window type “Animator”:

Img 20

Click on the animator and this will attach it to the Player game object:

Img 21

This is how we will attach every component on game objects from now on and you can use this guide as a reference to attach colliders, rigid bodies, scripts and anything in between whenever there is a need for that.

Creating The Animator Controller

The animator component is just the first step towards animating the Player character.

In order for the animator component to work, we need to create an animator controller who controls the animations we create for the Player.

The animator controller is also used in code to manipulate the animations and play the right animation at the right time.

Going back to the root asset folder in the Project tab:

Img 22

Right Click -> Create -> Folder:

Img 23

Give the folder a name “Animations”. Inside the Animations folder, Right Click -> Create -> Animator Controller:

Img 24

Rename the animator controller to Player Controller.

Click on the Player game object in the Hierarchy tab, and drag the Player Controller inside the “Controller” field property of the animator component:

This will attach the Player Controller to the animator component and now we are ready to animate the Player.

Animating The Player Character

To animate the Player, we need the “Animation” tab. The Animation tab is located under Window -> Animation -> Animation:

Img 25

If the tab is floating, you can dock it wherever you want inside the editor, but I usually make the Animation tab above the Project tab, because we will need to drag the sprites from the project tab in the Animation tab to create the animations.

To animate a game object, it needs to have the animator component attached along with the animator controller, which we already did.

After that, select the game object you want to animate in the Hierarchy tab, in our case Player, and when you do, in the Animator tab you will see a “Create” button:

Img 26

When you click the create button a new window will pop up, prompting you to give your animation a name and a location where to save the animation:

Img 27

Make sure that you save the animation in the animations folder that we created. This is just for organizational purposes which is always a good idea.

When we start working on more complex games we will have a separate folder for every game character that we will animate and we will store the animations for the specific character in their own folder.

In the “Save As” field give the animation a name “Idle” and click the “Save” button at the bottom right corner to save the animation clip.

Now when you click on the Player object in the Hierarchy, you will see how the Animation tab now has a timeline where you can drag images which act as frames for the animation:

Img 28 - 85%

You will also notice that it displays the name of the current animation in the top left corner of the Animation tab:

Note that this is only displayed for the game object who has the animator component and has animations, like the Player character does.

If you select another game object that doesn’t have the mentioned things you will see an empty animation tab.

You can try this out with selecting the Main Camera game object in the Hierarchy tab and see the outcome.

Now we can drag the two frames for the Idle animation inside the timeline. First select the Player object in the Hierarchy tab, and from the Sprites -> Player folder, click on the drop down list for the Idle sprite sheet and drag the two frames in the timeline:

By doing this we created the Idle animation for the Player game character.

Now to create the Walk animation, we need to create a new animation clip, but since we already have one animation created, we don’t see the “Create” button in the Animation tab.

So how can we create a new animation clip?

For that, we need to click on the button that displays the current animation name located at the top left corner of the Animation tab, and from there click on the “Create New Clip…” button:

Give the animation name “Walk” and save it in the same folder where we saved the Idle animation.

Now that you have two animations, you can switch between them by clicking on the same button and selecting your desired animation.

Repeat the same process we did for the Idle, but this time drag the frames of the Walk animation in the timeline.

Make sure that you have selected the correct animation before dragging the frames in the timeline:


Previewing The Animations And Changing The Speed Of The Animation

Now that we created the animations for the player character, we can preview them by using the play commands buttons in the Animation tab:

As you can see both Idle and Walk animations are previewing.

The Idle animation does look unnatural because there are only two frames for the animation, while the Walk animations looks a lot smoother.

There is a fix for this.

In the Animation tab, you can click on the three dots at the top right corner:

Img 30 - 80%

From there, you can go to “Set Sample Rate” and play with the numbers until you are satisfied with how the animation looks like:


Changing The Animation Speed In The Animator Tab

There is also another way how we can control the speed of the animations and that is from the Animator tab.
 
The Animator tab is located under Window -> Animation -> Animator. After you selected the tab you can dock it wherever you want in the editor:
 
Img 32

Same as with the Animation tab, in order for the Animator tab to display information, you first need to select the game object that has an Animator component attached to it and has animations.

So when you select the Player game object in the Hierarchy tab, this is what you will see in the Animator tab:

Img 33 - 80%

You will notice that the animations inside the Animator tab are displayed as nodes. Every node has the name of the animation it represents.

In the case of our Player character we have the Idle and Walk animation.

You will also see that the Idle animation has an orangy – yellowish color, denoting that this is the starting animation e.g. the animation that will be first played for the Player character when the game starts.

This is also denoted by the yellow arrow pointing from the “Entry” node towards the Idle node:

Img 34

You can always change the starting animation by Right Click -> Set As Layer Default State on any animation node you wish to be the starting animation:

To change the speed of the animation, you can click on the animation node you wish the change the speed for, and in the Inspector tab you will see a speed property:

Img 36

Number 1 indicates that the animation will play at its normal speed. If you set that number to 2, then the animation will play three times faster that the normal speed.

If you set it to 3, then the animation will play three times faster and so on. If you set it to 0.5 then the animation will play at half of the normal speed.

I am going to set the default animation to be Walk, and preview the normal speed of the animation, and the change the speed to see the outcome:

I am going to put the Idle animation back to be the starting animation, and set the speed for the Idle animation at 0.14, and the speed for the Walk animation at 0.6.

You can of course, experiment on your own and set these values as you wish, as long as you think the animation looks great at that speed, it will not affect the outcome of this tutorial.

Animation Transitions

Besides from being able to change the speed of the animations, we can also define animation transitions inside the Animator tab.
 
The animation transitions define how we can play the animations in our game.
 
We add parameters as conditions for those transitions and they allow us to play the Idle animation when the Player is not moving or play the Walk animation when the Player is moving.
 
For that, we need to create the parameters first. Inside the animator tab at the top right corner, you will see two additional tabs – “Layers” and “Parameters” tabs:
 

Click on the Parameters tab, and at the top right corner of the Parameters tab click on the ” + ” button:

Img 38

You will see a drop down list giving you a choice between float, int, bool, and a trigger as the parameter. We are going to select bool as the parameter:

Double click on the bool parameter now located below the Parameters tab, and give it a name Walk:

To create transitions between animations, we need to Right Click -> Make Transition on the animation that will transition from, and click on the animation where we want to transition to:


Creating Transition Parameters

What we did in the previous step will only create transitions for the animations, it will not specify how the transitions will happen. For that we will use the bool Walk parameter that we created.

To create conditions for the transitions, you click on the desired transition, for example the transition from Idle to Walk animation, and in the Inspector tab under “Conditions” click on the “+” button:

In the example above, we create a condition for the transition from Idle to Walk animation, and the condition is the Walk parameter.

To be more specific when the value of the Walk parameter is set to true, then we will transition from Idle to Walk animation.

Of course, if we have multiple parameters, we can choose which parameter is going to be used for transition condition, and we will see examples of this for more complex games.

Repeat the same process for the transition from Walk to Idle, but this time set the value of the Walk parameter to false from the drop down list:

Img 41 - 75%

Even though we still didn’t code the movement for the Player, we can test if these transitions work by checking and unchecking the check box for the Walk parameter inside the Animator tab:

Of course, when we start coding, we will change the parameter value from our code to animate the Player walking or idle state.

Where To Go From Here

This was the first part of this tutorial where you learned how to animate sprites using Unitys so called Mechanim animation system.

Next, we will move on to the second part of this tutorial titled Sprites, Colliders, And Rigidbodies where we will learn how to set up the rendering order of our game objects and how to apply physics to game objects.

Leave a Comment

With a FREE Awesome Tuts account

you can download assets and project files, bookmark your favorite posts and more

Already have an account? Sign in