• 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 bpears · Oct 18, 2013 at 11:40 PM · gameobjectraycastcolliderhit

checking colliders from multiple raycast

Trying to raycast within a layerMask and check which colliders were hit, which were not, by checking them in a for loop

aArrayOfColliders is obtained at Start()

 Ray ray = //basically a group of rays
 
    if (Physics.Raycast (ray, hit, Mathf.Infinity,1<<4)){
  
         for(var hit in aArrayOfColliders){
         //do stuff
         }
         for (var !hit in aArrayOfColliders){
         //do something else
         }
 }
                             
 
Comment
Add comment · Show 18
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 Hoeloe · Oct 18, 2013 at 11:47 PM 2
Share

What are you trying to do? The foreach loop (which is the name of the loop you're using) simply looks through all variables in a collection. For one thing, a collider is not a collection, so you cannot iterate through it, and secondly, putting ! in front of it makes no physics sense, as that would have to mean "loop through everything that is not in this collection", which is not necessarily defined.

I'm really not sure what you're trying to do, but I gave an explanation of foreach loops on another question that you can find here: http://answers.unity3d.com/questions/548266/searching-through-an-array.html

I'm not sure what you think the foreach loop does, but judging from what you've written, it almost certainly doesn't work how you think.

avatar image Hoeloe · Oct 18, 2013 at 11:57 PM 2
Share

Raycasts only return the first object they hit, so what you have is a single collider, not a collection of them.

avatar image meat5000 ♦ · Oct 19, 2013 at 12:13 AM 1
Share

Did you click the RayCastAll link? notice how it returns an array of hits. It really sounds like this is what you need.

The Layer$$anonymous$$ask makes sure your RayCast is only checked against colliders in that specified Layer.

avatar image meat5000 ♦ · Oct 19, 2013 at 12:29 AM 1
Share

It would be every collider in the line of the ray, in the Layer

avatar image meat5000 ♦ · Oct 19, 2013 at 12:32 AM 1
Share

;) No problem

Give it a go, then if it works, hit us with some thumbs up :P

Show more comments

1 Reply

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

Answer by meat5000 · Oct 18, 2013 at 11:59 PM

Fire as many rays as you like, they will still hit the first object.

Try RaycastAll

If you wish to be selective with your RayCasts, employ a LayerMask.

To find what's NOT hit:

You would need to collect all the objects' information in the layer.

Check the Physics page in the Scripting Reference.

OverlapSphere will return all colliders within a radius from a point.

SphereCastAll is just like RayCastAll but is a thick beam.

Use OverLap to find ALL colliders, or, use SphereCastAll to find all colliders in the fat beam in a direction, then use RayCastAll to fire a smaller beam for the hit. Mix these up and you should have two separate arrays. One with all objects and one with objects hit.

And Here is an awesome QA on using LayerMasks. Read and absorb. It's totally worth it.

Comment
Add comment · Show 1 · 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 bpears · Oct 19, 2013 at 03:01 AM 0
Share

i posted my last breath of brainstorm. please let me know if brainstorm==brainfart. If so, seems I will have to do the doublecast. Thanks for help!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Detecting other Objects when placing Object 1 Answer

Click&Drag Misterious Disappearing! 1 Answer

Linecast from raycast hit point 1 Answer

How to hit two object with one raycast? 2 Answers

Assigning a transform using the ray cast collider. 2 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