Search Unity

Bug I need help making a door

Discussion in 'Scripting' started by corgiunistudios, Mar 16, 2023.

  1. corgiunistudios

    corgiunistudios

    Joined:
    Oct 5, 2022
    Posts:
    9
    I need help making a door I tried using this script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Open : MonoBehaviour
    6. {
    7.     public bool isOpen = false;
    8.  
    9.     void Update()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.E))
    12.         {
    13.             isOpen = !isOpen;
    14.  
    15.             transform.Rotate(0, 90, 0);
    16.         }
    17.     }
    18. }
    When I use it the doors scale goes weird
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    that there, shouldnt affect the scale at all. I suspect something up your transform hierarchy is scaled 'oddly'.

    That said, you are using a isOpen bool, even changing it, but never test for it... so , the dor will continuously keep rotating.
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    You're missing quite a bit to have a functional door. Here's how to set up an example:
    1. Make a transform at position (0, 0, 0) and put a box collider component on it with a collider size of (2, 2, 2) and select its "Is Trigger" box. Name it "Door Trigger." Put the script below on it.
    2. Make a transform at position (-0.5, 0, 0). Name it "Door Hinge." Select the Door Trigger transform and drag the Door Hinge transform into the scripts Door Hinge variable slot.
    3. Make a cube and make it a child of the door hinge transform. Make its position (0.5, 0, 0), and its scale (1, 2, 0.1). Name it "Door."
    4. Make a capsule at position (-2, 0, -0.5). Name it "Player." Set its tag to "Player." Put a rigidbody component on it and uncheck the rigidbody's "Use Gravity" box.
    5. Uncheck "Maximize on play" in the Game view tab. Press play and go to the scene view tab. Select the player object and drag it into the trigger area. Press E. The door will open. Press E again. The door will close. Drag the player object out of the trigger area. Pressing E will no longer open or close the door.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DoorScript : MonoBehaviour {
    6.  
    7.     public Transform doorHinge;
    8.     bool isOpen = false;
    9.     bool canOpen = false;
    10.  
    11.     void Update () {
    12.  
    13.         if (canOpen) {
    14.             if (Input.GetKeyDown (KeyCode.E)) {
    15.                 if (!isOpen) {
    16.                     doorHinge.rotation = Quaternion.Euler (0, 90, 0);
    17.                     isOpen = true;  
    18.                 }
    19.                 else {
    20.                     doorHinge.rotation = Quaternion.Euler (0, 0, 0);
    21.                     isOpen = false;
    22.                 }
    23.             }
    24.         }
    25.  
    26.     }
    27.  
    28.     void OnTriggerEnter (Collider collider) {
    29.  
    30.         if (collider.CompareTag ("Player")) canOpen = true;
    31.  
    32.     }
    33.  
    34.     void OnTriggerExit (Collider collider) {
    35.  
    36.         if (collider.CompareTag ("Player")) canOpen = false;
    37.  
    38.     }
    39.  
    40. }