Question How to make only one door open, and not all doors

alppi00a

Member
Joined
Mar 18, 2020
Messages
11
Programming Experience
Beginner
sorry to bother again but how can i get one door to open when i click it beacuse now all doors open when i try to open just one :D

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;




public class DoorOpen : MonoBehaviour
{
    public GameObject Playerr;
    public PlayerRay playerray;  
    public Vector3 AfterAngles;
    public Animator animator;
    public Vector3 CurrentAngles;
    public GameObject Door;
    public float x;
    public float y;
    public float z;
    public float x1;
    public float y1;
    public float z1;
    public bool DoorIsOpen = false;
    public bool DoorIsClosed = true;
    public bool PlayerIsNear = false;
    public Text InfoText;
    public GameObject TextParent;


   
    public void Start()
    {

      

        playerray = Playerr.GetComponent<PlayerRay>();




        TextParent.SetActive(false);

        Door = this.gameObject;



        animator = GetComponent<Animator>();
    }


    public void Update()
    {
      


        if (Input.GetKeyDown(KeyCode.E) && DoorIsOpen == false && PlayerIsNear == true)
        {
            animator.SetTrigger("OpenDoor");


            x = CurrentAngles.x;
            y = CurrentAngles.y;
            z = CurrentAngles.z;

            Door.GetComponent<Transform>().eulerAngles = CurrentAngles;

            Door.GetComponent<Transform>().eulerAngles.Set(x, y -= 90, z);

            Door.GetComponent<Transform>().eulerAngles = AfterAngles;

            x1 = AfterAngles.x;
            y1 = AfterAngles.y;
            z1 = AfterAngles.z;
        }

        if (Input.GetKeyDown(KeyCode.E) && DoorIsOpen == true && PlayerIsNear == true)
        {
            animator.SetTrigger("CloseDoor");


            x = CurrentAngles.x;
            y = CurrentAngles.y;
            z = CurrentAngles.z;

            Door.GetComponent<Transform>().eulerAngles = CurrentAngles;

            Door.GetComponent<Transform>().eulerAngles.Set(x, y += 90, z);

            Door.GetComponent<Transform>().eulerAngles = AfterAngles;

            x1 = AfterAngles.x;
            y1 = AfterAngles.y;
            z1 = AfterAngles.z;
        }

        if (playerray.IsOpen == true)
        {
            //Debug.Log("we hit" + hit.collider.name + " " + hit.point);
            rayopen();
          

        }
        else
        {
            rayclose();
        }

       
    }


    public void open()
    {
        DoorIsOpen = true;
        DoorIsClosed = false;
    }
    public void close()
    {
        DoorIsOpen = false;
        DoorIsClosed = true;
    }

   



        public void rayopen()
        {

        
                TextParent.SetActive(true);

                this.gameObject.GetComponentInChildren<DoorOpen>(PlayerIsNear = true);

                
          
                Debug.Log("welcome");

                InfoText.text = ("press E");



        }

        public void rayclose()
        {

        this.gameObject.GetComponentInChildren<DoorOpen>(PlayerIsNear = false);
       

            TextParent.SetActive(false);



        }
  



}


C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerRay : MonoBehaviour
{

    public bool IsOpen;
    public GameObject DoorParent;
    public DoorOpen dooropen;
    public LayerMask door;
    public GameObject ThisDoor;
    Camera cam;
   
    void Start()
    {
        ThisDoor = DoorParent.GetComponentInChildren<DoorOpen>().Door;

        dooropen = DoorParent.GetComponentInChildren<DoorOpen>();

        cam = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 5, door))
        {
            Debug.Log("we hit" + hit.collider.name + " " + hit.point);
          
            IsOpen = true;
          

        }
        else
        {
           
            IsOpen = false;

        }
    }
}
 
Last edited:
Why do you need two booleans for the same action?

That's a lot of code to read through and what is the point in posting commented code? If its not meant to be there, then remove it.

Which part of your code is responsible for all doors opening?
 
Why do you need two booleans for the same action?

That's a lot of code to read through and what is the point in posting commented code? If its not meant to be there, then remove it.

Which part of your code is responsible for all doors opening?

player ray script is only to ”draw” lime from mouse to forwards and to get information from what it hit and giving the dooropen script command when to close and when to openg .

in the dooropen script i only control door animations and doors opening and closing.

I did it using colliders before but then i thought that using rays is easier so now i got it to work except every door opens and closes at the same time

Every doors playerisnear bool turns true at the sametime
 
I have no clue how this all hangs together, but in this line in rayopen()

C#:
this.gameObject.GetComponentInChildren<DoorOpen>(PlayerIsNear = true);
are you sure the = should not be == ? Same in rayclose().

Why are you testing booleans with == ? Instead of
C#:
if (Input.GetKeyDown(KeyCode.E) && DoorIsOpen == false && PlayerIsNear == true
you should just have
C#:
if (Input.GetKeyDown(KeyCode.E) && ! DoorIsOpen && PlayerIsNear
 
Back
Top Bottom