Unity 2D Game Development, Create a Platform Game Including Jumps, Obstacles, and Enemies.

Hello, everyone! In this post, we will take an in-depth look at how to develop a simple 2D platform game using Unity. This tutorial will guide you step by step through the process of creating a basic platform game that includes the main character’s jumping ability, obstacles, and enemies. Learn the basics of Unity and more through this course!

Table of Contents

1. Project Setup

First, let’s open the Unity editor and create a new project. Select the ‘2D’ template, specify the project name and location, and then click the ‘Create’ button. Once the project opens, a basic 2D environment will be prepared.

Using the Asset Store

You can utilize the Asset Store to create the basic character, obstacles, and backgrounds needed for your game. Go to ‘Window’ -> ‘Asset Store’ and enter ‘2D Platformer’ or your desired keywords to download the necessary graphic assets.

2. Creating the Basic Character

Now, let’s create the basic character. Right-click in the project view and select 2D Object -> Sprite to create a new sprite. Name the created sprite ‘Player’. Then, set the character image for the selected sprite.

Adding Player Script

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    private Rigidbody2D rb;
    private Vector2 movement;

    void Start()
    {
        rb = GetComponent();
    }

    void Update()
    {
        movement.x = Input.GetAxis("Horizontal");
        movement.y = Input.GetAxis("Vertical");
    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}

This script simply implements the player’s movement. You need to add the ‘Rigidbody2D’ component to the player object.

3. Implementing the Jump Mechanism

Let’s add a jump mechanism so the player can jump. We will modify the PlayerController script to add this jump feature.

public float jumpForce = 300f;
    private bool isGrounded;
    public Transform groundCheck;
    public LayerMask groundLayer;

    void Update()
    {
        movement.x = Input.GetAxis("Horizontal");
        
        // Check for jump
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            rb.AddForce(new Vector2(0f, jumpForce));
        }
    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, groundLayer);
    }

This code applies the jump force to push the character upward when the jump button is pressed. ‘groundCheck’ is used to verify if the player is touching the ground.

4. Adding Obstacles

Let’s add obstacles to increase the difficulty of the game. Create a simple obstacle sprite and save it as ‘Obstacle’. Add ‘BoxCollider2D’ and ‘Rigidbody2D’ components to the obstacle. Set the Body Type of ‘Rigidbody2D’ to Kinematic to prevent it from being affected by the physics engine.

Adding Obstacle Script

using UnityEngine;

public class Obstacle : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            // Handle Game Over
            Debug.Log("Game Over!");
            // Logic to restart or exit the game can be added
        }
    }
}

The above code performs a simple function that outputs a ‘Game Over’ message when the player collides with the obstacle. Based on this message, you can implement a game over screen or restart logic.

5. Adding Enemy Characters

Now, let’s add enemy characters to make the game more interesting. Create the enemy character as a sprite and name it ‘Enemy’. Add ‘Rigidbody2D’ and ‘BoxCollider2D’ to the enemy character, setting the Body Type of ‘Rigidbody2D’ to Kinematic.

Adding Enemy AI Script

using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float moveSpeed = 2f;
    public float moveRange = 3f;
    private Vector2 startPosition;

    void Start()
    {
        startPosition = transform.position;
    }

    void Update()
    {
        float newPosX = Mathf.PingPong(Time.time * moveSpeed, moveRange) + startPosition.x;
        transform.position = new Vector2(newPosX, transform.position.y);
    }
}

The above code contains a simple AI logic that moves the enemy character back and forth. It uses the ‘Mathf.PingPong’ function to set movement within a certain range. You can further complexify the enemy character’s behavior as needed.

6. Building and Testing the Game

Now that all elements are in place, let’s build and test the game. Go to ‘File’ -> ‘Build Settings’ in the top menu to select the platform to build for. If necessary, add the current scene in ‘Scenes in Build’ and click the ‘Build’ button.

Once the build is complete, run the game and test the character’s jumping, obstacles, and enemy behaviors. You can proceed with additional features or debugging to improve the game’s quality as needed.

Conclusion

In this tutorial, we explored the process of creating a simple 2D platform game. By implementing character movement, jumping, obstacles, and enemy AI, we experienced the fundamental skills of game development in Unity. Use this tutorial as a foundation to unleash your creativity and create richer games!

In future posts, we will cover adding in-game UI or implementing audio effects, so stay tuned. May your journey in game development always be enjoyable and creative!