Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Open Door

Discussion in 'Scripting' started by ARares, Mar 5, 2017.

  1. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    i have this code but is for one door, and i want for a double door, i modified that code myself but when i press E to leftDoor is opening that that door:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ArmoryDoor : MonoBehaviour
    5. {
    6.  
    7.     [Header("-Door Options-")]
    8.     public float timeLeft;
    9.     public int range;
    10.     public float DoorOpenValue = 112.5f;
    11.  
    12.     [Header("-Door Setup-")]
    13.     public Transform rightDoor;
    14.     public Transform leftDoor;
    15.     public Transform cam;
    16.     public LayerMask mask;
    17.     private RaycastHit hit;
    18.     private bool open;
    19.     private bool isOpeningDoor;
    20.  
    21.  
    22.     void Update()
    23.     {
    24.         if (Input.GetButtonDown("Interact") && timeLeft == 0)
    25.         {
    26.             checkDoor();
    27.         }
    28.  
    29.         if(isOpeningDoor)
    30.         {
    31.             OpenAndCloseDoor();
    32.         }
    33.     }
    34.  
    35.     void checkDoor()
    36.     {
    37.         if(Physics.Raycast(cam.position, cam.forward, out hit, range, mask))
    38.         {
    39.             open = false;
    40.             if(hit.transform.localRotation.eulerAngles.y > 56.25)
    41.             {
    42.                 open = true;
    43.             }
    44.  
    45.             isOpeningDoor = true;
    46.             rightDoor = hit.transform;
    47.         }
    48.     }
    49.  
    50.     void OpenAndCloseDoor()
    51.     {
    52.         timeLeft += Time.deltaTime;
    53.  
    54.         if(open)
    55.         {
    56.             rightDoor.localRotation = Quaternion.Slerp(rightDoor.localRotation, Quaternion.Euler(270, 0, 0), timeLeft);
    57.             leftDoor.localRotation = Quaternion.Slerp(leftDoor.localRotation, Quaternion.Euler(270, 0, 0), timeLeft);
    58.         }
    59.         else
    60.         {
    61.             rightDoor.localRotation = Quaternion.Slerp(rightDoor.localRotation, Quaternion.Euler(270, DoorOpenValue, 0), timeLeft);
    62.             leftDoor.localRotation = Quaternion.Slerp(leftDoor.localRotation, Quaternion.Euler(270, -DoorOpenValue, 0), timeLeft);
    63.         }
    64.  
    65.         if(timeLeft > 1)
    66.         {
    67.             timeLeft = 0;
    68.             isOpeningDoor = false;
    69.         }
    70.     }
    71. }
    72.  
     
  2. booiljoung

    booiljoung

    Joined:
    May 12, 2013
    Posts:
    57
    Hi there!

    Connect the two doors to each other.

    When the E key is pressed, send the message "OnUserActionMessage" to the user's view door.
    In "OnUserActionMessage", the door should not be opened or closed by itself.
    In "OnUserActionMessage", the door sends a "DoAction" message to another door associated with it.
    When the door receives the "DoAction" message, it opens or closes it.

    Is not it simple for you to think?

    if you have more question? mail me : booiljung@me.com.
     
  3. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I solved :))), i made the same code but modified for left door, and it working.
     
  4. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I have a porblem, i have more doors with the same layer: RightDoor and is opening all doors with the layer RightDoor.