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. Dismiss Notice

Question regarding doors...

Discussion in 'Scripting' started by Levinant5150, Sep 19, 2020.

  1. Levinant5150

    Levinant5150

    Joined:
    May 31, 2020
    Posts:
    2
    So currently I'm trying to work on a door controller script, (I'm new to unity) and I'm having some issues with collision detection. Originally, I used checksphere that was able to tell if the player was colliding with a trigger collider, but when I added more doors, it would open them all at once. So I cut that, and now im here...
    Code (CSharp):
    1. public class DoorController : MonoBehaviour
    2. {
    3.     public Animator anim;
    4.  
    5.     public AudioSource doorSound;
    6.  
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         anim = gameObject.GetComponent<Animator>();
    12.     }
    13.  
    14.     //I need this code to check if it collided with one specific collider
    15.     public void OnTriggerStay(Collider other)
    16.     {
    17.         Debug.Log("Open");
    18.  
    19.         if (other.gameObject.tag == "1st floor power door" && Input.GetKeyDown("e"))
    20.         {
    21.             anim.SetTrigger("OpenClose");
    22.  
    23.             GetComponent<AudioSource>().Play();
    24.  
    25.             GetComponent<AudioSource>().volume = Random.Range(0.4f, .8f);
    26.             GetComponent<AudioSource>().pitch = Random.Range(1.2f, 1.8f);
    27.         }
    28.     }
    29. }
    My problem is this... I don't want to use tags, I want to be able to directly reference the trigger game object so I don't have to make a new tag every time I want to make a new door. Is there a way to check if the player controller is colliding with a game object/trigger by referencing it before start?
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    I would use a class for this task. Simply add the class below to each door gameobject:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. //Assign this script to a "Door" Object (the one with a Trigger Collider)
    4. public class OpenableDoor : MonoBehaviour
    5. {
    6.   // you can add variables here to descripe the properties of this door but dont needed
    7.   public bool open = false;
    8.   public bool enter = false;
    9.   public float openSpeed = 1f;
    10. }
    And then check if you entered a door:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DoorController : MonoBehaviour
    4. {
    5.     public Animator anim;
    6.     public AudioSource doorSound;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         anim = gameObject.GetComponent<Animator>();
    11.     }
    12.     //I need this code to check if it collided with one specific collider
    13.     public void OnTriggerStay(Collider other)
    14.     {
    15.         OpenableDoor openableDoor = other.gameObject.GetComponent<OpenableDoor>();
    16.         if (openableDoor!=null) {
    17.           openableDoor.enter = true;
    18.  
    19.           if (Input.GetKeyDown("e"))
    20.           {
    21.              openableDoor.open = true;
    22.              anim.SetTrigger("OpenClose"); // here your could pass the speed too -> openableDoor.openSpeed
    23.              GetComponent<AudioSource>().Play();
    24.              GetComponent<AudioSource>().volume = Random.Range(0.4f, .8f);
    25.              GetComponent<AudioSource>().pitch = Random.Range(1.2f, 1.8f);
    26.           }
    27.         }
    28.     }
    29.  
    30.     public void OnTriggerExit(Collider other)
    31.     {
    32.         OpenableDoor openableDoor = other.gameObject.GetComponent<OpenableDoor>();
    33.         if (openableDoor!=null) {
    34.           openableDoor.enter = false;
    35.         }
    36.     }
    37. }
     
    Last edited: Sep 19, 2020