• 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 bpears · Oct 18, 2013 at 11:54 PM 0
Share

I had a feeling, dang. I'm trying to look through what a raycast has hit, and what it has not hit, by first using predfined array of objects, and getting their colliders at start. And checking against those colliders

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 bpears · Oct 18, 2013 at 11:59 PM 0
Share

im shooting several rays at once

avatar image bpears · Oct 19, 2013 at 12:02 AM 0
Share

@meat5000 catch is im shoot many rays within a layer

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 bpears · Oct 19, 2013 at 12:19 AM 0
Share

I edited my post to include some code and things, but ah, I did not know it would return an array, but also, I am not raycasting directly at every collider, so I dont know if this would work in my case, as I dont want to raycast every collider in the layermask, but to check if I have hit those colliders.

avatar image bpears · Oct 19, 2013 at 12:21 AM 0
Share

I know it sounds weird as hell..

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 bpears · Oct 19, 2013 at 12:31 AM 0
Share

ah! Well this makes things easy peasy.. Thanks!

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

avatar image bpears · Oct 19, 2013 at 12:42 AM 0
Share

oh, and how would I check which colliders were not hit in the layer? Just the same as I have it but with RaycastAll?

avatar image bpears · Oct 19, 2013 at 12:46 AM 0
Share

I should mention I need to get the hit.distance returned... would raycastAll stack that value or return an array of values?

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

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. $$anonymous$$ix 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 Layer$$anonymous$$asks. Read and absorb. It's totally worth it.

avatar image meat5000 ♦ · Oct 19, 2013 at 12:57 AM 0
Share

With the 'All' functions the Array of info that is returned is RaycastHit[] info.

Cobbled some of these comments and converted it to an answer, for completeness.

avatar image bpears · Oct 19, 2013 at 02:13 AM 0
Share

couldnt I just do

 private var hitTargets : GameObject[];
 var pre$$anonymous$$adeArray : GameObject[];
 
 

Update(){ //do raycasting if (Physics.RaycastAll (ray, hit, $$anonymous$$athf.Infinity,1<<4)){ hitTargets = hit.collider.transform.parent.gameObject for (var go1 in hitTargets){ for(var go2 in pre$$anonymous$$adeArray){ for(go2 == go1){ //dostuff } for(go2 != go1){ //dostuff } } } } }

avatar image Hoeloe · Oct 19, 2013 at 08:32 AM 0
Share

You can't treat arrays as though they are single objects. Think of arrays not as "groups of objects", but "groups that happen to contain certain objects". This line here:

 hitTargets = hit.collider.transform.parent.gameObject

Will not work, because when you create an array, you define the type of objects to be stored in it, but it is still its own distinct object. Because of this, arrays don't have a collider member. You can't just pull out arrays of objects like that. The collider member is specifically one collider attached to one RaycastHit. You cannot take a collection of objects and build an array by referencing the member of an object like that.

What you can do, though, is to construct the array explicitly. The reason you can't do it like you've tried to do is because it breaks the typing system - and quite badly. However, what you actually want to do is "look through all of the RaycastHit objects, and store the attached GameObjects in an array." You can do this, it just takes a few more lines of code.

So first of all, let's make an empty array that is the right size:

 hitTargets = new GameObject[hit.Length];

This makes a completely blank array that is big enough to store one object for each collision the Raycast detected.

So now all we need to do is look through all of the RaycastHit objects... and as it turns out, there's a nice way of doing that: for loops. Now, the first approach might be to use the foreach loop (which is the one you're using there), but you should quickly see that that's not a good idea, because we also need the index of the array element, in order to assign it to the new array, so we need something like this:

 for(var i = 0; i < hit.Length; i++)
 {
     hitTargets[i] = hit[i].collider.transform.parent.gameObject;
 }

Now, the difference between this and your code is that this only refers to one object at a time - hitTargets[i] is just a GameObject, not an array, and the same is true for hit[i]. Because hit[i] is no longer a collection, you can refer to it as you would a single RaycastHit - and because this iterates through the entire array, you end up with hitTargets storing all the GameObjects attached to the Raycast collisions.

avatar image meat5000 ♦ · Oct 19, 2013 at 10:45 AM 0
Share

Don't forget that that array is a RayCastHitInfo[] array, not a gameobject or collider array.

$$anonymous$$eep in $$anonymous$$d that for loop in a for loop in a raycastall will be potentially expensive, but yes, using a pre-defined array IS a good idea to save on processing, avoiding the dynamic population which will rob all your cpu cycles.

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

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

How to detect if a raycast ray stop hitting an object 1 Answer

Help destroying gameObject on RayCastHit? 1 Answer

How to hit two objects with only one shot (raycast) 0 Answers

what is the best way to detect if an object has been clicked on? 1 Answer

Lag when I change the layer of a game object ingame 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