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 RealMTG · Feb 16, 2015 at 11:07 PM · raycastarray

Only one raycast out of two works as intended

Hi!

First of all, the name for this problem was hard to come up with. Second of all, here's the problem.

So I have this RTS colony style game and I want to be able to build corridors and I want the doorways to connect to other doorways. But here's my problem. I have 2 doorways on a simple corridor. Only one works to detect a doorway that the corridor can be attached too. If I rotate it, it detects the other doorway from the other corridor I want to connect with.

Here's my code for this detection:

 Doorway[] doorways = cursor.GetComponentsInChildren<Doorway>();
 
             Debug.Log(doorways.Length);
 
             foreach (Doorway door in doorways)
             {
                 if (Physics.Raycast(door.transform.position, door.transform.forward, out doorHit, 1.1f, doorLayer))
                 {
                     Debug.Log("Can place room");
                     Debug.DrawRay(door.transform.position, door.transform.forward, Color.green);
                     canPlaceRoom = true;
                 }
                 else
                 {
                     Debug.Log("Can't place room");
                     Debug.DrawRay(door.transform.position, door.transform.forward, Color.red);
                     canPlaceRoom = false;
                 }
             }
 
             Debug.Log(canPlaceRoom);

I know what the problem is (it is that it uses the first doorway it can find in the array) or at least I think I know but I do not know how I can solve it.

Thanks in advance.

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
0

Answer by curiouspers · Mar 03, 2015 at 11:21 AM

if it works ok with second door when you rotate, then you have to use 2 raycasts, one is yours and second with -door.transform.forward that will flip the direction of ray.

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 lordlycastle · Mar 03, 2015 at 01:10 PM

You are setting the direction of the ray relative to the doorway's transform, so when you rotate it, so does the ray. If you are looking for absolute forward direction, then use Vector3.forward instead. Other than that, the code is fine. The issue is in your logic. So more context would be helpful.

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

21 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

Related Questions

How to write raycast arrays? 2 Answers

Making an Array of Rays 1 Answer

Instantiate same PreFab to an array of GameObject by RayCast. 1 Answer

NullReferenceError, Tilemap Array with Transforms and Raycast (C# with Demo) 0 Answers

How to change colour of Game objects when hit by ray cast using array 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