Reading time: 3 minutes
In this Unity tutorial, I will show you how to create a simple 2D platformer game using the Unity game engine. We will cover the following topics:
- Setting up the project in Unity
- Level and obstacle design
- Player movement
- Collisions and collision detection
- Game control and scoring
Let’s get started.
- Setting up the project in Unity
First, we open Unity and create a new project. On the start screen, select “New project” and give our project a name. Make sure the “2D” option is selected at the bottom right of the screen. Click “Create,” and Unity will create the new project.
Once we have created our project, we can start working on it. In the “Hierarchy” window, we can see the objects we have created in our scene. In the “Inspector” window, we can edit the properties of the objects.
- Level and obstacle design
To create our levels, we can use Unity’s “Tilemap” tool. This tool allows us to create a grid of tiles and place them in our scene. We can select different types of tiles to create our level, such as floor tiles, walls, and obstacles.
To create an obstacle, we can use a tile with a collider. A collider is a component that allows us to detect collisions between objects. To create a collider, we can right-click on our tile and select “Add Component” -> “Physics 2D” -> “Box Collider 2D”.
- Player movement
To create player movement, we can use a C# script. First, we create a new script by right-clicking on our “Assets” folder and selecting “Create” -> “C# Script”. Give our script a name and open it in the code editor.
In our script, we create a public variable that represents the player’s speed. Then, in the “Update” function, we use the player’s input to move the player through the scene.
Here is an example of code:
public float speed; void Update() { float horizontalInput = Input.GetAxis("Horizontal"); transform.Translate(Vector2.right * horizontalInput * speed * Time.deltaTime); }
In this code, we use the player’s input to get the horizontal movement direction. Then, we use Unity’s “Translate” function to move the player along the horizontal axis.
- Collisions and collision detection
To detect collisions between objects, we can use a C# script and the colliders of the objects. First, we add a collider to our player and obstacles. Then, we create a new C# script and attach it to our player.
In our script, we use the “OnCollisionEnter2D” function to detect collisions between the player and obstacles. Here is an example of code:
void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Obstacle")) { // Here, we can restart the level or reduce the player's health } }
In this code, we use the Unity function “CompareTag” to check if the object the player collided with is an obstacle. If it is, we can perform an action, such as restarting the level or reducing the player’s health.
- Game control and scoring
To control our game and keep track of the score, we can use another C# script. First, we create an empty object in our scene and name it “GameController”. Then, we create a new C# script and attach it to our “GameController” object.
In our script, we use public variables to keep track of the score and game state. We use the “OnCollisionEnter2D” function to increase the player’s score when they collect an object. Here is an example of code:
public int score; public bool gameOver; void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Collectible")) { score++; Destroy(collision.gameObject); } if (collision.gameObject.CompareTag("Obstacle")) { gameOver = true; } }
In this code, we use Unity’s “Destroy” function to destroy the object the player has collected. Then, we use a boolean variable to detect when the player has lost the game.
To display the score on the screen, we can use a Unity text object and change its content in our script. Here is an example of code:
public Text scoreText; void Update() { scoreText.text = "Score: " + score; }
In this code, we use the “score” variable to update the content of the text object on the screen.
With these basic concepts, you can start creating your own 2D platformer game in Unity. Remember that game development is an ongoing process, and there are many ways to improve and expand your game. Have fun creating!