Raycasts Ignore Some Layers

Hello

For my game i’m working on I need my npcs to ignore objects when they use their raycasts for the their line of sights. However I need my player to be able to shoot a raycast that affects the target the npcs ignore. Is this possible through the use of layers?

Or can you not make gameobjects specific raycasts?

(I hope i’m wording this right, it’s a weird question I know)

Thanks

When you raycast, you can supply a layermask as one of the parameters. Your layermask can be set up in the property inspector, which is a lot easier to do than through code. In the inspector you are able to check off the layers you’d like to be affected by the raycast. Each separate call to the raycast function, you can use whatever layer mask you want too.

See the docs here:

Look for the overload of the function that includes the layer mask parameter. Actually, I think layer mask can be supplied for any of the overloads, it’s just an optional parameter for them all, I believe.

2 Likes

Thank you! I’ll take a look!