Script opening multiple doors with GetButtonDown

Hi all, pretty new to Unity and the world of coding, I have a javascript that I basically copied from an internet tutorial, to open a door (01) with a switch. This works fine of itself and the animations are all working correctly. The problem is that I have another door (02) which I would like the user to open directly (no switch) by using the same button. Once again the door does what I want it to, with regard to the animations but the pressing of the “Action” button in this case ‘E’ triggers both the doors to open simultaneously, which needless to say I do not want. The sript is added to the switch of door 01 and the hinge of door 02, which parents the door object.

The Character Controller Script and the first door script are copied below, the second door script is identical to the first, apart from the names “door 2” and “open door 2”. I would also like them to play different sounds to each other. Any help would be most appreciated, I have fried enough braincells so far, suspecting that it is to do with the button press, I just can’t figure it out!
Thanks in advance!

Player Script:

static var DistanceFromTarget : float;

var ToTarget : float;

function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))

ToTarget = hit.distance;
DistanceFromTarget = ToTarget;
}

Door Script:

import UnityEngine.UI;

//varTextDisplay:GameObject;
var TheDistance : float = PlayerCasting.DistanceFromTarget;

var TheDoor1 : GameObject;

var hit : RaycastHit;

function Update () {
TheDistance = PlayerCasting.DistanceFromTarget;

if (Input.GetButtonDown(“Action”))
{

if (TheDistance <=2) {
OpenTheDoor1 ();
}
}
}

/*functionOnMouseOver(){
if(TheDistance<=2){
TextDisplay.GetComponent.().text=“PresstoOpenDoor”;
}
}

functionOnMouseExit(){
TextDisplay.GetComponent.().text=“”;
}
*/
function OpenTheDoor1 () {
var LightHouseDoorShutter : AudioSource = GetComponent.();
LightHouseDoorShutter.Play();
TheDoor1.GetComponent (“Animator”).enabled=true;
yieldWaitForSeconds (6);
TheDoor1.GetComponent (“Animator”).enabled=false;
yieldWaitForSeconds (5);
LightHouseDoorShutter.Play();
TheDoor1.GetComponent (“Animator”).enabled=true;
yieldWaitForSeconds (6);
TheDoor1.GetComponent (“Animator”).enabled=false;
}

The problem is each update your doing a raycast and checking the distance your player is from the nearest thing, no matter what it is. So if your within 2 units of door1, when door2 checks if theDistance <= 2 it will be… because your within 2 steps of door1. If you added a collider to a wall, both your doors would open if you were within 2 spaces of the wall, even if you were 100 spaces from the doors.

If you have multiple doors , instead of raycasting everyTime I’d used triggers to figure out if you were within range. You can add colliders to the player, and colliders to the door. Make the door colliders spheres or box colliders that are 2 units large. You can turn IsKinematic to true so physics doesn’t mess with you… You can also checkMark is trigger. Then Unity will send OnTriggerEnter, OnTriggerStay and OnTriggerExit events to your doors and your player

your door script could look like this:

var isCloseEnough : bool;

void Awake()
{
        isCloseEnough = false;
}

function Update()
{
            if (Input.GetButtonDown(*Action*) && isCloseEnough)
            {
                    // do all the door open stuff
             }
}

function OnTriggerEnter(other: Collider)
{
        isCloseEnough = true;
}

fuction OnTriggerExit( other : Collider)
{
         isCloseEnough = false;
}

Thank you so much for taking the time to help me out takatok, I got it sorted and am on my way to door heaven :smile:

This is what I use to interact with the environment:

using UnityEngine;

public interface IUseable {
    void Use();
}

public class Interact : MonoBehaviour {

    const float MAX_DISTANCE = 1;

    [SerializeField]
    Camera _cam;

    void Start() {
        if (!_cam) _cam = GetComponentInChildren<Camera>();
    }

    void Update () {

        if (Input.GetButtonUp("Use")) {
            Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, MAX_DISTANCE)) {
                if (hit.collider == null) return;
                IUseable useable = hit.transform.GetComponent<IUseable>();

                if (useable != null) {
                    useable.Use();
                }
            }
        }
    }
}

Anything I want to interact with has to implement the IUseable interface.