Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 MustacheBill · Oct 29, 2015 at 09:22 AM · scripting problemscript.door

Door open script opens all the doors in the scene

I have a door open/close script i have created. It works with tags. If raycast hits object tagged with "door" it opens the door. However it opens all the doors in the scene. Does anybody have an alternative i can try?

if (In put.GetKeyDown (KeyCode.E)) { print ("e was pressed");

         if (Physics.Raycast (ray, out hit, rayDistance))  //check for raycast
             {
             print ("Ray hit a surface");
     
                 if (hit.collider.gameObject.tag == "Door") //check for door
                     {
                     print ("ray cast on door");
                     //change crosshair
                     
                         if (gateOpen == false) //check for key press/gate is open(Input.GetKeyDown (KeyCode.E) && 
                         {
                         print ("e was pressed to open");
                         gateControl ("open");
                         gateOpen = true;
                         AudioSource.PlayClipAtPoint (gateOpenSound, transform.position);
                     //    print ("door is open");
     
                         }
     
                 else if (gateOpen == true) //check for key press2/gate
                     {
                         print ("e was pressed to close"); 
                         gateControl ("close");
                         gateOpen = false;
                         AudioSource.PlayClipAtPoint (gateCloseSound, transform.position);




Comment
Add comment · Show 2
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 DFT-Games · Oct 29, 2015 at 10:03 AM 1
Share

Hi @$$anonymous$$ustachBill, What you describe doesn't seem normal unless the animation is triggered on all the objects: can you post the content of the gateControl code?

avatar image MustacheBill · Oct 29, 2015 at 10:27 AM 0
Share

This is my full script by the way

public float rayDistance = 3f; public AudioClip gateCloseSound; public AudioClip gateOpenSound;

 // Use this for initialization
 void Start () {
     gateOpen = false;
     animator = GetComponent<Animator>();
 }
 
 // Update is called nce per frame
     void Update() 
 {
     RaycastHit hit; 
     Ray ray = Camera.main.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 0f));

     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E))
         {
         print ("e was pressed");
     

     if (Physics.Raycast (ray, out hit, rayDistance))  //check for raycast
         {
         print ("Ray hit a surface");

         if (hit.collider.gameObject.tag == "Door") //check for door
             {
             print ("ray cast on door");
             //change crosshair
             
                 if (gateOpen == false) //check for key press/gate is open(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E) && 
                 {
                 print ("e was pressed to open");
                 gateControl ("open");
                 gateOpen = true;
                 AudioSource.PlayClipAtPoint (gateOpenSound, transform.position);
             //    print ("door is open");

                 }

         else if (gateOpen == true) //check for key press2/gate
             {
                 print ("e was pressed to close"); 
                 gateControl ("close");
                 gateOpen = false;
                 AudioSource.PlayClipAtPoint (gateCloseSound, transform.position);
             //    print ("door is closed");            
             }

             }
         }
         }
 }


 
 void gateControl(string direction)
 {
     animator.SetTrigger (direction);
 }
 

}

3 Replies

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

Answer by Bonfire-Boy · Oct 29, 2015 at 10:43 AM

This line...

 if (hit.collider.gameObject.tag == "Door")

Checks to see if the raycast hit a door. Any door.

But the script is on all doors. So it is effectively saying to each door, "if the raycast hit any door, open this door". You want it to be more like "if the raycast hit this door, open this door". So, change it to...

 if (hit.collider.gameObject == this.gameObject)

Note that the script is on a door, so having checked that the raycast has hit the current object, you don't need to check that it's a door.

The way you've done things, every door is doing the raycast for itself. An alternative way of doing things, that's more efficient, is to do the raycast just once, in a manager object of some kind (ie there is only one of this kind of object in the scene). Then, the code would look more like what you've done. You would need to check that the object that's been hit is a door, but then make that door open. Something like this in the manager object's update function

 if (hit.collider.gameObject.tag == "Door")
 {
      hit.collider.gameObject.GetComponent<Door>().HandleHit();
 }

and a HandleHit() function in the Door script that does the opening/closing (or whatever).

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 MustacheBill · Oct 29, 2015 at 12:05 PM 0
Share

Thank you very much, looks like ill need to do some more work on these scripts. Appreciate the answer.

avatar image jmoocow2003 · Apr 24, 2018 at 12:45 AM 0
Share

A million thanks to you sir! :)

avatar image
2

Answer by screenname_taken · Oct 29, 2015 at 09:44 AM

What is that gateOpen and how is it related to the door? Because i think that you are checking if the raycast hit a door, and then just opens a door, instead of accessing the door open script on that specific door that it hit. (like hit.GetComponent.().open(); for example) Also, it's easier on resources if you are using collider.CompareTag("tag") instead of checking against a string.

Also, if (!gateOpen) is the same as if (gateOpen==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
avatar image
0

Answer by MustacheBill · Oct 29, 2015 at 10:25 AM

Bare with me, I pretty much made this script myself and I'm new to scripting. (don't be afraid to give me basic tips) This script is the only script used and it is attached to the door, should I instead have it on the player?

Thanks very much

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

35 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

Related Questions

Door Script compiler error 0 Answers

Why the cannon is never rotate looking at the target ? 1 Answer

Help with deselection please 1 Answer

Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer

How can i List objects by name but also in small text or big text or any kind ? 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