- Home /
Is it possible to have multiple layers on one object?
I applied mask1 for planes that construct maps and then using Physics.Raycast(ray, out hit, 1000, mask1) I know if it hit the right layer. On the other hand I want to apply mask2 to some of that planes for Pathfinder from Aron Granberg. But I cannot apply multiple layers for the same object. What I should do?
Answer by Owen-Reynolds · Mar 30, 2013 at 04:20 PM
Phy.raycast(.., mask1 | mask2) will hit anything selected by mask1 or 2. Or, you can make the mask directly. Say you want to hit layer 8 and 12 -- int layer8n12Mask = 1<<8 | 1<<12;
If you need the ray to hit all of 8, but only some things on 12 (the ones you wanted to put on 8 and 12,) but not the rest of 12. You have to split them out. Maybe make layer 14 be the "count as 8+12" layer. For pathfinding, you can check 12 and 14 in the list. For ray casts, can use mask8 | mask14.
you know you're totally 100% right Owen -- I vagued on it !!! deleted my incorrect answer. hey i believe your thingy just clicked over to "10k"
know any Facebook/P31 experts?! FORU$$anonymous$$ ... WOE cheers!
2016, v5.4 here, Raycasting is still a pain. Thank you for the great answer, I'll do this in my code (I set layer collisions based on difficulty so I need a dynamic set of 'what hits what' for some rays too). There's another reason -on top of saying thanks- for popping a comment: hopefully Unity devs. will read: would be great if I could cast Physics rays by handing over the "source layer" as a value/variable, so raycasting could use the physics / collision layer I defined / set. Cheers
There's a new Layer$$anonymous$$ask type for people who don't want to learn binary ops, which also allows you to look up layers by name. This old way works (and is better, I think, if you know it.) But pretty much, this answer is obsolete.
Ah...the trick is that raycasts aren't from anything, its just code that starts at a point. If the math you used happened to use some gameObject, well, fine, but not relevant.
Yes, the new LayerClass type can take multiple layers by name, and build that mask.
Sounds like you're wanting a feature like Layer$$anonymous$$ask.Get$$anonymous$$askFromPhysicsSettings(layerName);. You could make it with a loop, but you could add a feature request. Seems like a thing they might add.
Your answer