• 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 ronie635 · Mar 27, 2018 at 08:17 PM · raycastdoors

Opening multiple doors with raycast,

Hi!

Beginner here, so bear with me please :)

I have an almost fully working script, where I open doors with raycast, but multiple doors cause a problem. Basically the script does not understand the boolean of the opened door, which in turn means that I only can open and close the same door, and not for ex.. open two adjacent doors after one another - without it displaying the door-closing animation first.

Can someone point out the mistake I am making? Thanks in advance!

using System.Collections; using UnityEngine;

public class RayCastDoor : MonoBehaviour {

 bool guiShow = false;
 bool isOpen = false;

 public int rayLength = 2;

 void Update()
 {
     RaycastHit hit;
     Vector3 fwd = transform.TransformDirection(Vector3.forward);

     if(Physics.Raycast(transform.position, fwd, out hit, rayLength))
     {
         if (hit.collider.gameObject.tag == "door")
         {
             guiShow = true;
             {
                 if(Input.GetKeyDown("f") && isOpen == false)
                 {
                     hit.collider.gameObject.GetComponent<Animation>().Play("DoorRayOpen");
                     isOpen = true;
                     guiShow = false;
                 }

                 else if(Input.GetKeyDown("f") && isOpen == true)
                 {
                     hit.collider.gameObject.GetComponent<Animation>().Play("DoorRayClose");
                     isOpen = false;
                     guiShow = false;
                 }
             }
         }
     }
     else
     {
         guiShow = false;
     }
 }
 void OnGUI()
 {
     if(guiShow == true && isOpen == false)
     {
         GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Open Door");
     }
     else if(guiShow == true && isOpen == true)
     {
         GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Close Door");
     }
 }

}

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 toddisarockstar · Mar 28, 2018 at 06:06 AM 0
Share

technically you should have an array or list of bools and or game objects to represent each door independantly but if you are a newbie..........

make a separate script to attach to the doors !!!!

when the player gets the raycast hits name of "door" it should change a bool or run a function in the door's script.

avatar image ronie635 · Mar 28, 2018 at 11:14 AM 0
Share

Thanks toddisarockstar! A good point there, I just might try that if i cannot get this figured out in the current script. Thanks for your time!

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

door that closes by itself 2 Answers

Is it possible to allow a raycast to pass through a collider to hit things behind it? 6 Answers

Raycast to Terrain (Conditional Statements) 1 Answer

Follow Nearest Target 1 Answer

LayerMask for RayCast 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges