- Home /
Raycast and Layer Masks
Back again, more problems :)
After completing and ironing out all the problems with making a collider based projectile and handling the damage across game objects, I realized as so many do. Fast moving collisions don't work. Kinda wish I was warned, but it lead to some extremely useful knowledge so I'm happy for the experience.
Moving along, I started to learn about ray casting. But I have some questions about it, and how to apply layer masking to it. Heres my code first.
var hit = (Physics.Raycast (transform.position, target, range));
if (hit != null) {
var testhit = hit.GetComponent(test);
if (testhit != null) {
print("Companion Cube in sight!");
}
}
Now as you can plainly see... I haven't gotten far. I was using code who's sintax I cannot seem to fathom using the lovingly emblished reference material. I thought I was applying a layer mask to variable testhit. The layer I was trying to restrict to was test. The only object in my test environment that has the layer test is a cube named 'Companion Cube'. But it was sorta a failure on the debug check. Kept spitting the same mumbo jumbo that every programmer I know hates. Error code: Something~in~greek
So moving along. What I need help with is the sintax involved. If you could show me my trouble spots using the same variables I used. (When people use cookie cutter expamples I get very lost as to what is a function/var/expression/blackmagic etc.)
Also, layer masks seem to be Exclusive only from what I've read (Which really makes me feel my code is fragged) is there a way to write it so that I don't have to isolate the layer mask as a variable and invert it with the whole var = ~var expression?
I'm fairly confident that once I understand the layer mask sintax, so I can restrict the contact of my ray-caster unity will stop having a hissy-cow about unexpected Boolean nonsense.
Next question I'm fairly sure is in-depth. Raycasting appears to cast the ray as far as the distance its marked for. Returning everything it bashes into (Maybe I'm totally wrong there, Sorry if I am please set me straight). But I don't want all my bullets to be piercing. I'd like to be able to restrict the selection to the first, second, third, All, objects it collides with, in order. This way I can add target's pierced. Or all. Or simply stop on the first target. Is there a way to isolate and restrict this or pick it apart as usable information?
On the flip side, if ray-casts don't push beyond the first collision, how am I to accomplish piercing bullets?
I know this is a lot of questions honestly, but they are all the same two subjects in the heading, and I feel splitting all this information up would cause me to lose sight of the concept as a whole. Thanks for reading if you got this far and haven't given up on me!
Your second question- there are two kinds of raycasts!
Physics.Raycast()
only returns the first hit,
Physics.RaycastAll()
returns an array of everything it hits.
Oh, and as for the layer masks- have a look at this. I have found it endlessly useful!
Layer$$anonymous$$askExtensions.Create() doesn't appear to exist in my copy of unity. Is this a pro only item, or something I need to download?
I've been at trying to get the java-code syntax to accept it for days in various arrangements. I'm being driven a little bonkers by this. $$anonymous$$y studies have come to a complete halt T.T
That one link looked awesome but sadly I couldn't get it to work, and find its sister-page in java.
And what exactly does >> mean. Its in the 'layermask' portion of the script reference. They show the example calling it a bitshift which means nothing to me sadly (Even after trying to figure it out via the gooles). Why would they use an example like that? I'm sorry to fume but if they'd used something common, I might have figured it out by now.
Answer by srmojuze · Apr 04, 2014 at 11:18 AM
Try this: http://docs.unity3d.com/Documentation/Components/Layers.html ...Basically each layer has a number (integer). Then you use the "bitshift" to specify which layers you WANT to collide the ray with and which you DON'T. Good luck!
Your answer
![](https://koobas.hobune.stream/wayback/20220613004647im_/https://answers.unity.com/themes/thub/images/avi.jpg)