Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alismuffin · Oct 16, 2011 at 12:37 PM · collidersraycastingdoors

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);
     
 }
     
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Oct 16, 2011 at 12:40 PM 1
Share

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.

avatar image onelove · Feb 04, 2013 at 02:44 AM 0
Share

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

avatar image onelove · Feb 04, 2013 at 02:45 AM 0
Share

i have come around to where to place trigger and such, :( i just dont know the script needed to use such trigger ...please help

avatar image superpig ♦♦ · Feb 04, 2013 at 02:48 AM 0
Share

Use layers. Also, please post a new question, ins$$anonymous$$d of attaching your problem to this one.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image onelove · Feb 03, 2013 at 10:47 PM 0
Share

amazing post, im sorry but is there a way that i could use a trigger ins$$anonymous$$d of the door colliders ? ...

avatar image superpig ♦♦ · Feb 03, 2013 at 10:49 PM 0
Share

Probably. Could you explain why you want to do that, exactly?

avatar image onelove · Feb 04, 2013 at 03:48 AM 0
Share

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

avatar image superpig ♦♦ · Feb 04, 2013 at 08:47 AM 0
Share

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.

avatar image onelove · Feb 28, 2013 at 05:56 PM 0
Share

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

avatar image
0

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);
   }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges