Creating a Simple 2D Platformer in Unity
In this tutorial, we will create a basic 2D platformer in Unity. This will involve creating a player character that can move, jump, and interact with platforms. We will also add some basic game elements like obstacles and collectibles.
1. Setting Up the Project
Let's start by setting up a new Unity 2D project:
- Open Unity and create a new 2D project.
- Go to File > New Scene and select 2D Mode.
- Save the scene as MainScene.
2. Creating the Player Character
We will first create a simple player character using a sprite and add movement functionality:
- Right-click in the Hierarchy window and select Create Empty. Name this object Player.
- Add a Sprite Renderer component to the Player object by clicking Add Component in the Inspector.
- Select a sprite to use as the character, either by creating your own or importing a 2D asset from the Unity Asset Store.
- Add a Rigidbody2D and a BoxCollider2D to the Player object for physics and collision detection.
3. Writing the Player Movement Script
Now, let's write a script to allow the player to move and jump. Follow these steps:
- Right-click in the Project window and choose Create > C# Script. Name it PlayerController.
- Attach this script to the Player object by dragging it from the Project window to the Player in the Hierarchy.
Replace the contents of the script with the following:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 7f;
private Rigidbody2D rb;
private bool isGrounded = true;
void Start()
{
rb = GetComponent();
}
void Update()
{
// Player movement
float moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
// Jumping
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
isGrounded = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
// Check if player is touching the ground
if (collision.collider.CompareTag("Ground"))
{
isGrounded = true;
}
}
}
4. Creating Platforms and Environment
Now we will create platforms for the player to jump on:
- In the Hierarchy, right-click and select Create Empty, then name it Platform.
- Add a BoxCollider2D and Sprite Renderer to the platform.
- Select a sprite for the platform (e.g., a flat rectangle).
- Duplicate the platform by pressing Ctrl+D (or Cmd+D on Mac) and position the copies to create a level.
- Tag these objects as Ground in the Inspector for the player's jump logic.
5. Adding Obstacles
To make the game more challenging, let’s add some obstacles:
- Create another empty GameObject and name it Obstacle.
- Add a BoxCollider2D and Sprite Renderer to this object.
- Choose a sprite for the obstacle (e.g., a spike or hazard).
- In the player’s OnCollisionEnter2D method, you can detect collisions with obstacles and restart the game or deduct health.
6. Adding Collectibles
We can also add collectibles, such as coins or power-ups:
- Create another empty GameObject and name it Collectible.
- Add a CircleCollider2D (set it as a trigger) and a Sprite Renderer to represent the collectible.
- Write a simple script to detect when the player collects an item:
using UnityEngine;
public class Collectible : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
// Add points or other effects here
Destroy(gameObject); // Remove the collectible from the scene
}
}
}
7. Testing and Tweaking the Game
Now that the basic mechanics are in place, you can test the game by hitting the Play button. Try adjusting the following:
- Modify the moveSpeed and jumpForce values to tweak how fast the player moves and jumps.
- Duplicate platforms and obstacles to create different levels.
- Add background elements or decorations to make the game visually appealing.
8. Enhancing the Platformer
To take your platformer to the next level, consider adding:
- Animations for the player, such as walking and jumping animations.
- Sound effects for jumping, collecting items, and hitting obstacles.
- A camera that follows the player’s movements for a more dynamic view of the game.
Conclusion
With this simple 2D platformer setup, you now have a basic game structure that can be expanded with more features, levels, and polish. Experiment with different mechanics and create your own unique platformer.