مخطوطات الباب للوحدة

في هذا البرنامج التعليمي، سأوضح كيفية صنع باب كلاسيكي وباب منزلق في Unity.

باب كلاسيكي

الباب الكلاسيكي هو باب عادي يفتح بالتدوير حول مفصلاته.

خطوات

لإنشاء باب عادي في Unity، اتبع الخطوات التالية:

  • قم بإنشاء سكريبت جديد، وسمه 'SC_DoorScript'، وأزل كل شيء منه ثم الصق الكود أدناه:

SC_DoorScript.cs

//Make an empty GameObject and call it "Door"
//Drag and drop your Door model into Scene and rename it to "Body"
//Make sure that the "Door" Object is at the side of the "Body" object (The place where a Door Hinge should be)
//Move the "Body" Object inside "Door"
//Add a Collider (preferably SphereCollider) to "Door" object and make it bigger then the "Body" model
//Assign this script to a "Door" Object (the one with a Trigger Collider)
//Make sure the main Character is tagged "Player"
//Upon walking into trigger area press "F" to open / close the door

using UnityEngine;

public class SC_DoorScript : MonoBehaviour
{
    // Smoothly open a door
    public AnimationCurve openSpeedCurve = new AnimationCurve(new Keyframe[] { new Keyframe(0, 1, 0, 0), new Keyframe(0.8f, 1, 0, 0), new Keyframe(1, 0, 0, 0) }); //Contols the open speed at a specific time (ex. the door opens fast at the start then slows down at the end)
    public float openSpeedMultiplier = 2.0f; //Increasing this value will make the door open faster
    public float doorOpenAngle = 90.0f; //Global door open speed that will multiply the openSpeedCurve

    bool open = false;
    bool enter = false;

    float defaultRotationAngle;
    float currentRotationAngle;
    float openTime = 0;

    void Start()
    {
        defaultRotationAngle = transform.localEulerAngles.y;
        currentRotationAngle = transform.localEulerAngles.y;

        //Set Collider as trigger
        GetComponent<Collider>().isTrigger = true;
    }

    // Main function
    void Update()
    {
        if (openTime < 1)
        {
            openTime += Time.deltaTime * openSpeedMultiplier * openSpeedCurve.Evaluate(openTime);
        }
        transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Mathf.LerpAngle(currentRotationAngle, defaultRotationAngle + (open ? doorOpenAngle : 0), openTime), transform.localEulerAngles.z);

        if (Input.GetKeyDown(KeyCode.F) && enter)
        {
            open = !open;
            currentRotationAngle = transform.localEulerAngles.y;
            openTime = 0;
        }
    }

    // Display a simple info message when player is inside the trigger area (This is for testing purposes only so you can remove it)
    void OnGUI()
    {
        if (enter)
        {
            GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 155, 30), "Press 'F' to " + (open ? "close" : "open") + " the door");
        }
    }
    //

    // Activate the Main function when Player enter the trigger area
    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            enter = true;
        }
    }

    // Deactivate the Main function when Player exit the trigger area
    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            enter = false;
        }
    }
}
  • قم بسحب وإفلات نموذج الباب الخاص بك في عرض المشهد (أو قم بإنشاء مكعب جديد وقم بقياسه ليشبه الباب)
  • قم بإنشاء GameObject جديد (GameObject -> إنشاء فارغ) وقم بتسميته "Door"
  • قم بتحريك الكائن "Door" إلى الموضع الذي يجب أن تكون فيه مفصلة الباب

موضع كائن مفصل باب الوحدة

  • أرفق مكون SphereCollider بالكائن "Door" وقم بتغيير نصف قطره بحيث يكون أكبر من الباب (ستكون هذه هي المنطقة التي سيتمكن اللاعب منها من فتح الباب)
  • انقل نموذج الباب الخاص بك داخل الكائن "Door"
  • تأكد من وضع علامة على المشغل الخاص بك باسم "Player"
  • عند الدخول إلى منطقة الزناد، يجب أن تكون قادرًا على فتح/إغلاق الباب بالضغط على 'F'.

باب جرار

الباب المنزلق هو باب يفتح عن طريق الانزلاق في اتجاه محدد (على سبيل المثال، لأعلى أو لأسفل أو لليسار أو لليمين) ويستخدم غالبًا في مستويات الخيال العلمي.

خطوات

لإنشاء باب منزلق في Unity، اتبع الخطوات التالية:

  • أنشئ سكريبت جديد وسميه 'SC_SlidingDoor' وأزل كل شيء منه ثم الصق الكود أدناه:

SC_SlidingDoor.cs

//Make an empty GameObject and call it "SlidingDoor"
//Drag and drop your Door model into Scene and rename it to "Body"
//Move the "Body" Object inside "SlidingDoor"
//Add a Collider (preferably SphereCollider) to "SlidingDoor" Object and make it bigger then the "Body" model
//Assign this script to a "SlidingDoor" Object (the one with a Trigger Collider)
//Make sure the main Character is tagged "Player"
//Upon walking into trigger area the door should Open automatically

using UnityEngine;

public class SC_SlidingDoor : MonoBehaviour
{
    // Sliding door
    public AnimationCurve openSpeedCurve = new AnimationCurve(new Keyframe[] { new Keyframe(0, 1, 0, 0), new Keyframe(0.8f, 1, 0, 0), new Keyframe(1, 0, 0, 0) }); //Contols the open speed at a specific time (ex. the door opens fast at the start then slows down at the end)
    public enum OpenDirection { x, y, z }
    public OpenDirection direction = OpenDirection.y;
    public float openDistance = 3f; //How far should door slide (change direction by entering either a positive or a negative value)
    public float openSpeedMultiplier = 2.0f; //Increasing this value will make the door open faster
    public Transform doorBody; //Door body Transform

    bool open = false;

    Vector3 defaultDoorPosition;
    Vector3 currentDoorPosition;
    float openTime = 0;

    void Start()
    {
        if (doorBody)
        {
            defaultDoorPosition = doorBody.localPosition;
        }

        //Set Collider as trigger
        GetComponent<Collider>().isTrigger = true;
    }

    // Main function
    void Update()
    {
        if (!doorBody)
            return;

        if (openTime < 1)
        {
            openTime += Time.deltaTime * openSpeedMultiplier * openSpeedCurve.Evaluate(openTime);
        }

        if (direction == OpenDirection.x)
        {
            doorBody.localPosition = new Vector3(Mathf.Lerp(currentDoorPosition.x, defaultDoorPosition.x + (open ? openDistance : 0), openTime), doorBody.localPosition.y, doorBody.localPosition.z);
        }
        else if (direction == OpenDirection.y)
        {
            doorBody.localPosition = new Vector3(doorBody.localPosition.x, Mathf.Lerp(currentDoorPosition.y, defaultDoorPosition.y + (open ? openDistance : 0), openTime), doorBody.localPosition.z);
        }
        else if (direction == OpenDirection.z)
        {
            doorBody.localPosition = new Vector3(doorBody.localPosition.x, doorBody.localPosition.y, Mathf.Lerp(currentDoorPosition.z, defaultDoorPosition.z + (open ? openDistance : 0), openTime));
        }
    }

    // Activate the Main function when Player enter the trigger area
    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            open = true;
            currentDoorPosition = doorBody.localPosition;
            openTime = 0;
        }
    }

    // Deactivate the Main function when Player exit the trigger area
    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            open = false;
            currentDoorPosition = doorBody.localPosition;
            openTime = 0;
        }
    }
}
  • قم بسحب وإفلات نموذج الباب الخاص بك في عرض المشهد (أو قم بإنشاء مكعب جديد وقم بقياسه ليشبه الباب)
  • قم بإنشاء GameObject جديد (GameObject -> إنشاء فارغ) وقم بتسميته "SlidingDoor"
  • انقل الكائن "SlidingDoor" إلى الموضع المركزي لنموذج الباب الخاص بك
  • أرفق مكون SphereCollider بالكائن "SlidingDoor" وقم بتغيير نصف قطره بحيث يكون أكبر من الباب (ستكون هذه هي المنطقة التي ستؤدي إلى الحدث المفتوح)
  • انقل نموذج الباب الخاص بك داخل الكائن "SlidingDoor"
  • تأكد من وضع علامة على المشغل الخاص بك باسم "Player"
  • عند الدخول إلى منطقة الزناد، يجب أن يفتح الباب تلقائيًا ثم يُغلق بمجرد مغادرة اللاعب منطقة الزناد.

Sharp Coder مشغل فديوهات

المقالات المقترحة
الماوس نظرة السيناريو للوحدة
العد التنازلي البرنامج التعليمي للوحدة
اختيار وحدة نمط RTS للوحدة
Zone Controller Pro - حزمة Unity Asset Store
كيفية استخدام نظام المياه HDRP الجديد في الوحدة
FPC Swimmer - أصل الوحدة الشامل للبيئات المائية الغامرة
Ultimate Spawner 2.0 - أصل يغير قواعد اللعبة