How Build Platformer Game In Unity
Unity is a powerful and widely-used game development platform and engine known for its versatility, ease of use, and ability to create stunning interactive experiences across various platforms. With Unity, developers can build games, interactive applications, simulations, and more for a wide range of platforms, including mobile devices, consoles, desktops, and even virtual reality (VR) and augmented reality (AR) devices. Its cross-platform capabilities make it a popular choice for game developers, indie creators, and large studios alike.
A platformer game is a type of video game genre characterized by gameplay that primarily involves controlling a character to navigate a two-dimensional (2D) or three-dimensional (3D) environment, often filled with platforms, obstacles, and challenges. The main objective in platformer games is to move the character through the level, usually from left to right, while overcoming obstacles, jumping across platforms, avoiding hazards, and sometimes defeating enemies.
To create a project in Unity you will need to have Unity and Unity Hub installed. You can download it from Unity's website. To complete the project, follow this tutorial from start to end.
1. Create Project:
First, we open the Unity Hub and Create a 2D Project.
2. Setting the Project:
Begin by setting up a new Unity project. To streamline the process, import the 2D Standard Assets package. Open the Asset Store panel, search for "Standard Assets", and download the package. Afterward, click on the Import button to bring in the assets. In the import window, exclusively select the "Standard Assets > 2D" and "StandardAssets > CrossPlatformInput" folders for import.
Following this, establish two new folders named "Animations" and "Tiles". Within the Tiles folder, create an additional directory labeled "Tile Palettes". While this folder's purpose will become more evident later on, it's crucial for organization.
Now, proceed to import Cinemachine by navigating to Package Manager (Window > Package Manager) and installing the "Cinemachine" package.
Select Cinemachine from left menus.
With the necessary assets in place, it's time to organize your tools. Begin by setting up the Tilemap Editor. Head to Window > 2D > Tile Palette and position it conveniently on your workspace. You can choose a location that makes sense; for instance, between the inspector and the scene view.
Next, configure the Timeline Editor. Access it via Window > Sequencing > Timeline. Note that the positioning of this tab is flexible, so feel free to rearrange it based on your preferences.
With your workspace arranged, you're now ready to begin the creative process!
3. Creating Enviroment:
Access the Tile Palette tab and proceed to generate a new palette titled "Solids". This palette will encompass tiles that the character cannot pass through.
Change the name from Forest to Solid.
Functioning much like its name suggests, a palette is a collection of images utilized for "painting". Keep the default settings unchanged and save it in the previously established Tile Palettes folder within the Tiles directory.
To configure the tiles, navigate to the Environment Tiles > Grass folder and drag the corresponding sprites into the Tile Palette window. Repeat this process for any additional tiles you wish to use. Make sure to save the resulting .asset files in the Tiles folder.
You will see a platform like this.
With all tiles properly arranged, the next step involves creating a "canvas" for painting. In the hierarchy, right-click and access 2D Object > Tilemap.
Select Tilemap from the menu after clicking on 2D objects.
This action effectively generates a grid, within which your "canvas" or tilemap is located. To commence painting, familiarize yourself with the toolbar present in the Tile Palette tab. Start by selecting your rule tile and experiment with the various brushes available. Once you're comfortable with the tools, proceed to construct your level by utilizing the painting techniques.
4. Creating Character:
To ensure our tilemap has the required physics interactions, we need to address the issue where any object placed on it would simply fall through. A remedy for this comes in the form of a new collider component introduced by Unity Technologies: the Tilemap Collider. This component operates as its name implies – it generates a collider around each individual tile. To implement this, navigate to your Tilemap and click on "Add Component".
Set values according to need from the inspector.
Proceed to search for "CharacterRobotBody," and then drag and drop the corresponding character into the scene.
For this purpose, we'll employ the default character included in the 2D Standard Asset pack. You can locate this character by heading to Standard Assets -> 2D -> Prefabs and selecting "CharacterRobotBoy." Alternatively, you can directly search for "CharacterRobotBoy" and access it from there. Once the character is successfully placed within the scene, you can hit the play button to initiate the game, and control the character's movement using the arrow keys. Adjusting the camera positioning might be necessary to properly view the robot's actions. With this step completed, we're ready for the next stage!
5. Creating Enemy:
Navigate to the "Enemies" folder within the asset pack and select an enemy that you believe suits your scene's aesthetic. Place this chosen enemy within your scene in a suitable location.
Recommended by LinkedIn
Subsequently, create a fresh tag titled "Enemy" and apply it to your newly introduced enemy object.
Our next step involves animating this enemy character. You should already have the Animation tab readily available in the lower section of your workspace. If it's not visible, access it by navigating to Window > Animation > Animation.
With your enemy character selected, proceed to click on the "Create" button within the Animation tab. This action will generate a new animation controller, which you can name "Enemy1Controller".
Ensure you save the animator controller in the "Animations" folder you established earlier.
Initiate the recording process by clicking the record button. Within the Sprite field, make a modification to reflect the other provided image (for instance, "Saw_move"). This step may be slightly different depending on the specific enemy you've chosen. Progress approximately four frames ahead in the timeline and revert the image back to its original state.
Advance a few more frames and reinstate the image to its initial form.
Upon hitting the play button, you will observe that our enemy character is now smoothly animated! This adds an appealing dynamic to your scene.
6. Program Enemy:
To implement a mechanism where the level restarts upon the character's contact with the enemy, you need to create a new script along with a box collider attached to the character. Begin by configuring the box collider as a Trigger and ensuring it adequately encompasses the character's dimensions. Name the newly generated script "EnemyReaction".
To maintain an organized structure, establish a new folder named "Scripts" to accommodate this new script.
Here's the content of the EnemyReaction script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // This is very important if we want to restart the level
public class EnemyReaction : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// This function is called every time another collider overlaps the trigger collider
void OnTriggerEnter2D (Collider2D other){
// Checking if the overlapped collider is an enemy
if (other.CompareTag ("Enemy")) {
// This scene HAS TO BE IN THE BUILD SETTINGS!!!
SceneManager.LoadScene ("scene1");
}
}
}
For this script to function correctly, there are a few prerequisites. First, you need to assign a collider to the enemy. A basic box collider generally suffices for this purpose. Subsequently, save the current scene with the name "scene1". Since you're working with a single scene, save it in the root folder. However, if you're planning to work with multiple scenes, consider creating a dedicated folder for them.
Finally, include this scene in the build settings. To achieve this, navigate to File -> Build Settings and click "Add Open Scenes".
With these steps completed, your setup should be functional. Press the play button and observe the character colliding with the enemy, resulting in a level restart. This adds an interactive layer to your game!
7. Cinemachine and Timeline:
We've reached the final section of this tutorial, where we'll be utilizing Cinemachine along with the Timeline editor. Let's begin with Cinemachine. You might have noticed that our camera isn't properly following the character's movements. To resolve this, we can create a Virtual Camera. Locate the Cinemachine option in your toolbar and choose "Create 2D Camera".
Afterward, assign the "follow" field of this camera to the CharacterRobotBoy. Rename it to "PlayerCam". Configure the Aim to "Do Nothing" and set the Body to "Framing Transposer".
Moving on to the Timeline Editor, initiate the process by creating a new empty game object called "Timeline". Inside the Timeline Editor, click on "Create". Name it "TheTimeline" and save it in the root folder.
Though it's possible to have multiple timeline editors in a scene, for simplicity, we're using just one. With this Timeline, our objective is to craft a cutscene where the camera captures the entire level and then zooms in on the player. This sequence will occur right at the beginning of the scene. To achieve this, we'll combine the capabilities of Cinemachine and the Timeline Editor using Cinemachine shot clips. Inside the Timeline, click "Add" then navigate to Cinemachine.Timeline -> Cinemachine Track.
Drag the Main Camera into the designated field. Right-click and choose "Add Cinemachine Shot Clip".
This track allows us to specify the duration for which a particular camera remains active. Let's create a new 2D camera and position it to encompass the entire level. Name this camera "FullLevel".
This will serve as our initial camera. Select the Cinemachine shot clip and associate this camera with the "Virtual Camera" field. Determine the duration for which this camera will remain active. It's helpful to ensure the timescale is set in seconds rather than frames.
Subsequently, we need to generate another clip for the player camera. Position this clip after the first one to ensure a smooth transition. You can drag either clip on top of the other to achieve this. Now, upon playback, a cohesive cutscene unfolds! However, you may have noticed that when the last track concludes, the camera reverts to the first camera instead of staying focused on the player. To rectify this, the simplest approach is to deactivate the first camera after transitioning to the second one. We can accomplish this using an Activation Track.
As the name suggests, the specified game object remains active as long as the track persists. Create an Activation track and assign the first camera to it.
Then adjust the length slightly after the transition point. Upon playback, everything should function smoothly, and the player camera will remain active as intended!
If you like the article please 👍 it, wants to refer somebody 📤 with him/her. We also provide Services of 2D/3D Game Development, 2D/3D Animations,Video Editing, UI/UX Designing.
If you have questions or suggestions about the game or want something to build from us, Feel free to reach out to us anytime!
📱 Mobile: +971 544 614 238
📧 Email: wahhab_mirza@vectorlabzlimited.com