These settings allow the sprite sheet to be animated, have the same detail as the normal image, and keep the max file size down by limiting the max texture sheet size to 1024 pixels.
Next, we need to update the sprite sheet so that it knows the boundaries for each of the animation frames in the sheet. To perform this, in Inspector, click on Sprite Editor.
This is designed to help us break the image based on the X and Y values we want to. For example, if each frame of the animation is 32
pixels wide on X and 32
pixels wide on Y, we can use the Sprite Editor to cut the image into smaller images based on that size.
This should result in the image being sliced into 32 x 32 boxes across the whole image. Take a look at the following image:
The major issue with this image is that it has the character facing in the wrong direction because the character will run from left to right in the game and not from right to left, as shown in the preceding image. We will fix this later by flipping the X axis of the character to -1, which will have the character face the correct orientation.
In the Assets
folder, you can see that CharacterSheet
went from a single image to now a parent object with 16 children, ranging from CharacterSheet_0
to CharacterSheet_15
.
In order to make this CharacterSheet
work in game, we need to create an animation for it. To perform this, select the CharacterSheet
parent object in the Assets
folder and left-click and drag it onto the Scene window.
You should see a popup asking you to save the anim file in the Assets/Character
folder. Name it Character_Run
and click on Save. Remember to drag it onto Scene and not on Hierarchy. If you drag it onto Hierarchy, the animation window will not appear.
After you click on Save, a couple of things will happen. The first and most noticeable change is that the GameObject named CharacterSheet_0
will be available in your game world. The second and third things are additions to files in your Assets/Character
folder.
The first file added will be the .anim
file called Character_Run
. This is the animation file that is in charge to cycle the animation of the character. The second is a controller file.
The controller file is used to set how the character will blend its animations. Double-click on the ControllerSheet_0
controller file to open it. You will see three nodes: the Any State node, the
Entry node, and the Character_Run node. The Any State node is the node that essentially starts the controller, meaning that no matter what goes on in the controller file, this is what will run as long as it exists.
In order to start your controller, right-click on the Any State node and select Make Transition. You'll see that your mouse will not be connected to a white line with an arrow in the middle.
Move the end of the white line and click on the Character_Run node. This connects the Any State node to the Character_Run node, making it the default node that will play if this controller is being used. This will be the case for our character.
To be sure, select File and then click on Save Project.
Next, we need to complete the controller by adding a jumping portion to it. To perform this, we need to take the same steps as the Character_Run node:
In the Assets/Character
folder, right-click on and select Import New Asset….
In the same ChapterThree_Character
folder, select the CharacterJumpSheet.png
file and import it.
32
.32
.You should now have a similar file as what CharacterSheet
shows and a parent object name (CharacterJumpSheet
) with two children named CharacterJumpSheet_0
and CharacterJumpSheet_1
.
In order to have an animation that we can use for this file and CharacterSheet
, perform the same as what we did earlier for CharacterSheet
: click on CharacterJumpSheet
and drag it onto the Scene window.
Name the anim file Character_Jump
.
One thing that needs mentioning is that by default, Unity will create another controller file for this animation. We do not need this.
Click on CharacterJumpSheet_0.controller
and then Delete on your keyboard. Alternatively, you can right-click on CharacterJumpSheet_0
and select Delete from the options.
We now need to connect the new Character_Jump.anim
file to the ControllerSheet_0.controller
file. To start with, open the ControllerSheet_0.controller
file. Now, you can again see the animation state controller in the Animator window.
Before adding the
Controller_Jump.anim
file, we first need to create a parameter for the controller file to use in order to blend between the two animations we have. In the top-left corner of the Animator window, there are two tabs: Layers and Parameters. Click on Parameters.
Under the selection for Parameters, there is a small + icon. Click on it and select Bool. Name this bool Jump
:
The parameter system in the Animator window is a utility that allows you to blend between specific animations during the period for which the game runs. In our case, we need to let our controller know when the player jumps. When this happens, we can change the bool value in the controller to true and the animations will blend from running to jumping.
Next, let's add the Character_Jump.anim
file to the controller. To perform this, select the anim file and then click and drag it onto the Animator window. You'll see that a new animation state box called Character_Jump will appear in the Animator window.
We now need to connect the Character_Run state to the Character_Jump state. As before, when we connected states, right-click on the Character_Run state box and select Make Transition. Then, with the white line attached to the mouse cursor, click on Character_Jump.
We also need to connect Character_Jump to the Character_Run state box. To perform this, follow the same steps of selecting the animation state box—this time Character_Jump—and select Make Transition. With the white line attached to the mouse cursor, click on Character_Run.
This will now make it such that both the animation states are connected to each other and allows you to blend between each of them:
For the controller to know when to switch between these two animation states, we have to tell the transition lines what parameter to look out for. Perform the following steps:
As we only have one parameter, this will automatically fill with Jump and set it to true.
Next, find the section named Has Exit Time and uncheck this selection box. If checked, this setting allows the animation to complete before it transitions into the new state. As we want the Jump animation to play as soon as the input has been received from the player, we will uncheck it.
We now need to do the same thing in reverse to connect Character_Jump to the Character_Run transition.