Raycast and Layer Masks

Back again, more problems :slight_smile:

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!

Try this: Unity - Manual: Layers ā€¦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!