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

यह ट्यूटोरियल दिखाएगा कि CharacterController के लिए Unity में मूविंग प्लेटफ़ॉर्म सपोर्ट कैसे जोड़ा जाए।

तो चलिए शुरू करते हैं!

कदम

  • अपने कैरेक्टर कंट्रोलर को दृश्य में रखें
  • एक नई स्क्रिप्ट बनाएं, इसे "SC_MovingPlatform" कहें और इसके अंदर नीचे दिया गया कोड पेस्ट करें:

SC_MovingPlatform.cs

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

public class SC_MovingPlatform : MonoBehaviour
{
    public Transform activePlatform;

    CharacterController controller;
    Vector3 moveDirection;
    Vector3 activeGlobalPlatformPoint;
    Vector3 activeLocalPlatformPoint;
    Quaternion activeGlobalPlatformRotation;
    Quaternion activeLocalPlatformRotation;

    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        if (activePlatform != null)
        {
            Vector3 newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
            moveDirection = newGlobalPlatformPoint - activeGlobalPlatformPoint;
            if (moveDirection.magnitude > 0.01f)
            {
                controller.Move(moveDirection);
            }
            if (activePlatform)
            {
                // Support moving platform rotation
                Quaternion newGlobalPlatformRotation = activePlatform.rotation * activeLocalPlatformRotation;
                Quaternion rotationDiff = newGlobalPlatformRotation * Quaternion.Inverse(activeGlobalPlatformRotation);
                // Prevent rotation of the local up vector
                rotationDiff = Quaternion.FromToRotation(rotationDiff * Vector3.up, Vector3.up) * rotationDiff;
                transform.rotation = rotationDiff * transform.rotation;
                transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);

                UpdateMovingPlatform();
            }
        }
        else
        {
            if (moveDirection.magnitude > 0.01f)
            {
                moveDirection = Vector3.Lerp(moveDirection, Vector3.zero, Time.deltaTime);
                controller.Move(moveDirection);
            }
        }
    }

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        // Make sure we are really standing on a straight platform *NEW*
        // Not on the underside of one and not falling down from it either!
        if (hit.moveDirection.y < -0.9 && hit.normal.y > 0.41)
        {
            if (activePlatform != hit.collider.transform)
            {
                activePlatform = hit.collider.transform;
                UpdateMovingPlatform();
            }
        }
        else
        {
            activePlatform = null;
        }
    }

    void UpdateMovingPlatform()
    {
        activeGlobalPlatformPoint = transform.position;
        activeLocalPlatformPoint = activePlatform.InverseTransformPoint(transform.position);
        // Support moving platform rotation
        activeGlobalPlatformRotation = transform.rotation;
        activeLocalPlatformRotation = Quaternion.Inverse(activePlatform.rotation) * transform.rotation;
    }
}

मूविंग प्लेटफ़ॉर्म समर्थन अब तैयार है:

Sharp Coder वीडियो प्लेयर

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