Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
12
Question by KubeX · Mar 14, 2013 at 06:34 PM · raycastlayersignore

Making raycast ignore multiple layers

I know how to make the raycast ignore a layer but I want it to ignore layers 9 and 10 but collide with the rest of the layers.

Currently, the raycast ignores layer 9.

 // Bit shift the index of the layer (9) to get a bit mask
   var layerMask = (1 << 9);
 layerMask = ~layerMask;
 
 if (Physics.Raycast(transform.position, transform.forward, Hit , Range, layerMask))
     {
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Aug 30, 2018 at 11:01 PM 0
Share

Simply adjust:



Physics.kIgnoreRaycastLayer

project wide

6 Replies

· Add your reply
  • Sort: 
avatar image
51
Best Answer

Answer by AlucardJay · Mar 14, 2013 at 06:39 PM

It is much easier to declare a variable that is typecast as LayerMask, then in the Inspector, simply tick the layers that you wish to be detected by the raycast. As a test, create a new script, at the top put

 var myLayerMask : LayerMask;

Now save this script, go back to Unity and attach the script to an empty gameObject or the camera (it doesn't matter, this is just a test). Now look in the inspector, click on the drop-down menu then tick the layers you wish to be acknowledged. This is much easier than trying to implement the bitshift method in the documentation. Now you've seen how it works, simply use :

 var myLayerMask : LayerMask;

 // in a function
 if ( Physics.Raycast(transform.position, transform.forward, Hit, Range, myLayerMask) )
 {
     // Do Stuff
 }

http://answers.unity3d.com/questions/409360/raycasthit-behind-object.html

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KubeX · Mar 14, 2013 at 07:25 PM 0
Share

Thanks! This is much easier to use.

avatar image agentsmith · Mar 14, 2013 at 07:27 PM 0
Share

That's a great tip thanks! However if I wanted the Layer$$anonymous$$ask to be private, how would this be coded?

avatar image AlucardJay · Mar 14, 2013 at 07:59 PM 0
Share

Is there a specific reason why you want this to be a private variable? As you are assigning the values in the Inspector, if you make it private then this ability is lost (private variables do not appear in the inspector, they are private), then you are back to the same problem where you have to use some bitshift calculations to select layers.

avatar image entity476 · Jun 04, 2013 at 11:11 AM 0
Share

Unless, you really mean that you need a Private Variable and not a hidden Public one, you could first, and while the Variable is Public, select the Layers you desire in the Inspector, and then to use the "@HideInInspector", before the line you declare the Layer$$anonymous$$ask Variable. Notice that for each Variable you want to hide in the Inspector, you have to write the above statement. However, the whole method misses its usability, as alucardj already said, this way.

avatar image Bunny83 · Jun 04, 2013 at 11:28 AM 11
Share

I would do it the other way round: declare the variable private and use the SerializeField attribute. This way you can edit the variable in the inspector and keep it private so other scripts can't directly access it.

Of course when you don't want to show it in the inspector (to be able to change the layermask) you have to choose the layers in your code manually.

I take the example from the OP:

     var layer$$anonymous$$ask = (1 << 9);
     layer$$anonymous$$ask |= (1 << 13);
     layer$$anonymous$$ask |= (1 << 15);
     layer$$anonymous$$ask = ~layer$$anonymous$$ask;

Or in one line:

     var layer$$anonymous$$ask = ~( (1 << 9) | (1 << 13) | (1 << 15) );

This would exclude layer 9, 13 and 15.

$$anonymous$$eep in $$anonymous$$d that the IgnoreRaycastLayer should also be ignored. You can use the Physics.kIgnoreRaycastLayer constant which is the layermask for the layer2 (has a value of 4 == 1 << 2 ). So just add this:

     [...]
     layer$$anonymous$$ask |= Physics.kIgnoreRaycastLayer;
     layer$$anonymous$$ask = ~layer$$anonymous$$ask;
     [...]
avatar image Rafaelski Bunny83 · Nov 08, 2020 at 10:54 PM 0
Share

$$anonymous$$any Thanks! It Worked perfectly for me.

Show more comments
avatar image
15

Answer by Spiff McShizly · Nov 25, 2014 at 01:43 PM

 layerMask = 1 << LayerMask.NameToLayer ("layerX"); // only check for collisions with layerX
 
 layerMask = ~(1 << LayerMask.NameToLayer ("layerX")); // ignore collisions with layerX
 
 LayerMask layerMask = ~(1 << LayerMask.NameToLayer ("layerX") | 1 << LayerMask.NameToLayer ("layerY")); // ignore both layerX and layerY

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
8

Answer by Bryan-Legend · Apr 15, 2016 at 09:10 PM

Make sure you're not passing in the layer mask in the distance argument position.

Do This

     if (Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, SelectionLOSCheckMask))

NOT THIS

     if (Physics.Raycast(transform.position, transform.forward, out hit, SelectionLOSCheckMask))
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 16, 2016 at 04:15 AM 2
Share

Please don't post the same answer to several questions. Also your answer doesn't answer the question at all. The code in question does have the distance parameter setup correctly so this answer doesn't address the actual issue in this question.

If you want to add some hints you might want to post a comment next time.

avatar image Andre_51X · Jun 01, 2018 at 06:50 PM 0
Share

This helped me save hours of headaches. Thanks!

avatar image
2

Answer by guilhermereisrbm · Sep 17, 2017 at 03:51 PM

Here's how you ignore any layers you want:

using namespace System;

private int layerMask = Convert.ToInt32("11111111111111111111100011111111", 2);

this ignores layers 8,9,10 (where the zeroes are)

Remember the lowest order bit is zero-indexed - that means layer 8 is the 9th position from the right.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 17, 2017 at 04:10 PM 0
Share

Uhm that's not really easier, is it? It's kinda hard to tell where layer 23 is.

Using a method like this would be better:

 public static int CreateLayer$$anonymous$$ask(bool aExclude, params int[] aLayers)
 {
     int v = 0;
     foreach(var L in aLayers)
         v |= 1 << L;
     if (aExclude)
         v = ~v;
     return v;
 }

Now you can simply do:

 int layer$$anonymous$$ask = CreateLayer$$anonymous$$ask(false, 2, 5, 7); // only cast against the layers 2, 5, 7 and ignore all others
 
 int layer$$anonymous$$ask = CreateLayer$$anonymous$$ask(true, 0, 1, 6, 8); // cast against all layers except layers 0, 1, 6, 8

However if you like to work with 32 digit binary numbers, go for it -.- I think using hex constants would be more practical. Each hex digit stands for exact 4 bits.

 0xFFFFFFFF // all layers
 0x00000100 // only layer 8
 0x00000300 // layer 8 and 9
 0xFFFFFDFF // all layers except layer 9

avatar image guilhermereisrbm · Sep 17, 2017 at 05:30 PM 0
Share

that's perfect

avatar image Maloke guilhermereisrbm · May 10, 2018 at 02:07 PM 0
Share

Using the "Convert.ToInt32" solved my problem in just one line of code :) Thanks very much!!!

avatar image
0

Answer by BLior · Apr 10, 2018 at 09:45 AM

The easiest way to choose is this I think:

[SerializeField] private LayerMask Name;

And then you can check the layers you want in the inspector.
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make raycast ignore a layer? 1 Answer

How to make a user layer ignore raycasts of OnMouse* events? 1 Answer

Use Layer Collision Matrix for Raycast 1 Answer

Why won't Physics.RaycastNonAlloc mask layers properly? 1 Answer

Make everything above the floor my player is standing invisible when my camera cant see my character 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges