एकता के लिए द्वार स्क्रिप्ट

इस ट्यूटोरियल में, मैं दिखाऊंगा कि 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;
        }
    }
}
  • अपने दरवाज़े के मॉडल को दृश्य दृश्य में खींचें और छोड़ें (या एक नया क्यूब बनाएं और उसे एक दरवाज़े जैसा आकार देने के लिए स्केल करें)
  • एक नया गेमऑब्जेक्ट बनाएं (गेमऑब्जेक्ट -> खाली बनाएं) और इसे नाम दें "Door"
  • "Door" ऑब्जेक्ट को उस स्थिति में ले जाएं जहां दरवाजे का कब्ज़ा होना चाहिए

एकता दरवाजा काज वस्तु स्थिति

  • "Door" ऑब्जेक्ट में एक SphereCollider घटक संलग्न करें और इसकी त्रिज्या बदलें ताकि यह एक दरवाजे से बड़ा हो (यह वह क्षेत्र होगा जहां से खिलाड़ी दरवाजा खोलने में सक्षम होगा)
  • अपने दरवाज़े के मॉडल को "Door" ऑब्जेक्ट के अंदर ले जाएँ
  • सुनिश्चित करें कि आपके खिलाड़ी को इस रूप में टैग किया गया है "Player"
  • ट्रिगर क्षेत्र में जाने पर आपको 'F' दबाकर दरवाजा खोलने/बंद करने में सक्षम होना चाहिए।

स्लाइडिंग दरवाजा

स्लाइडिंग दरवाज़ा एक ऐसा दरवाज़ा है जो एक विशिष्ट दिशा (उदा. ऊपर, नीचे, बाएँ या दाएँ) में फिसलकर खुलता है और इसका उपयोग अक्सर Sci-fi स्तर में किया जाता है।

कदम

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;
        }
    }
}
  • अपने दरवाज़े के मॉडल को दृश्य दृश्य में खींचें और छोड़ें (या एक नया क्यूब बनाएं और उसे एक दरवाज़े जैसा आकार देने के लिए स्केल करें)
  • एक नया गेमऑब्जेक्ट बनाएं (गेमऑब्जेक्ट -> खाली बनाएं) और इसे नाम दें "SlidingDoor"
  • "SlidingDoor" ऑब्जेक्ट को अपने दरवाजे के मॉडल की केंद्र स्थिति में ले जाएं
  • "SlidingDoor" ऑब्जेक्ट में एक SphereCollider घटक संलग्न करें और इसकी त्रिज्या बदलें ताकि यह एक दरवाजे से बड़ा हो (यह वह क्षेत्र होगा जो खुली घटना को ट्रिगर करेगा)
  • अपने दरवाज़े के मॉडल को "SlidingDoor" ऑब्जेक्ट के अंदर ले जाएँ
  • सुनिश्चित करें कि आपके खिलाड़ी को इस रूप में टैग किया गया है "Player"
  • ट्रिगर क्षेत्र में जाने पर दरवाज़ा अपने आप खुल जाना चाहिए और खिलाड़ी के ट्रिगर क्षेत्र छोड़ने के बाद बंद हो जाना चाहिए।

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

सुझाए गए लेख
एकता के लिए माउस लुक स्क्रिप्ट
एफपीसी तैराक - गहन जल वातावरण के लिए एक व्यापक एकता परिसंपत्ति
एकता में गति के लिए जॉयस्टिक नियंत्रक कैसे स्थापित करें
एकता के लिए 2डी हाथापाई हमला ट्यूटोरियल
एकता के लिए काउंटडाउन टाइमर ट्यूटोरियल
एकता के लिए आरटीएस-शैली इकाई चयन
एकता में कर्सर ट्रेल प्रभाव बनाने के लिए C# स्क्रिप्ट