Doors open when you look at it

Hey guys,I made a door that works but the problem is that it also opens when you are not looking at it.If you can help me modify it,that is helpful.

code:

//add a cube and a empty game object and make the cube a child of the game object and position the game object to the side of the cube
    
      var IsOpen : boolean = false;
     
    var CanOpen : boolean = false;

     
    function Start () {
    }
     
    function Update () {
     
    if(Input.GetButtonUp("E") && !IsOpen && CanOpen)
     
    {
            Opening();
            IsOpen = true;
    }
     
    else if(Input.GetButtonUp("E") && IsOpen && CanOpen)
     
    {
            Closing();
            IsOpen = false;
    }
     
     
    }
     
    function Opening()
    {
    for (var i = 0; i < 100; i++)
    {
        transform.Rotate(0,0.9,0);
       
        yield WaitForSeconds(0.01);
            }
    }
     
    function Closing()
    {
    for (var i = 0; i < 100; i++)
    {
        transform.Rotate(0,-0.9,0);
       
        yield WaitForSeconds(0.01);
            }
    }
     
    function OnTriggerEnter (other : Collider)
    {
            if(other.gameObject.tag == "Player")
            {
                    CanOpen = true;
            }
    }
     
    function OnTriggerExit (other : Collider)
    {
            if(other.gameObject.tag == "Player")
            {
                    CanOpen = false;
            }
    }

What you have to use is called a ‘raycast’. If you don’t know what those are, then imagine a small dot flying in a direction, forming a ray. The function takes in 3 parameters, an origin, direction and hit (for storing raycast data).

Also, we want the raycast to only check interactable objects, so we setup a layer system. This means you have to place the door on a layer called ‘Interactives’. To do this, follow these steps:

Add a new layer:

alt text

alt text

And then assign it to the door:

alt text

Now, the scripts. The first one ‘CameraInteraction.js’ should be attached on the camera, second ‘DoorToggle.js’, on the door.

CameraInteraction.js:

function Update () {
			
			if(Input.GetKeyDown("E")) {
				
				var hit : RaycastHit;
				var mask : LayerMask = LayerMask.NameToLayer("Interactives");

				if(Physics.Raycast(transform.position, transform.forward, hit, 100, mask))
					hit.collider.gameObject.SendMessage("Toggle", SendMessageOptions.DontRequireReceiver);
			}
		}

DoorToggle.js:

public var Speed : float = 10;

	private var isOpen : boolean = false;
	private var defaultAngle : float = 0;

	function Start () { defaultAngle = transform.localEulerAngles.y; }

	function Update () {
		transform.localEulerAngles.y = Mathf.LerpAngle(transform.localEulerAngles.y, defaultAngle + (isOpen ? 90 : 0), Time.deltaTime * Speed);
	}

	function Toggle () { isOpen = !isOpen; }

–David

Hy. You need to RayCast from the camera forward, if there is a door in the line of sight. Check, if the Object you hit has the tag for doors. Do this procedure OnUpdate(). If its true, then you can check if the right key is pressed too.
I hope, this helps.

Use a raycast, make the raycast to go from the camera to transform.forward, and then, if they raycast is hitting, send a message to open the door, let’s use the example script from the scripting reference and modify it for you:

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    void Update() {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, hit))
            if (hit.collider.tag == "Door" && hit.collider.getComponent(DoorScript).canOpen) { // tag the door as Door
                 hit.collider.sendMessage("Open");
    }
}

I think this should get you going, create a script for the door called DoorScript and put a function called Open, and in that funcion, if door is closed, make it open