- Home /
Make mask layer have priority over other mask layer on raycast
Hi, I would like to know if there is a way to make raycast hit a layer with more priority than other layer.
I have two layers which I can click on them, but I want to raycast on one layer before another one, so is there a way o I have to use raycastAll and then check all elements and code the priority myself. I was hoping with the order in the layer editor the raycast check one layer before another one but it doesn't.
I hope I was clear enough.
Thanks!
Wow! Thanks everyone for the support! I don't even have time to read all the answers! ... ¬¬
It would be helpful if you explained why you need to do this as there are a couple of options but without knowing what the intended behaviour is it's hard to say what will work.
Using a Layer$$anonymous$$ask you can set the raycast to only hit the layers you specify or to ignore the layers you specify and hit everything else. You could use this to raycast against one layer and then the other to get the order you are looking for. However once it finds something on the layer it is looking for it will stop.
RaycastAll is probably a better idea and then just store the hits in the order they come in or calculate the distance between the hit and the source if that's what you are trying to use this for.
In regards to 'priority' the raycast is just going to hit whatever collider it encounters first. If those colliders are on top of each other then I suspect it will be based on scene hierarchy. Unless it is using a layermask, the layers the objects are on isn't going to make a difference.
Thanks for the answer! Ok I totally forgot to mention that I'm working on 2D. The idea to use 2 raycasts is good but is it expensive for mobiles?
Imagine I have multiple things in my scene, all of them are interactive (for example furniture) and I have also characters there, and when click on one of them I want to prioritize the click on the character even if there is a "furniture" in front of it.
What I'm currently doing is raycastAll and then I have a script that has a priority variable, which I read in the raycastAll method and then order by this variable. Another solution I thought was to move on Z the sprites, and then use the order in the sprite renderer, but I really didn't want to use the Z so I used the raycastAll option.
I just wanted to know if there was a way to tell the raycast to prioritize one layer over other. Thanks!!
Using a layer mask probably is the best idea then. Changing the Z depth so the character is in front of furniture would work but it'd still cause an issue if you wanted to click furniture behind them.
RaycastAll is probably more expensive I'd think as it is returning multiple raycasts anyway. Whereas raycasting once with layer mask furniture and/or once with character is going to be more simple.
Answer by raybarrera · Apr 14, 2016 at 02:38 AM
The only way I can think of doing this is by having two separate raycasts. I can see how the term "layer' is a bit misleading in this scenario, but as you mentioned, the order has no bearing on how a raycast works (when doing RaycastAll in this manner).
Your answer
Follow this Question
Related Questions
Layer Mask on raycast2d not working? 1 Answer
Why is RaycastHit2d Not working 0 Answers
Making a raycast ignore specific collider 1 Answer
Raycast Not Working 3 Answers
My Raycasts seem to sometimes miss 0 Answers