यूनिटी में 2डी प्लेटफ़ॉर्मर कैरेक्टर कंट्रोलर में डबल जंप सपोर्ट जोड़ना

इस ट्यूटोरियल में, हम डबल जंप फीचर को शामिल करके Unity में 2D प्लेटफ़ॉर्मर प्लेयर को बढ़ाएंगे।

यह संशोधन मार्गदर्शिका मानती है कि आपने पहले ही निम्नलिखित ट्यूटोरियल का अनुसरण कर लिया है: 2D कैरेक्टर कंट्रोलर के लिए Unity।

चरण 1: चर घोषित करें

डबल जंप सुविधा को प्रबंधित करने के लिए मौजूदा स्क्रिप्ट में निम्नलिखित वेरिएबल जोड़ें:

public int maxJumps = 2;
int jumpsRemaining;

ये चर अनुमत छलांगों की अधिकतम संख्या 'maxJumps' और शेष छलांगों 'jumpsRemaining' पर नज़र रखेंगे।

चरण 2: जंपिंग लॉजिक को संशोधित करें

डबल जंप सुविधा को लागू करने के लिए 'void Update()' विधि में जंपिंग लॉजिक को समायोजित करें:

// Jumping
if (Input.GetKeyDown(KeyCode.W))
{
    if (isGrounded || jumpsRemaining > 0)
    {
        r2d.velocity = new Vector2(r2d.velocity.x, jumpHeight);

        // Reset jumps when grounded
        if (isGrounded)
        {
            jumpsRemaining = maxJumps;
        }
        jumpsRemaining--;
    }
}

यह संशोधन खिलाड़ी को कूदने की अनुमति देता है यदि वह ग्राउंडेड है या अभी भी छलांग बाकी है।

नीचे अंतिम संशोधित स्क्रिप्ट देखें:

'CharacterController2D.cs'

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

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(CapsuleCollider2D))]

public class CharacterController2D : MonoBehaviour
{
    // Move player in 2D space
    public float maxSpeed = 3.4f;
    public float jumpHeight = 6.5f;
    public float gravityScale = 1.5f;
    public Camera mainCamera;

    public int maxJumps = 2;
    int jumpsRemaining;

    bool facingRight = true;
    float moveDirection = 0;
    bool isGrounded = false;
    Vector3 cameraPos;
    Rigidbody2D r2d;
    CapsuleCollider2D mainCollider;
    Transform t;

    // Use this for initialization
    void Start()
    {
        t = transform;
        r2d = GetComponent<Rigidbody2D>();
        mainCollider = GetComponent<CapsuleCollider2D>();
        r2d.freezeRotation = true;
        r2d.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
        r2d.gravityScale = gravityScale;
        facingRight = t.localScale.x > 0;

        if (mainCamera)
        {
            cameraPos = mainCamera.transform.position;
        }
    }

    // Update is called once per frame
    void Update()
    {
        // Movement controls
        if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (isGrounded || Mathf.Abs(r2d.velocity.x) > 0.01f))
        {
            moveDirection = Input.GetKey(KeyCode.A) ? -1 : 1;
        }
        else
        {
            if (isGrounded || r2d.velocity.magnitude < 0.01f)
            {
                moveDirection = 0;
            }
        }

        // Change facing direction
        if (moveDirection != 0)
        {
            if (moveDirection > 0 && !facingRight)
            {
                facingRight = true;
                t.localScale = new Vector3(Mathf.Abs(t.localScale.x), t.localScale.y, transform.localScale.z);
            }
            if (moveDirection < 0 && facingRight)
            {
                facingRight = false;
                t.localScale = new Vector3(-Mathf.Abs(t.localScale.x), t.localScale.y, t.localScale.z);
            }
        }

        // Jumping
        if (Input.GetKeyDown(KeyCode.W))
        {
            if (isGrounded || jumpsRemaining > 0)
            {
                r2d.velocity = new Vector2(r2d.velocity.x, jumpHeight);

                // Reset jumps when grounded
                if (isGrounded)
                {
                    jumpsRemaining = maxJumps;
                }
                jumpsRemaining--;
            }
        }

        // Camera follow
        if (mainCamera)
        {
            mainCamera.transform.position = new Vector3(t.position.x, cameraPos.y, cameraPos.z);
        }
    }

    void FixedUpdate()
    {
        Bounds colliderBounds = mainCollider.bounds;
        float colliderRadius = mainCollider.size.x * 0.4f * Mathf.Abs(transform.localScale.x);
        Vector3 groundCheckPos = colliderBounds.min + new Vector3(colliderBounds.size.x * 0.5f, colliderRadius * 0.9f, 0);
        // Check if player is grounded
        Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckPos, colliderRadius);
        //Check if any of the overlapping colliders are not player collider, if so, set isGrounded to true
        isGrounded = false;
        if (colliders.Length > 0)
        {
            for (int i = 0; i < colliders.Length; i++)
            {
                if (colliders[i] != mainCollider)
                {
                    isGrounded = true;
                    break;
                }
            }
        }

        // Apply movement velocity
        r2d.velocity = new Vector2((moveDirection) * maxSpeed, r2d.velocity.y);

        // Simple debug
        Debug.DrawLine(groundCheckPos, groundCheckPos - new Vector3(0, colliderRadius, 0), isGrounded ? Color.green : Color.red);
        Debug.DrawLine(groundCheckPos, groundCheckPos - new Vector3(colliderRadius, 0, 0), isGrounded ? Color.green : Color.red);
    }
}

चरण 3: अपने गेम का परीक्षण करें

चलाएं अपना गेम Unity और डबल जंप सुविधा का परीक्षण करें। पात्र को जमीन छोड़ने के बाद हवा में दो बार छलांग लगाने में सक्षम होना चाहिए।

सुझाए गए लेख
यूनिटी में कैरेक्टर कंट्रोलर में मूविंग प्लेटफ़ॉर्म सपोर्ट कैसे जोड़ें
एकता के लिए प्लेयर 3डी और 2डी वॉल जंप ट्यूटोरियल
एकता के लिए 2डी कैरेक्टर नियंत्रक
यूनिटी में कैमरे में हेड बॉबिंग इफ़ेक्ट जोड़ना
यूनिटी में थर्ड-पर्सन कैमरा
यूनिटी में एफपीएस प्लेयर में क्राउचिंग जोड़ना
यूनिटी के लिए टॉप-डाउन प्लेयर कंट्रोलर ट्यूटोरियल