- Home /
Opening Multiple Doors
Hey guys. So I have created a script that allows me to open multiple doors around my environment. However, I am having trouble creating them as independent doors. ie, 3 may be open and two may be closed. So far I am running into the problem of not being able to open another door, but I can close an already closed door. This is because when the player opens the door it changes a boolean variable that applies to every single door in the game. That is, the player may open one door, but all the rest are apparently open as well.
I am having trouble giving these variables independent boolean variables without unnecessary code copying.
Here is my javascript code:
private var doorIsOpen : boolean = false;
private var currentDoor : GameObject;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
function Update () {
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 3)) {
if(hit.collider.gameObject.tag == "Door" && doorIsOpen == false && Input.GetMouseButtonDown(0)) {
currentDoor = hit.collider.gameObject;
Door(doorOpenSound, true, "doorOpen", currentDoor);
}
if(hit.collider.gameObject.tag == "Door" && doorIsOpen == true && Input.GetMouseButtonDown(1)) {
currentDoor = hit.collider.gameObject;
Door(doorShutSound, false, "doorClose", currentDoor);
}
}
}
function Door (aClip : AudioClip, openCheck : boolean, animName : String, thisDoor : GameObject) {
audio.PlayOneShot(aClip);
doorIsOpen = openCheck;
thisDoor.transform.parent.animation.Play(animName);
}
It doesn't help that you switch whether a door is open in a script that is on your player. I'm afraid the only way to do this will be to put a 'door' script on each of the doors, which manages the opening and closing upon a message sent from this raycasting script using Send$$anonymous$$essage. Then, just put one instance of that script on each door.
I'm sorry for not explaining my self, I have the doors opening, bit i have to be facing the door to open and to close it , where it foces me to be facing the opened door object where i have to go behind the opened door just to close it, i figure if i place a trigger big enough to use as a collider around the door , that way i wouldn't have to be facing the opened door just to close it, .... i just have to be facing the frame, which has no animation
i have come around to where to place trigger and such, :( i just dont know the script needed to use such trigger ...please help
Use layers. Also, please post a new question, ins$$anonymous$$d of attaching your problem to this one.
Answer by superpig · Oct 16, 2011 at 03:29 PM
Building on syclamoth's comment, the way to do this is to have two separate scripts:
A script on each door, that stores whether the door is open (like your doorIsOpen variable) and handles the animation of the door opening or closing (like your Door function). I'd call the Door function something like 'Toggle' instead, or maybe have two separate functions, one that you call to open the door (which does nothing if the door is already open) and one that you call to close the door (which does nothing if the door is already closed).
A script on the player which does the Raycast forward every frame (or when a key is pressed, or whatever) and checks whether the object hit has a Door component attached. If so, use SendMessage() to instruct it to toggle/open/close. I'd recommend doing this when a key is pressed rather than every frame, so that the player doesn't accidentally toggle the door shut again when they walk up to it.
This will allow you to have any number of doors, or indeed other objects (e.g. chests, wardrobes, air vents...) which the player will Toggle when he walks up to them. It will also allow you to have NPCs that open/close doors by sending the appropriate messages to them, or whatever you like. The door takes care of "being a door" and all the behavior associated with that.
amazing post, im sorry but is there a way that i could use a trigger ins$$anonymous$$d of the door colliders ? ...
Probably. Could you explain why you want to do that, exactly?
im sorry for not been so clear, i used the code shown above, which i thank for posting it, but now seems i have to get so close to the door just to open, and to close it i have to go behind the object to close it, by placing a trigger, i would be able to control the distance and angle to which i can activate the door ins$$anonymous$$d of just facing the door object it self at a nose distance ....please help ... im a game art designer, program$$anonymous$$g is new to me .... basically trying to learn unity since so far its been so user friendly to import my models
The best thing to do is not to use triggers, but a regular collider, and to put the collider on another layer that you set to not collide with the player in the Physics Settings. That way you'll be able to ray cast against it as normal, but it won't block the player's movement.
could i use this same code on a double door window ...classic not rotating ....but it has two doors and both must open, but when i click it wont work, i used the code shown above on my doors ,,,and it worked awesome, but now im trying to use the same code on a window and doors which both are double doors, any suggestions
please help
Answer by Alismuffin · Oct 18, 2011 at 11:44 AM
Thank you very much both of you! Thanks to your help I managed to get it working exactly as I wanted. here's my two scripts just so anyone else trying to work this out will have a starting point:
This one I have attached to my player controller:
private var currentDoor : GameObject;
function Update () {
var hit : RaycastHit;
if(Physics.Raycast(transform.position,
transform.forward, hit, 3)) {
if(hit.collider.gameObject.tag == "Door" && Input.GetMouseButtonDown(0)){
currentDoor = hit.collider.gameObject;
currentDoor.GetComponent(DoorScript).OpenCheck();
}
}
}
And this one is attached to each door:
var doorIsOpen : boolean = false;
var doorOpenSound : AudioClip; var doorShutSound : AudioClip;
function OpenCheck (){
if(doorIsOpen == false){ Door(doorOpenSound, "doorOpen"); doorIsOpen = true;
}
else if(doorIsOpen == true){ Door(doorShutSound, "doorClose"); doorIsOpen = false;
}
}
function Door (aClip : AudioClip, animName : String) {
audio.PlayOneShot(aClip);
gameObject.transform.parent.animation.Play(animName);
}
Your answer
Follow this Question
Related Questions
Raycasting to make Buildings Clickable 1 Answer
How would I shoot out a ray where the mouse is clicked and check for collision in 2D. 1 Answer
Raycast not hitting the collider properly it always has a weird offset 0 Answers
Reacting to terrain height changes without a rigidbody component 0 Answers
Voxel Raycasting Help 0 Answers