- Home /
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;
}
}
Answer by DavidDebnar · Dec 27, 2013 at 11:21 AM
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:
And then assign it to the door:
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
No problem :) If an answer helps you solve your problem, feel free to check it and 1+ it, so that other people in the future can solve their similar problem quicker.
Answer by 25motion · Dec 27, 2013 at 11:11 AM
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.
Answer by MadJohny · Dec 27, 2013 at 11:06 AM
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
Your answer
Follow this Question
Related Questions
Multi Switch Door 4 Answers
trigger animation problems 2 Answers
Import and launch a animation (with trigger) 2 Answers
Can't click gameobject when over another trigger? 1 Answer
How to fix my door from being destroyed? 0 Answers