- Home /
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
}
}
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.
Raycasts only return the first object they hit, so what you have is a single collider, not a collection of them.
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.
It would be every collider in the line of the ray, in the Layer
;) No problem
Give it a go, then if it works, hit us with some thumbs up :P
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.
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
Follow this Question
Related Questions
How to detect if a raycast ray stop hitting an object 1 Answer
Help destroying gameObject on RayCastHit? 1 Answer
I don't know why i can't detect by ray sth. tagged, 1 Answer
Raycast from inside an object 2 Answers
raycast to determine pivot 1 Answer