• 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 /
  • Help Room /
avatar image
0
Question by DannyCampi · Apr 19, 2017 at 03:00 PM · script.dooropendoorsclose

Open/Close Door only if player is near the Door

Hi I have an Script in which the "Player" can open and close door but he can do it wherever he is. Also a Text ("Press E to Open Door"/"Press F to close Door" appears wherever the player is. What I need help is how can I do that the Player only can open/close the door when he is near it.(also the text). I have an Empty Gameobject (this one has the Script) which is the Hingle and a cube as the door. using UnityEngine; using System.Collections;

 public class doorOpenandClose : MonoBehaviour {
 
     //Text variables
     public bool T_ActivatedOpen = true;
     public bool T_ActivatedClose = false;
     public bool activateTrigger = false;
 
     public GameObject textO;
     public GameObject textC;
 
     //Animator variables
     Animator animator;
     bool doorOpen;
     //Sound variables
     public GameObject doorOpenSound;
     public GameObject doorCloseSound;
 
 
 
 
 
 
     void Start () { //what happens in the beginning of the game.
         textC.SetActive (false);
         textO.SetActive (false);
         T_ActivatedOpen = true;
         T_ActivatedClose = false;
 
         animator = GetComponent<Animator> ();
         doorOpen = false;
 
 
         doorCloseSound.SetActive (false);
         doorOpenSound.SetActive (false);
 
 
 
     
     }
     
 
     void Update () { //The main function wich controlls how the system will work.
 
         if (T_ActivatedOpen == true)
             T_ActivatedClose = false;
 
         else if (T_ActivatedClose == true)
             T_ActivatedOpen = false;
 
         if (activateTrigger && Input.GetKeyDown (KeyCode.E)) //For some reaseon you can't have both E (open and close).
             {
                 
                 T_ActivatedOpen = false;
                 T_ActivatedClose = true;
                 textO.SetActive (false);
                 textC.SetActive (true);
                 doorOpen = true;
 
                 doorOpenSound.SetActive (true);
                 doorCloseSound.SetActive (false);
 
             if (doorOpen) 
             {
                 doorOpen = true;
                 doorController ("Open");
             }
                 
             }
             else if(T_ActivatedClose && activateTrigger && Input.GetKey (KeyCode.F)) 
             {
                 T_ActivatedOpen = true;
                 T_ActivatedClose = false;
                 textO.SetActive (true);
                 textC.SetActive (false);
 
                 doorCloseSound.SetActive (true);
                 doorOpenSound.SetActive (false);
     
             if (doorOpen) 
             {
                 doorOpen = false;
                 doorController ("Close");
             }
                 
             } 
     }
 
 
                                                         
     void OnTriggerEnter(Collider col) //If you enter the trigger this will happen.
     {
         if(col.gameObject.tag == "Player")
         {
 
                 activateTrigger = true;
             if((T_ActivatedOpen == true))
             textO.SetActive (true);
 
             if((T_ActivatedClose == true))
                 textC.SetActive (true);
         }
         
     }
 
 
     void OnTriggerExit(Collider col) //If you exit the trigger this will happen.
     {
         if(col.gameObject.tag == "Player")
         {
             textO.SetActive (false);
             textC.SetActive (false);
             activateTrigger = false;
         }
 
     }
 
     void doorController(string direction) //Animator function.
     {
         animator.SetTrigger(direction);
     }
         
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alexanderameye · Apr 19, 2017 at 03:12 PM

To check whether or not the player is close to the door, you have two possible situations.

1) You want the text to show up when the player is close to the door

solution: Build an empty gameobject, add a collider to it, mark it as 'IsTrigger' and add a script onto it with OnCollisionEnter and/or OnCollisionStay functions in it. Then you can check if the player is close to the door (has collided with the collider) and open/close the door.

2) You want the text to show up when the player is looking at the door

solution: Add a script onto your player that has a RayCast function and check that when the ray hits a collider, that collider has a certain tag like 'Door', and only then show the text.

If you would like to see some code examples of this, take a look at this free package: https://www.assetstore.unity3d.com/en/#!/content/38694

Good luck,

Alex

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

100 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Open Door Animation Problem 0 Answers

Door keeps rotating 0 Answers

Door open/close sound effect?? 0 Answers

Need help getting my door to open and close without triggering other doors. 1 Answer

Help: My doors won't work properly >< 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