How to make platformer game in unity | 2D Character controller

How to make platformer game in unity | 2D Character controller

How to make platformer game in unity | 2D Character controller

To create a 2D platformer game in Unity, you will need to use the following steps:

  1. Create a new project in Unity and select the 2D option.

  2. Create a new scene and add a character sprite to the scene. This can be done by either importing a sprite sheet or creating a sprite from an image file.

  3. Create a script for the character controller and attach it to the character sprite. This script will handle the movement and physics of the character.

  4. Add colliders to the character sprite and to any objects in the scene that the character should be able to interact with, such as platforms or enemies.

  5. Create a script for the platforms and any other interactive objects in the scene. These scripts will handle the behavior of these objects, such as moving platforms or enemy AI.

  6. Create a script for the camera, which will follow the character as it moves through the scene.

  7. Use Unity's built-in animation system to create animations for the character and any other animated objects in the scene.

  8. Add background music and sound effects to enhance the gameplay experience.

  9. Test and debug the game to ensure that it is functioning as intended.

2D Character controller Script example :

Here is an example of a basic 2D character controller script in C# that you can use as a starting point for your project:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterController2D : MonoBehaviour
{
    // movement variables
    public float moveSpeed = 5f;
    public float jumpForce = 5f;
    public bool facingRight = true;

    // ground check variables
    public Transform groundCheck;
    public LayerMask whatIsGround;
    public float groundCheckRadius = 0.2f;
    private bool isGrounded;

    // component references
    private Rigidbody2D rb;
    private Animator anim;
    private SpriteRenderer spriteRenderer;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        // check if character is grounded
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);

        // get input axis
        float horizontalInput = Input.GetAxis("Horizontal");

        // set animator variables
        anim.SetFloat("Speed", Mathf.Abs(horizontalInput));
        anim.SetBool("Grounded", isGrounded);

        // move character
        rb.velocity = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);

        // flip sprite if necessary
        if ((horizontalInput > 0 && !facingRight) || (horizontalInput < 0 && facingRight))
        {
            Flip();
        }

        // jump if character is grounded and jump button is pressed
        if (isGrounded && Input.GetButtonDown("Jump"))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        spriteRenderer.flipX = !spriteRenderer.flipX;
    }
}

This script handles basic movement (left/right and jumping) for a 2D character. It also includes variables for setting the character's speed and jump force, as well as a ground check to determine if the character is touching the ground. It also includes references to the character's Rigidbody2D, Animator, and SpriteRenderer components, which are used for movement, animation, and sprite flipping, respectively.


I hope this helps! Let me know if you have any questions or need further assistance.