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 /
avatar image
5
Question by chema.sre · Oct 08, 2012 at 04:03 PM · raycastlayermask

Raycast ignores my layer mask?

Hi, this is my first question in Unity Answers.

I want to do a ray cast against layer 9 so I wrote this piece of code

                 Ray r = camera.ScreenPointToRay(touchPosition);
                 RaycastHit hit;
                 int mask = (1 << 9);
                 if(Physics.Raycast(r, out hit, Mathf.Infinity, mask))
                 {
                     startPosition =touchPosition;
                     catchedObject = hit.transform;
                     Debug.Log("Touched object " + hit.transform.gameObject.name + " layer is " + hit.transform.gameObject.layer);
                 }
                 

Surprisingly, the output is

 Touched object MainCamera layer is 14


The Main Camera has a sphere collider with layer 14, but the ray cast mask should filter it, right?

So, where is the problem?

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 Jeff-Kesselman · Apr 02, 2013 at 06:00 PM 0
Share

Are you colliding with a physics object or a trigger object?

I've found trigger objects seem to react oddly to the mask, if that helps.

8 Replies

· Add your reply
  • Sort: 
avatar image
45

Answer by Bryan-Legend · Apr 15, 2016 at 09:09 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 7 · 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 FullHeartGames · Apr 25, 2016 at 03:17 AM 2
Share

Duuddde thank you thank you, I have been trying to solve this for days and even though the documentation is different than this, this fixed everything, thank you!!

avatar image nenok1 · Dec 20, 2016 at 03:08 PM 2
Share

This IS the answer, thanks a lot.

avatar image Bunny83 nenok1 · Dec 21, 2016 at 03:35 AM 1
Share

This is not the answer. It might be a helpful addition but has no relevance to the question as the OP actually used $$anonymous$$athf.Infinity in the original code. Specifically the OP used this version of Raycast:

 public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layer$$anonymous$$ask)

and passed the parameters correctly.

avatar image Sixbraker · Jan 15, 2018 at 10:56 AM 0
Share

omg dude thank u it worked; after 5 hours of fiddling, smashing my head on a locker, punching myself to the face, omg omg omg

avatar image DragonSix · Jan 18, 2018 at 11:52 PM 0
Share

It's 2018 and I had to stumble upon this to explains the weird collision issues I kept having. Thanks man.

avatar image SugarSores · Aug 04, 2019 at 08:20 AM 0
Share

Hell yes! $$anonymous$$y eyes totally danced over this. Thanks!

Show more comments
avatar image
14

Answer by BBG · Oct 08, 2012 at 05:51 PM

mask is an int. It should be defined as type LayerMask.

Also it's easier to do :-

public LayerMask mask;

Then set the desired layer(s) in the inspector. Then you don't need to do the binary stuff.

Comment
Add comment · Show 1 · 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 LavishSky · Oct 03, 2016 at 08:02 AM 2
Share

As a new unity developer, I appreciate the tip about the public variable. I was struggling with a related issue where I was initializing the layermask variable in code and wondering why my changes didn't affect the program. Turns out that in the editor, the variable was already initialized there.

avatar image
2

Answer by lloydv · Jun 22, 2021 at 01:34 AM

Make sure you're using LayerMask.GetMask

LayerMask.NameToLayer returns the index of the layer as seen in the layer inspector, not the mask. Pretty easy mistake to make, since it's the LayerMask class, so I hope Unity would update their comment for the NameToLayer method to make this clearer.

Comment
Add comment · Show 1 · 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 MagonX · Jul 04, 2021 at 12:21 PM 0
Share

Thank you! That was exactly my problem, I changed for LayerMask.GetMask and it worked :)

avatar image
1

Answer by foxor · Oct 09, 2012 at 05:01 AM

From the docs:

layerMask:    A Layer mask that is used to selectively ignore colliders when casting a ray.

So you have created the mask that ignores only layer 9

try:

 int mask = ~(1 << 9);

For the exact opposite meaning

Comment
Add comment · Show 4 · 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 pixelsyntax · Feb 28, 2013 at 12:30 PM 1
Share

This appears to be correct from my testing, despite the official documentation see$$anonymous$$g to indicate otherwise.

avatar image Bunny83 · Feb 28, 2013 at 01:16 PM 1
Share

No, the layermask should have a 1 for each layer that should be tested and a 0 for each layer that should be ignored. You interpreted the sentence the wrong way.

The two constants in the Physics class are declared like this:

     public const int kDefaultRaycastLayers = -5;
     public const int kAllLayers = -1;

kDefaultRaycastLayers has the value 0xFFFFFFFB (all1 except the 3rd bit which is the ignoreraycast layer)

kAllLayers has the value of 0xFFFFFFFF

kDefaultRaycastLayers is used when you don't pass a layermask

avatar image BAST! · Feb 28, 2013 at 01:37 PM 0
Share

You don't have to invert the mask after shifting to raycast a specific layer, see http://docs.unity3d.com/Documentation/Components/Layers.html for some good examples.

avatar image pointcache · Jul 25, 2014 at 04:16 PM 1
Share

LOoool it is! It's eather a bug or a mistake!

avatar image
0

Answer by ShoutTree · May 17, 2020 at 03:09 PM

After a few tests, I think there's a bug for the Physics.Raycast api (the layerMask behaves abnormally).

I tested the one @Lone-Coder mentioned

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

and the one the @chema.sre used in the question

public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask)

Both failed for the correct layerMask. and if I do it like @foxor , it works.. But the result is not what I want since other objects of different layers will also return true if a ray intersection happens.

If I change to Collider.Raycast, there's no such problem.

Plus, I'm using Unity 2019.3.12f1

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

29 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 avatar image avatar image avatar image

Related Questions

SphereCast doesn't work with a layerMask 1 Answer

unity raycast layers in Multiplayer 0 Answers

Layer Mask on raycast2d not working? 1 Answer

I have a problem with Layermask Unity2D 0 Answers

layers not ignored by raycast 1 Answer


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