Timing: Approximately 4 hours to complete.
For people who wish to get something up and running as quickly as possible. This is best suited to people with an intermediate understanding of Unity. You need to understand how to create objects, add components, duplicate objects, find and assign variables to components, etc.
If you are new to Unity you may prefer to look at the section:
Lets begin by creating a folder at the root of the project hierarchy. This will be referred to as the prototype folder. You can name 'prototype', or name it for your game (or whatever you desire).
Browse the Samples
In this approach we are aiming to get something worthy of being considered a 'prototype' up and running as quickly as possible. The easiest way to do this is to find the sample thats closest to your ideas and use that character as a basis.
Search for scenes in Unity using the Project search query: "Sample t:Scene". You may want to save this search query.
Play the samples and find the one thats most similar to your game.
Warning: This article assumes you use a 2D sprite based character. If you select a 3D character most of the steps are the same, but setting up your animator will require you to read some of the other sections of this documentation.
Once you have found a sample create a new prefab from the Character object in that sample (this is the GameObject with the Character script attached). Make sure this new prefab is in your prototype folder and rename it to 'PrototypeCharacter'.
Tip: Its best if you break the prefab connection between the sample and your newly created prefab.
Create Your Level
- Create a new scene and save it to your prototype folder and rename it to 'PrototypeScene'.
- Open this new scene.
- You may want to find the MainCamera and change it to Orthographic projection. You may also want to set the size to a larger number like 8.
- Drop the PrototypeCharacter in to the scene as a top level object and make sure its at position (0,0,0).
- Find the prefab called 'Template_GeometryQuickTest' and drag it in to your scene.
- Create an Empty GameObject in your scene and rename it to Input.
- Add a StandardInput Component to this GameObject.
Your scene hierarchy look like this:
Press Play and you should be able to move your character around the game world using the default keys (arrows keys, Z and X).
Tip: Notice how the camera and input automatically pick up the Character.
Update Your Characters Look
As a first step for 2D sprites you may want to find your characters SpriteRenderer and update the base Sprite (shown in scene view but replaced once animations start).
It's a good idea to use a neutral pose like the first frame of your IDLE animation here.
Find your characters animator, it will typically be on a child of the Character GameObject called CharacterSprite or CharacterModel. With the project window visible click the Controller field of the Animator:
This will select the controller in the project view.
Duplicate this controller and rename it to PrototypeController. Move this Duplicate to your prototype folder.
Update your animator to use this new controller and Apply the changes to the prefab.
Tip: The steps here are done to ensure you don't alter the samples but they are not strictly necessary. You could instead just update the existing animation controller.
For Simple Animation Bridges
With your character sprite/model selected open the Animator window. You should see the animation state machine for your character.
You may want to read about Animation Bridges before proceeding.
If your animator looks something like this (with no transitions between states):
your animator is of the simple type.
To update an animation simple select the Animation State in the Animation state machine and assign a new animation:
To add new animations (i.e. for different states) create a new state and make sure the name is an exact uppercase match for the state name found in the script file AnimationState.
Tip: If your new movements (see below) send extra states you will get a warning that they are missing in the editor window.
For Complex Animation Bridges
If your animation bridge is more complex type then it is not covered in this section. Find the matching article in the Animation category of this documentation.
If you press Play you will probably find that your characters colliders no longer match the characters shape. It might be standing well above the ground, well below the ground, or maybe it can walk inside a wall.
To adjust your colliders click your character object and press the large Reset Colliders button. Click the Reset button next to the option 'Use basic sprite detection'.
Tip: If your sprite is readable you will also have the option to 'Use smart sprite detection' you are welcome to give this option a go. It tries to use transparency to better match your characters shape but wont always be accurate.
This should give you colliders that basically match your sprite. You may want to make the following adjustments:
- Click Edit Feet and then add 1 or 2 more feet colliders using the Add Collider button. This will help with slopes and also stop your character from falling through very thin pieces of ground.
- Click Edit Sides and use the (+) icon in the scene view to add more side colliders. Remember if the character runs in to a wall but it doesn't hit a side collider they will walk straight through. So you need enough side colliders to ensure the gap between them is smaller than the smallest platform height. For most geometry 1 or 2 is enough.
- Click Edit Head and then add 1 or 2 more head colliders using the Add Collider button. This will stop your character from jumping through very thin pieces of ground.
Fine tune the Colliders
Press Play and see where your character is standing. If he is standing too high, go back to the scene view click Edit Feet and use the handles in the side view to raise the Feet Colliders. If it is standing too low, do the opposite.
Repeat until it looks right.
Use the same process for the side colliders and head colliders. Pressing Play and adjusting the colliders to ensure the character meets the wall or roof in the right place.
Once done save your scene and Apply the changes to your character prefab.
Note: This process can be a little tedious but only needs to be done once for each character.
Hit Boxes and Hurt Boxes
The HurtBox is the part of the character that registers damage. Like the colliders, after you update the sprite the hurt box may no longer be aligned with the character.
The HurtBox is a standard Unity Collider2D. Drag the HurtBox GameObject until the collider is in the right place. Then use the collider settings to adjust the size of the collider.
The HurtBox is also generally used for collecting items although you can use a different collider with a CharacterReference attached if you prefer (for example if your character can only take damage when hit in the head, but can collect items using any part of their body).
The HitBox component causes damage to others (enemies) and is typically associated with melee attacks. Like the HurtBoxes these are standard colliders that can be moved in to position to match your character sprite.
There are many, many options for customising movements. Here we discuss this using a few examples, but its recommended that you read the article:
Example: Increasing the speed of the Alien Character
A simple example, we want our alien character to move faster.
- Select the GameObject that contains the movements for the character. For the Alien its the child object called Movements (for some other characters it may be the Character GameObject itself.
- Find the Ground Movement.
- Increase the value of the speed variable:
Example: Adding crouch to a Character
Crouch is a movement that overrides the default ground movement when the user presses down. To override a movement the movement must be placed higher in the component list.
- Add a new GroundMovement to the Movements object.
- Select Crouch Movement/Digital no Crawl.
- Open the advanced settings and click the Shrink Head and Sides box.
- Enter a New Head Height of 0.5.
- Move the Crouch ground movement to be above the existing GroundMovement in the list (to do this click the gear icon in the top right of the component inspector and select the Move Up option).
- Apply changes to the prefab.
Important: If you are using a prefab you MUST apply the changes to the prefab for this to work. Unity has a bug where the prefabs component order overrides the component order of the GameObject (unlike any other time when the GameObjects settings override the Prefabs).
Add Missing Components
The prefab you based your character on may not have all of the behaviours you want. This section covers a few components you may want to add.
If you want your character to take damage or just to be able to die, respawn, etc, then it needs a CharacterHealth.
To set it up, add the CharacterHealth component to the same gameObject as your Character.
There are a lot of settings here so its best to read about them in the article:
In order to collect items a Character needs an ItemManager.
To set it up, add this ItemManager component to your Character GameObject.
No additional setup is required for basic behaviour but you may want to read the article:
Power-ups a re special kinds of items which give your character additions abilities. PowerUps require a PowerUp Responder to work.
To set it up, add the PowerUpResponder component to the same gameObject as your Character or to a child GameObject of your character
There are a lot of settings here so its best to read about them in the article: