Game 2D platformer for Unity

Introduction

What is 2d platformar?

Talking about the gameplay, usually, players control a character who moves from left to right across the screen, navigating various obstacles, jumping between platforms, and defeating enemies.

Used technology:

C#, Unity



Features

  1. Smooth moment
  2. Direction moment
  3. Head check
  4. Ground check
  5. Customisable parameters


How to implement?

First, create a new project. Then, add several 2D boxes as illustrated in the provided image. Layer these boxes to form a ground surface, ensuring they are positioned correctly. Finally, attach a Box Collider 2D component to each of the boxes. This will allow for proper collision detection in your project.

To create a player object, you can either add a capsule or use a player sprite.



Attach a 2D capsule collider and a Rigidbody2D component to the player.



Add two empty game objects to the player, naming them GroundCheck and HeadCheck, and position them at the head and foot.



Please attach the script to the player provided below.

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

public class Platformer_Script : MonoBehaviour
{
    private Rigidbody2D rb;
    //private Animator animator;

    [Header("Prameters:")]
    public float speed = 5f;
    public float scale = 1.5f;
    public float jumpForce = 5f;
    public float lifes = 0f;

    [Header("Ground Check:")]
    public bool isGrounded = false;
    public bool DrawGroundCheck = true;
    public float GroundCheckRadius = 0.1f;       // Radius to check for the ground
    public LayerMask GroundLayer;               // Layer mask for the ground
    public Transform GroundCheckPosition;

    [Header("Head Check:")]
    public bool isHeaded = false;
    public bool DrawHeadCheck = true;
    public float HeadCheckRadius = 0.1f;       // Radius to check for the ground
    public LayerMask HeadLayer;               // Layer mask for the ground
    public Transform HeadCheckPosition;

    private float horizontalInput;

    void Start()
    {
        transform.localScale = new Vector3(scale, scale, scale);
        //animator = GetComponent();
        rb = GetComponent();
    }

    void Update()
    {
        // Get horizontal input from keyboard or virtual controller
        horizontalInput = 0f;
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            horizontalInput = 1f;
        }
        else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            horizontalInput = -1f;
        }

        // Set running animation
        //animator.SetBool("isRunning", horizontalInput != 0);

        // Handle flipping the player
        if (horizontalInput != 0)
        {
            transform.localScale = new Vector3(Mathf.Sign(horizontalInput) * scale, scale, scale);
        }

        // Handle jumping
        if ((Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.UpArrow)))
        {
            Jump();
        }

        // Check if grounded
        Collider2D groundDetected = Physics2D.OverlapCircle(GroundCheckPosition.position, GroundCheckRadius, GroundLayer);
        isGrounded = groundDetected != null;

        Collider2D headDetected = Physics2D.OverlapCircle(HeadCheckPosition.position, HeadCheckRadius, HeadLayer);

        if (headDetected != null && !isHeaded)
        {
            isHeaded = true;
        }
        if (isGrounded)
        {
            isHeaded = false;
        }

        // Set jumping animation
        //animator.SetBool("isJump", !isGrounded);
    }

    void FixedUpdate()
    {
        // Apply movement using Rigidbody2D
        rb.velocity = new Vector2(horizontalInput * speed, rb.velocity.y);
    }

    public void Jump()
    {
        if (isGrounded)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    void OnDrawGizmosSelected()
    {
        // Visualize the detection radius in the Scene view
        if (DrawGroundCheck)
        {
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(GroundCheckPosition.position, GroundCheckRadius);
        }

        if (DrawHeadCheck)
        {
            Gizmos.color = Color.green;
            Gizmos.DrawWireSphere(HeadCheckPosition.position, HeadCheckRadius);
        }
    }
}     


Set those parameters to the script that show in the image.

Use Cinemachine for camera movements. Navigate to Window -> Package Manager -> Unity Registry and select Cinemachine.

After installing Cinemachine, add a CinemachineBrain component to the Main Camera. To add a Virtual Camera, right-click on the Scene tab, then navigate to Cinemachine and select Virtual Camera.

Next, change the Body parameter from Transposer to Framing Transposer, and set the Aim parameter from Composer to "Do Nothing.



Please access the Virtual Camera and adjust the settings according to the parameters displayed in the image.

It’s done now. You can run it and check the results.

Conclusion

The purpose of this is strictly for use in personal projects only.