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 Reefer · Jul 11, 2014 at 09:53 AM · dooropening

Opening door.

Hello, I have this simple script of opening doors:

 #pragma strict
 
 private var guiShow : boolean = false;
 
 var isOpen : boolean = false;
 
 var door : GameObject;
 
 var rayLength = 10;
 
 function Update()
 {
     var hit : RaycastHit;
     var fwd = transform.TransformDirection(Vector3.forward);
     
     if(Physics.Raycast(transform.position, fwd, hit, rayLength))
     {
         if(hit.collider.gameObject.tag == "Door")
         {
             guiShow = true;
             if(Input.GetKeyDown("e") && isOpen == false)
             {
                 door.animation.Play("dooropen");
                 isOpen = true;
                 guiShow = false;
             }
             
             else if(Input.GetKeyDown("e") && isOpen == true)
             {
                 door.animation.Play("DoorClose");
                 isOpen = false;
                 guiShow = false;
             }
         }
     }
     
     else
     {
         guiShow = false;
     }
 }
 
 function OnGUI()
 {
     if(guiShow == true && isOpen == false)
     {
         GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Use Door");
     }
 }

Now.. I've put it in my character and it works etc. But I have to manually everytime change that variable door : GameObject to which door I want to open.

Could someone point me to the right way how to get this script to look itself/automatically for which door I'm looking at as now it only works for that one door as it needs to be manually changed from editor if you wanna open up another doors than that one.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MSpiteri · Jul 11, 2014 at 10:42 AM

Hello there,

You do not need the door variable at all.

In the Raycasting command, remember that the variable 'hit' will contain the information about the object that you have hit with the raycast. So, hit.collider.gameObject will refer to the door.

Basically, you should replace this:

 door.animation.Play("dooropen");

with this

 hit.collider.gameObject.animation.Play("dooropen");

And do the same for the close door.

Comment
Add comment · Show 2 · 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 Reefer · Jul 11, 2014 at 11:19 AM 0
Share

This causes every door open up on wrong side, but it's fixable by doing animations again.. Thanks god I didnt do alot of those door open/close animations. Thank you anyways it works and it's easy to fix at this point still.

avatar image MSpiteri · Jul 11, 2014 at 11:44 AM 0
Share

For opening and closing a door, I suggest that you make the door open vertically (ie. it moves upwards) like in Doom 1 and Doom 2 :) That will always open properly ;)

avatar image
0

Answer by PiraLezz · Jul 11, 2014 at 11:38 AM

 //function in the doors script
 
 var open : boolean = false;
 
 function OpenClose()
 {
     if(open)
     {
         animation.Play("dooropen");
         open = true;
     }
     else
     {
         animation.Play("DoorClose");
         open = false;
     }
 }
 
 // when you must open/close a door you call the OpenClose function of the door
 
 if(Physics.Raycast(transform.position, fwd, hit, rayLength))
 {
     if(hit.collider.gameObject.tag == "Door")
     {
         guiShow = true;
         if(Input.GetKeyDown("e") && isOpen == false)
         {
             hit.transform.SendMessage("OpenClose",SendMessageOptions.DontRequireReceiver);
             guiShow = false;
         }
 
     }
     else
     {
         guiShow = false;
     }
 }
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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[WILLING TO EVEN PAY $] Working Dragging objects/doors like in Amnesia, But Need Simple FIX 3 Answers

Open door as Character approaches 2 Answers

Door Opening And Closing 2 Answers

How to open individual prefab doors 1 Answer

Need Help | Door Opening Script Error 1 Answer


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