How to make a 2d platformer in unity

How to make a 2d platformer in unity


To make a 2D platformer in Unity, you will need to create a new 2D project in Unity and import any assets, such as graphics and sounds, that you will be using in your game. You will then need to create a character game object, which will be controlled by the player, and add the necessary components, such as a Rigidbody 2D and a Collider 2D, to make it move and interact with other objects in the game world.

Next, you will need to create platforms for your character to walk on, as well as obstacles and enemies for the player to avoid or defeat. You can use the built-in 2D physics engine in Unity to make objects collide and interact with each other in realistic ways.

To control the character, you will need to use the Input class in Unity to detect player input from the keyboard, mouse, or game controller, and then use this input to move the character and make it jump or perform other actions. You can also use Unity's animation system to create smooth, realistic character animations for things like walking, jumping, and attacking.

Finally, you will need to add a user interface (UI) to your game, which will allow the player to see their score, health, and other important information, as well as start and pause the game. You can use Unity's UI system to create buttons, text labels, and other UI elements, and position them on the screen in a way that looks good and is easy for the player to understand and use.

Overall, creating a 2D platformer in Unity involves using a combination of game objects, components, physics, animation, and UI, to create a fun and engaging game that players can enjoy

Platformer game Scriple example


To create a 2D platformer in Unity, you will need to write scripts to control the behavior of your game objects. For example, you might create a script to control the movement of your player character, which could include code to handle inputs from the keyboard or game controller and update the character's position and animation accordingly.

You could also create scripts for other game objects, such as enemies or power-ups, which define their behavior and how they interact with the player and other objects in the game world. This could include things like making enemies move and attack the player, or making power-ups give the player special abilities when they are picked up.

Here is an example of a script that you might use to control the movement of a player character in a 2D platformer in Unity:


using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // movement speed of the player
    public float speed = 10.0f;

    // reference to the Rigidbody2D component of the player
    private Rigidbody2D rb;

    void Start()
    {
        // get a reference to the Rigidbody2D component on the player
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        // get input from the horizontal axis (left/right arrow keys or A/D keys)
        float horizontalInput = Input.GetAxis("Horizontal");

        // calculate the movement vector based on the input and the player's speed
        Vector2 movement = new Vector2(horizontalInput, 0) * speed;

        // apply the movement vector to the player's Rigidbody2D component
        rb.velocity = movement;
    }
}


This script makes the player move left and right based on the horizontal input from the keyboard or game controller. It uses the Rigidbody2D component to apply the movement to the player, which allows it to collide with other objects and be affected by the 2D physics engine in Unity.

You can modify this script or create new ones to add more functionality and behavior to your 2D platformer game in Unity.

Here is an example of a simple script that you can use to control a 2D character in a platformer game in Unity:



using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpForce = 10f;

    Rigidbody2D rb;
    bool isGrounded = false;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        // Move the player horizontally
        float horizontalInput = Input.GetAxis("Horizontal");
        Vector2 move = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);
        rb.velocity = move;

        // Jump if the player is on the ground and pressing the jump button
        if (isGrounded && Input.GetButtonDown("Jump"))
        {
            rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        // Check if the player is colliding with the ground
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

    void OnCollisionExit2D(Collision2D collision)
    {
        // Check if the player is no longer colliding with the ground
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
}


This script uses the Rigidbody2D component to move the player horizontally and make them jump when the player presses the jump button. It also uses collision detection to keep track of whether the player is on the ground or not, so that they can only jump when they are on the ground.

You can attach this script to your player game object and customize the moveSpeed and jumpForce values to change the way the player moves and jumps in your game. You can also add other features, such as shooting, double jumping, or dashing, by adding more code to this script.

2d player controller for Pc and Mobile with joystick - unity engine