Be the first to post for 'Raycasting And Layer Masks'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

I’ve never really heard of Raycasting before this lecture, let alone what it is used for. It’s a good heads up and gives us students a chance to follow the sections better that are coming up.

Great explanation. Cheers

Whats the advantage of using LayerMasks over an If statement asking about the layer or a tag?
the performance is so better to justify all of this complexity?

1 Like

i have a question about the RayCasting ! do I need the ray caster for a gamepad controller or is it strictly for click and move games …

Strictly for click to move for now. You may need raycasting later if you want to move a cursor with the gamepad, for example to select a treasure chest or something.

There are other ways, like OnMouseOver() see https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html

Hi,

While I can see what you’re doing with the layer mask, I can’t see why you are doing it this way.

Why are you using
int layerMask = 1 << int (layer));
if (layerMask & hitMask)

instead of
if (layerMask == hitMask)

Is it just processor speed at runtime? I remember something from 68000 assembler many years back about AND being a basic operation and hence fast. Or is it just that raycasting uses a bit mask to determine what it can hit?

Its probably explained in the next lesson :slight_smile:

Thanks

My raycaster seems to see through walls, even though I check for enemies first and walls second… its seeing through the walkables or otherwise, and seeing enemies you cant actually see…

@ben being a programmer in the boring business world, I have never really ‘got’ or cared about bit shifting. You totally clarified it for me in this video (thanks much) and I think you might want to include it in the title. ( So I can find it easily again when I forget it in six months. :slight_smile: )

Still not sure why I would use bit shifting in most commercial applications (not counting maybe financial applications)

Ive been in health care most of my career and have never seen it used.

Thanks so much, I’m really glad I could help.

1 Like

Hi Ben,

I added an extra layer (for walls) , expanded the Layer enum, added my new value to cameraycaster script (before the walkable layer) but for some reason it doesn’t get hit when I raycast.

When i swap my wall layer with for example my enemy layer (and adjust the scripts) it will hit my wall but my enemy is not hit anymore by the raycast. I thought it would have something to do with the bit shift but i’m not seeing an issue with that. It just reads: int layerMask = 1 << (int)layer;

Hopefully you have an idea of what might be wrong?

Thanks in advance for your help and for this nice course!

Regards, Hans

I’ll be refactoring this heavily in the next section so hopefully it’ll be clear how to fix it after that.

Ah, just found it. I added the new layer in the raycaster script but didn’t update the properties in the unity inspector so it didn’t had any
effect :flushed:

Hi there. Your “true” example near the very end has 0010 being & with 0010, so…It looks like you are and-comparing the layer mask of layer 1 with itself rather than the ACTUAL layer 1 (which would be the integer 1, right?) with its mask. How would the actual layer ever return true? Or when to called the left hand column “layer”, did you not mean it would literally be using the layer integer in the comparison? Maybe the terminology is just confusing me here.

Privacy & Terms